下面列出了怎么用android.support.test.espresso.web.webdriver.Locator的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void typeTextInInput_clickButton_SubmitsForm() {
// Lazily launch the Activity with a custom start Intent per test
mActivityRule.launchActivity(withWebFormIntent());
// Selects the WebView in your layout. If you have multiple WebViews you can also use a
// matcher to select a given WebView, onWebView(withId(R.id.web_view)).
onWebView()
// Find the input element by ID
.withElement(findElement(Locator.ID, "text_input"))
// Clear previous input
.perform(clearElement())
// Enter text into the input element
.perform(DriverAtoms.webKeys(MACCHIATO))
// Find the submit button
.withElement(findElement(Locator.ID, "submitBtn"))
// Simulate a click via javascript
.perform(webClick())
// Find the response element by ID
.withElement(findElement(Locator.ID, "response"))
// Verify that the response page contains the entered text
.check(webMatches(getText(), containsString(MACCHIATO)));
}
@Test
public void typeTextInInput_clickButton_ChangesText() {
// Lazily launch the Activity with a custom start Intent per test
mActivityRule.launchActivity(withWebFormIntent());
// Selects the WebView in your layout. If you have multiple WebViews you can also use a
// matcher to select a given WebView, onWebView(withId(R.id.web_view)).
onWebView()
// Find the input element by ID
.withElement(findElement(Locator.ID, "text_input"))
// Clear previous input
.perform(clearElement())
// Enter text into the input element
.perform(DriverAtoms.webKeys(DOPPIO))
// Find the change text button.
.withElement(findElement(Locator.ID, "changeTextBtn"))
// Click on it.
.perform(webClick())
// Find the message element by ID
.withElement(findElement(Locator.ID, "message"))
// Verify that the text is displayed
.check(webMatches(getText(), containsString(DOPPIO)));
}
/**
* Enters a full name and then asserts if it's displayed
*
* @param firstName the entered first name
* @param lastName the entered last name
*/
private void assertName(String firstName, String lastName) {
typeIntoWebField(FIRST_NAME_FORM_ID, firstName);
typeIntoWebField(LAST_NAME_FORM_ID, lastName);
onWebView().withElement(findElement(Locator.ID, FULL_NAME_DISPLAY_ID)).
check(webMatches(getText(), containsString(firstName + " " + lastName)));
}
public static void AzureADSignIn(String username, String password, ActivityTestRule<SignInActivity> signInActivityTestRule) throws InterruptedException {
SignInActivity signInActivity = signInActivityTestRule.launchActivity(null);
onView(withId(R.id.o365_signin)).perform(click());
try {
onWebView()
.withElement(findElement(Locator.ID, USER_ID_TEXT_ELEMENT))
.perform(clearElement())
// Enter text into the input element
.perform(DriverAtoms.webKeys(username))
// Set focus on the username input text
// The form validates the username when this field loses focus
.perform(webClick())
.withElement(findElement(Locator.ID, PASSWORD_TEXT_ELEMENT))
// Now we force focus on this element to make
// the username element to lose focus and validate
.perform(webClick())
.perform(clearElement())
// Enter text into the input element
.perform(DriverAtoms.webKeys(password));
Thread.sleep(2000, 0);
onWebView()
.withElement(findElement(Locator.ID, SIGN_IN_BUTTON_ELEMENT))
.perform(webClick());
} catch (NoMatchingViewException ex) {
// If user is already logged in, the flow will go directly to SnippetListActivity
} finally {
Thread.sleep(2000, 0);
}
// Finally, verify that SnippetListActivity is on top
intended(allOf(
hasComponent(hasShortClassName(".SnippetListActivity")),
toPackage("com.microsoft.graph.snippets")
));
signInActivity.finish();
}
/**
* Types a given string into a id
*
* @param id the element id
* @param text the text input
*/
private void typeIntoWebField(String id, String text){
onWebView().withElement(findElement(Locator.ID, id)).perform(clearElement()).perform(webKeys(text));
}