-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added basic espresso tests
- Loading branch information
Showing
5 changed files
with
313 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...ndroidTestMedicmobilegammaWebviewDebug/java/org/medicmobile/webapp/mobile/LoginTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package org.medicmobile.webapp.mobile; | ||
|
||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.view.ViewParent; | ||
import androidx.test.espresso.DataInteraction; | ||
import androidx.test.espresso.ViewInteraction; | ||
import androidx.test.espresso.web.webdriver.DriverAtoms; | ||
import androidx.test.espresso.web.webdriver.Locator; | ||
import androidx.test.filters.LargeTest; | ||
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner; | ||
import androidx.test.rule.ActivityTestRule; | ||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeMatcher; | ||
import org.junit.FixMethodOrder; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.MethodSorters; | ||
import java.util.Locale; | ||
import static androidx.test.espresso.Espresso.onData; | ||
import static androidx.test.espresso.Espresso.onView; | ||
import static androidx.test.espresso.action.ViewActions.click; | ||
import static androidx.test.espresso.assertion.ViewAssertions.matches; | ||
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withId; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withParent; | ||
import static androidx.test.espresso.web.assertion.WebViewAssertions.webContent; | ||
import static androidx.test.espresso.web.assertion.WebViewAssertions.webMatches; | ||
import static androidx.test.espresso.web.matcher.DomMatchers.hasElementWithId; | ||
import static androidx.test.espresso.web.sugar.Web.onWebView; | ||
import static androidx.test.espresso.web.webdriver.DriverAtoms.clearElement; | ||
import static androidx.test.espresso.web.webdriver.DriverAtoms.findElement; | ||
import static androidx.test.espresso.web.webdriver.DriverAtoms.getText; | ||
import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick; | ||
import static org.hamcrest.Matchers.allOf; | ||
import static org.hamcrest.Matchers.anything; | ||
import static org.hamcrest.Matchers.containsString; | ||
|
||
@LargeTest | ||
@RunWith(AndroidJUnit4ClassRunner.class) | ||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
public class LoginTests { | ||
|
||
private static final String ERROR_INCORRECT = "Incorrect user name or password. Please try again."; | ||
|
||
@Rule | ||
public ActivityTestRule<SettingsDialogActivity> mActivityTestRule = new ActivityTestRule<>(SettingsDialogActivity.class); | ||
|
||
@Test | ||
public void testLoginScreen() throws Exception { | ||
DataInteraction linearLayout = onData(anything()) | ||
.inAdapterView(allOf(withId(R.id.lstServers), | ||
childAtPosition( | ||
withId(android.R.id.content), | ||
0))) | ||
.atPosition(2); | ||
linearLayout.perform(click()); | ||
Thread.sleep(10000);//TODO: use better ways to handle delays | ||
|
||
ViewInteraction webView = onView( | ||
allOf(withId(R.id.wbvMain), | ||
withParent(allOf(withId(R.id.lytWebView), | ||
withParent(withId(android.R.id.content)))), | ||
isDisplayed())); | ||
webView.check(matches(isDisplayed())); | ||
onWebView() | ||
.check(webContent(hasElementWithId("form"))) | ||
.withElement(findElement(Locator.ID, "locale")) | ||
.check(webMatches(getText(), containsString("English"))); | ||
String[] codes = {"es", "en", "fr", "sw"}; | ||
for (String code : codes) { | ||
onWebView().withElement(findElement(Locator.NAME, code)) | ||
.check(webMatches(getText(), containsString(getLanguage(code)))); | ||
} | ||
|
||
//login form and errors | ||
onWebView().withElement(findElement(Locator.ID, "user")) | ||
.perform(clearElement()) | ||
.perform(DriverAtoms.webKeys("fakeName")) //to be created first | ||
.withElement(findElement(Locator.ID, "password")) | ||
.perform(clearElement()) | ||
.perform(DriverAtoms.webKeys("fake_password")) | ||
.withElement(findElement(Locator.ID, "login")) | ||
.perform(webClick()); | ||
Thread.sleep(4000);//TODO: use better ways to handle delays - takes longer with emulators | ||
onWebView().withElement(findElement(Locator.CSS_SELECTOR, "p.error.incorrect")) | ||
.check(webMatches(getText(), containsString(ERROR_INCORRECT))); | ||
} | ||
|
||
private String getLanguage(String code) { | ||
Locale aLocale = new Locale(code); | ||
return aLocale.getDisplayName(); | ||
} | ||
|
||
private static Matcher<View> childAtPosition( | ||
final Matcher<View> parentMatcher, final int position) { | ||
|
||
return new TypeSafeMatcher<View>() { | ||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("Child at position " + position + " in parent "); | ||
parentMatcher.describeTo(description); | ||
} | ||
|
||
@Override | ||
public boolean matchesSafely(View view) { | ||
ViewParent parent = view.getParent(); | ||
return parent instanceof ViewGroup && parentMatcher.matches(parent) | ||
&& view.equals(((ViewGroup) parent).getChildAt(position)); | ||
} | ||
}; | ||
} | ||
} |
150 changes: 150 additions & 0 deletions
150
...tUnbrandedWebviewDebug/java/org/medicmobile/webapp/mobile/SettingsDialogActivityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package org.medicmobile.webapp.mobile; | ||
|
||
|
||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.view.ViewParent; | ||
import androidx.test.espresso.DataInteraction; | ||
import androidx.test.espresso.ViewInteraction; | ||
import androidx.test.espresso.web.webdriver.DriverAtoms; | ||
import androidx.test.espresso.web.webdriver.Locator; | ||
import androidx.test.filters.LargeTest; | ||
import androidx.test.rule.ActivityTestRule; | ||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeMatcher; | ||
import org.junit.FixMethodOrder; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.MethodSorters; | ||
|
||
import java.util.Locale; | ||
|
||
import static androidx.test.espresso.Espresso.onData; | ||
import static androidx.test.espresso.Espresso.onView; | ||
import static androidx.test.espresso.Espresso.pressBack; | ||
import static androidx.test.espresso.action.ViewActions.click; | ||
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard; | ||
import static androidx.test.espresso.action.ViewActions.replaceText; | ||
import static androidx.test.espresso.assertion.ViewAssertions.matches; | ||
import static androidx.test.espresso.matcher.ViewMatchers.hasErrorText; | ||
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withHint; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withId; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withParent; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withText; | ||
import static androidx.test.espresso.web.assertion.WebViewAssertions.webContent; | ||
import static androidx.test.espresso.web.assertion.WebViewAssertions.webMatches; | ||
import static androidx.test.espresso.web.matcher.DomMatchers.hasElementWithId; | ||
import static androidx.test.espresso.web.sugar.Web.onWebView; | ||
import static androidx.test.espresso.web.webdriver.DriverAtoms.clearElement; | ||
import static androidx.test.espresso.web.webdriver.DriverAtoms.findElement; | ||
import static androidx.test.espresso.web.webdriver.DriverAtoms.getText; | ||
import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick; | ||
import static org.hamcrest.Matchers.allOf; | ||
import static org.hamcrest.Matchers.anything; | ||
import static org.hamcrest.Matchers.containsString; | ||
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner; | ||
|
||
|
||
@LargeTest | ||
@RunWith(AndroidJUnit4ClassRunner.class) | ||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
public class SettingsDialogActivityTest { | ||
|
||
private static final String WEBAPP_URL = "Webapp URL"; | ||
private static final String SERVER_ONE = "https://medic.github.io/atp"; | ||
private static final String SERVER_TWO = "https://gamma-cht.dev.medicmobile.org"; | ||
private static final String SERVER_THREE = "https://gamma.dev.medicmobile.org"; | ||
private static final String ERROR_INCORRECT = "Incorrect user name or password. Please try again."; | ||
|
||
@Rule | ||
public ActivityTestRule<SettingsDialogActivity> mActivityTestRule = new ActivityTestRule<>(SettingsDialogActivity.class); | ||
|
||
@Test | ||
public void serverSelectionScreenIsDisplayed() { | ||
onView(withText("Medic Mobile")).check(matches(isDisplayed())); | ||
onView(withText("Custom")).check(matches(isDisplayed())); | ||
onView(withId(R.id.lstServers)).check(matches(isDisplayed())); | ||
|
||
onView(withText(SERVER_ONE)).check(matches(isDisplayed())); | ||
onView(withText(SERVER_TWO)).check(matches(isDisplayed())); | ||
onView(withText(SERVER_THREE)).check(matches(isDisplayed())); | ||
|
||
onView(withText("Custom")).perform(click()); | ||
ViewInteraction textAppUrl = onView(withId(R.id.txtAppUrl)); | ||
textAppUrl.check(matches(withHint(WEBAPP_URL))); | ||
|
||
textAppUrl.perform(replaceText("something"), closeSoftKeyboard()); | ||
onView(withId(R.id.btnSaveSettings)).perform(click()); | ||
textAppUrl.check(matches(hasErrorText("must be a valid URL"))); | ||
pressBack(); | ||
|
||
} | ||
|
||
@Test | ||
public void testLoginScreen() throws Exception { | ||
DataInteraction linearLayout = onData(anything()) | ||
.inAdapterView(allOf(withId(R.id.lstServers), | ||
childAtPosition( | ||
withId(android.R.id.content), | ||
0))) | ||
.atPosition(2); | ||
linearLayout.perform(click()); | ||
Thread.sleep(10000);//TODO: use better ways to handle delays | ||
|
||
ViewInteraction webView = onView( | ||
allOf(withId(R.id.wbvMain), | ||
withParent(allOf(withId(R.id.lytWebView), | ||
withParent(withId(android.R.id.content)))), | ||
isDisplayed())); | ||
webView.check(matches(isDisplayed())); | ||
onWebView() | ||
.check(webContent(hasElementWithId("form"))) | ||
.withElement(findElement(Locator.ID, "locale")) | ||
.check(webMatches(getText(), containsString("English"))); | ||
String[] codes = {"es", "en", "fr", "sw"}; | ||
for (String code : codes) { | ||
onWebView().withElement(findElement(Locator.NAME, code)) | ||
.check(webMatches(getText(), containsString(getLanguage(code)))); | ||
} | ||
|
||
//login form and errors | ||
onWebView().withElement(findElement(Locator.ID, "user")) | ||
.perform(clearElement()) | ||
.perform(DriverAtoms.webKeys("fakename")) //to be created first | ||
.withElement(findElement(Locator.ID, "password")) | ||
.perform(clearElement()) | ||
.perform(DriverAtoms.webKeys("fake_password")) | ||
.withElement(findElement(Locator.ID, "login")) | ||
.perform(webClick()); | ||
Thread.sleep(4000);//TODO: use better ways to handle delays - takes longer with emulators | ||
onWebView().withElement(findElement(Locator.CSS_SELECTOR, "p.error.incorrect")) | ||
.check(webMatches(getText(), containsString(ERROR_INCORRECT))); | ||
} | ||
|
||
private String getLanguage(String code) { | ||
Locale aLocale = new Locale(code); | ||
return aLocale.getDisplayName(); | ||
} | ||
|
||
private static Matcher<View> childAtPosition( | ||
final Matcher<View> parentMatcher, final int position) { | ||
|
||
return new TypeSafeMatcher<View>() { | ||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("Child at position " + position + " in parent "); | ||
parentMatcher.describeTo(description); | ||
} | ||
|
||
@Override | ||
public boolean matchesSafely(View view) { | ||
ViewParent parent = view.getParent(); | ||
return parent instanceof ViewGroup && parentMatcher.matches(parent) | ||
&& view.equals(((ViewGroup) parent).getChildAt(position)); | ||
} | ||
}; | ||
} | ||
} |