@Test public void testShowAlertDialog() throws Throwable { // create a signal to let us know when our task is done. final CountDownLatch signal = new CountDownLatch(1); waitWebViewLoad(MainFragment.DEFAULT_URL); new Thread(new Runnable() { @Override public void run() { try { // Wait second signal.await(1, TimeUnit.SECONDS); // Find ok button and click assertViewWithTextIsVisible(device, rule.getActivity().getString(android.R.string.ok)); } catch (Exception e) { e.printStackTrace(); } } }).start(); // WebView show alert dialog onWebView().withElement(findElement(Locator.ID, "showAlertBtn")) .perform(webClick()); }
@Test public void testWebToAndroidScriptCall() throws Exception { String go = MainFragment.DEFAULT_URL; onView(withId(R.id.et_url)).perform(clearText()); onView(withId(R.id.et_url)).perform(typeText(go)).perform(ViewActions.pressImeActionButton()); waitWebViewLoad(); onWebView() // Find the search keyword element by ID .withElement(findElement(Locator.ID, "search_keyword")) // Clear previous input .perform(clearElement()) // Enter text into the input element .perform(DriverAtoms.webKeys(ANDROID_SCRIPT_CALL)) // Value check. script getValue 'search_keyword' .check(webMatches(script("return document.getElementById('search_keyword').value", castOrDie(String.class)), containsString(ANDROID_SCRIPT_CALL))) // Find the submit button .withElement(findElement(Locator.ID, "updateKeywordBtn")) // Simulate a click via javascript .perform(webClick()); onView(withId(R.id.et_keyword)).check(matches(withText(ANDROID_SCRIPT_CALL))); }
@Test public void testWebViewUpdateElementByDisplayed() throws Exception { String go = MainFragment.DEFAULT_URL; onView(withId(R.id.et_url)).perform(clearText()); onView(withId(R.id.et_url)).perform(typeText(go)).perform(ViewActions.pressImeActionButton()); waitWebViewLoad(); onView(withId(R.id.et_keyword)).perform(clearText()).perform(typeText(JAVASCRIPT_CALL)); onView(withId(R.id.btn_search)).perform(click()); onWebView() // Find the message element by ID .withElement(findElement(Locator.ID, "message")) // Verify that the text is displayed .check(webMatches(getText(), containsString(JAVASCRIPT_CALL))); }
@Test public void testChangeWebViewTest() throws Exception { waitWebViewLoad(); onWebView() // Find the search keyword element by ID .withElement(findElement(Locator.ID, "url")) // Clear previous input .perform(clearElement()) // Enter text into the input element .perform(DriverAtoms.webKeys("http://google.com/")) // Find the submit button .withElement(findElement(Locator.ID, "changeWebView")) // Simulate a click via javascript .perform(webClick()); onWebView() .withNoTimeout() .check(webMatches(getCurrentUrl(), containsString("https://www.google"))); }
private void longPressLink(String id, String label, String path) { onWebView() .withElement(findElement(Locator.ID, id)) .check(webMatches(getText(), equalTo(label))); simulateLinkLongPress(path); }
@Test public void pullDownToRefreshTest() throws InterruptedException, UiObjectNotFoundException, IOException { onView(withId(R.id.urlView)) .check(matches(isDisplayed())) .check(matches(hasFocus())) .perform(click(), replaceText(webServer.url("/").toString()), pressImeActionButton()); onView(withId(R.id.display_url)) .check(matches(isDisplayed())) .check(matches(withText(containsString(webServer.getHostName())))); onWebView() .withElement(findElement(Locator.ID, COUNTER)) .check(webMatches(getText(), containsString(FIRST_TIME))); onView(withId(R.id.swipe_refresh)) .check(matches(isDisplayed())); // Swipe down to refresh, spinner is shown (2nd child) and progress bar is shown onView(withId(R.id.swipe_refresh)) .perform(withCustomConstraints(swipeDown(), isDisplayingAtLeast(85))) .check(matches(hasChildCount(2))); onWebView() .withElement(findElement(Locator.ID, COUNTER)) .check(webMatches(getText(), containsString(SECOND_TIME))); }
@Test public void takeScreenshotsOfErrorPages() throws Exception { for (ErrorTypes error: ErrorTypes.values()) { onView(withId(R.id.urlView)) .check(matches(isDisplayed())) .check(matches(hasFocus())) .perform(click(), replaceText("error:" + error.value), pressImeActionButton()); assertTrue(TestHelper.webView.waitForExists(waitingTime)); assertTrue(TestHelper.progressBar.waitUntilGone(waitingTime)); // Android O has an issue with using Locator.ID if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { UiObject tryAgainBtn = device.findObject(new UiSelector() .descriptionContains(getString(R.string.errorpage_refresh)) .clickable(true)); assertTrue(tryAgainBtn.waitForExists(waitingTime)); } else { onWebView() .withElement(findElement(Locator.ID, "errorTitle")) .perform(webClick()); onWebView() .withElement(findElement(Locator.ID, "errorTryAgain")) .perform(webScrollIntoView()); } Screengrab.screenshot(error.name()); onView(withId(R.id.display_url)) .check(matches(isDisplayed())) .perform(click()); } }
@Test public void testCustomTabUI() throws Exception { try { startWebServer(); // Launch activity with custom tabs intent activityTestRule.launchActivity(createCustomTabIntent()); // Wait for website to load onWebView() .withElement(findElement(Locator.ID, TEST_PAGE_HEADER_ID)) .check(webMatches(getText(), equalTo(TEST_PAGE_HEADER_TEXT))); // Verify action button is visible onView(withContentDescription(ACTION_BUTTON_DESCRIPTION)) .check(matches(isDisplayed())); // Open menu onView(withId(R.id.menuView)) .perform(click()); // Verify share action is visible onView(withId(R.id.share)) .check(matches(isDisplayed())); // Verify custom menu item is visible onView(withText(MENU_ITEM_LABEL)) .check(matches(isDisplayed())); // Close the menu again Espresso.pressBack(); // Verify close button is visible - Click it to close custom tab. onView(withId(R.id.customtab_close)) .check(matches(isDisplayed())) .perform(click()); } finally { stopWebServer(); } }
@Test public void testCallWebJavascript() throws Throwable { waitWebViewLoad(); onWebView() // Find the search keyword element by ID .withElement(findElement(Locator.ID, "search_keyword")) // Clear previous input .perform(clearElement()) // Enter text into the input element .perform(DriverAtoms.webKeys(ANDROID_SCRIPT_CALL)) .perform(script("updateKeyword();")); onView(withId(R.id.et_keyword)).check(matches(withText(ANDROID_SCRIPT_CALL))); }
@Test public void contentLicenses_whenClicked_thenWebViewWithContentLicensesIsLaunched() { // When onView(withChildText(R.id.guidedactions_list, R.string.ondemand_settings_about_content)) .perform(pressKey(KeyEvent.KEYCODE_ENTER)); // Then enableJavaScriptInWebViewToSupportEspresso(); onWebView().withElement(findElement(Locator.XPATH, "/html/body/h1")) .check(webMatches(getText(), is(getString(R.string.ondemand_settings_about_content)))); }
@Test public void thirdPartyLicenses_whenClicked_thenWebViewWithThirdPartyLicensesIsLaunched() { // When onView(withChildText(R.id.guidedactions_list, R.string.ondemand_settings_about_thirdparty)) .perform(pressKey(KeyEvent.KEYCODE_DPAD_DOWN), pressKey(KeyEvent.KEYCODE_ENTER)); // Then enableJavaScriptInWebViewToSupportEspresso(); onWebView().withElement(findElement(Locator.XPATH, "/html/body/h1")) .check(webMatches(getText(), is(getString(R.string.ondemand_settings_about_thirdparty)))); }
/** * 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))); }
@Test public void testVisitingMultipleSites() { { // Load website: Erase button visible, Tabs button not navigateToMockWebServer(webServer, "tab1.html"); checkTabIsLoaded("Tab 1"); onFloatingEraseButton() .check(matches(isDisplayed())); onFloatingTabsButton() .check(matches(not(isDisplayed()))); } { // Open link in new tab: Erase button hidden, Tabs button visible longPressLink("tab2", "Tab 2", "tab2.html"); openInNewTab(); onFloatingEraseButton() .check(matches(not(isDisplayed()))); onFloatingTabsButton() .check(matches(isDisplayed())) .check(matches(withContentDescription(is("Tabs open: 2")))); } { // Open link in new tab: Tabs button updated, Erase button still hidden longPressLink("tab3", "Tab 3", "tab3.html"); openInNewTab(); onFloatingEraseButton() .check(matches(not(isDisplayed()))); onFloatingTabsButton() .check(matches(isDisplayed())) .check(matches(withContentDescription(is("Tabs open: 3")))); } { // Open tabs tray and switch to second tab. onFloatingTabsButton() .perform(click()); final String expectedUrl = webServer.getHostName() + "/tab2.html"; onView(withText(expectedUrl)) .perform(click()); onWebView() .withElement(findElement(Locator.ID, "content")) .check(webMatches(getText(), equalTo("Tab 2"))); } { // Remove all tabs via the menu onFloatingTabsButton() .perform(click()); onView(withText(R.string.tabs_tray_action_erase)) .perform(click()); assertFalse(SessionManager.getInstance().hasSession()); } SystemClock.sleep(5000); }
private void checkTabIsLoaded(String title) { onWebView() .withElement(findElement(Locator.ID, "content")) .check(webMatches(getText(), equalTo(title))); }
@Test public void MismatchTest() throws InterruptedException, UiObjectNotFoundException { // Type "mozilla" into the URL bar. onView(withId(R.id.urlView)) .check(matches(isDisplayed())) .check(matches(hasFocus())) .perform(click(), replaceText("mozilla")); // Verify that the search hint is displayed and click it. onView(withId(R.id.searchView)) .check(matches(isDisplayed())) .check(matches(withText("Search for mozilla"))) .check(matches(isClickable())) .perform(click()); // A WebView is displayed onView(withId(R.id.webview)) .check(matches(isDisplayed())); // The displayed URL contains mozilla. Click on it to edit it again. onView(withId(R.id.display_url)) .check(matches(isDisplayed())) .check(matches(withText(containsString("mozilla")))) .perform(click()); // Type "moz" - Verify that it auto-completes to "mozilla.org" and then load the website onView(withId(R.id.urlView)) .perform(click(), replaceText("mozilla")) .check(matches(withText("mozilla.org"))) .perform(pressImeActionButton()); // Check we loaded the mozilla.org website onWebView() .withElement(findElement(Locator.CSS_SELECTOR, MOZILLA_WEBSITE_SLOGAN_SELECTOR)) .check(webMatches(getText(), containsString(MOZILLA_WEBSITE_SLOGAN_TEXT))); // The displayed URL contains www.mozilla.org onView(withId(R.id.display_url)) .check(matches(isDisplayed())) .check(matches(withText(containsString("www.mozilla.org")))); }
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)); }