// times out after 5 seconds WebDriverWait wait = new WebDriverWait(driver, 5); // while the following loop runs, the DOM changes - // page is refreshed, or element is removed and re-added wait.until(presenceOfElementLocated(By.id("container-element"))); // now we're good - let's click the element driver.findElement(By.id("foo")).click();
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until( ExpectedConditions.visibilityOfElementLocated(By.id("someid")));
WebDriverWait wait = new WebDriverWait(driver, 10); Alert alert = wait.until(ExpectedConditions.alertIsPresent()); alert.authenticateUsing(new UserAndPassword(**username**, **password**));
public void checkAlert() { try { WebDriverWait wait = new WebDriverWait(driver, 2); wait.until(ExpectedConditions.alertIsPresent()); Alert alert = driver.switchTo().alert(); alert.accept(); } catch (Exception e) { //exception handling } }
private WebElement waitForElementById(final String id, int timeoutInSeconds) { WebElement element = (new WebDriverWait(driver, timeoutInSeconds, 1000)) .until((ExpectedCondition<WebElement>) d -> d.findElement(By.id(id))); assertNotNull(element); return element; } }
private void login() { WebElement userNameElement = waitForElementById("user_name", 60); WebElement passwordElement = waitForElementById("user_password"); userNameElement.sendKeys(username); passwordElement.sendKeys(password); passwordElement.submit(); driver.get(getPulseURL() + "clusterDetail.html"); WebElement userNameOnPulsePage = (new WebDriverWait(driver, 30, 1000)).until( (ExpectedCondition<WebElement>) d -> d.findElement(By.id("userName"))); assertNotNull(userNameOnPulsePage); }
protected void doSimpleWebdriverTest(BrowserWebDriverContainer rule) { RemoteWebDriver driver = setupDriverFromRule(rule); System.out.println("Selenium remote URL is: " + rule.getSeleniumAddress()); System.out.println("VNC URL is: " + rule.getVncAddress()); driver.get("http://www.google.com"); WebElement search = driver.findElement(By.name("q")); search.sendKeys("testcontainers"); search.submit(); List<WebElement> results = new WebDriverWait(driver, 15) .until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("#search h3"))); assertTrue("the word 'testcontainers' appears in search results", results.stream() .anyMatch(el -> el.getText().contains("testcontainers"))); }
@Override public void waitForElementVisible(final WebElement element, long timeout) { new WebDriverWait(webDriver, timeout).until(new Predicate<WebDriver>() { @Override public boolean apply(WebDriver input) { return isElementPresent(element); } }); }
@Override public void waitForElementInvisible(final WebElement element) { new WebDriverWait(webDriver, webDriverTimeout).until(new Predicate<WebDriver>() { @Override public boolean apply(WebDriver input) { return !isElementPresent(element); } }); }
public static void assertImagePreviewIsDisplayed(String viewName, final WebElement webElement, final ExtendedWebDriver webDriver, final long timeout) { try { new WebDriverWait(webDriver, timeout).until(new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver input) { return webDriver.isElementPresent(webElement.findElement(By.xpath("//img[@name='picture']"))); } }); } catch (TimeoutException e) { fail("Image preview is not displayed on " + viewName); } }
/** * Waits for the page to load. This includes all AJAX requests and Angular animations in the page. */ public void waitForPageLoad() { WebDriverWait wait = new WebDriverWait(driver, TestProperties.TEST_TIMEOUT); ExpectedCondition<Boolean> expectation = driver -> "complete".equals(((JavascriptExecutor) driver).executeAsyncScript(PAGE_LOAD_SCRIPT).toString()); wait.until(expectation); }
@Test public void hideKeyboardWithParametersTest() { new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(By.id("IntegerA"))) .click(); driver.hideKeyboard(HideKeyboardStrategy.PRESS_KEY, "Done"); }
@Test public void openNotification() { driver.closeApp(); driver.openNotifications(); WebDriverWait wait = new WebDriverWait(driver, 20); assertNotEquals(0, wait.until(input -> { List<AndroidElement> result = input .findElements(id("com.android.systemui:id/settings_button")); return result.isEmpty() ? null : result; }).size()); } }
@Test public void webViewPageTestCase() throws InterruptedException { new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(By.id("login"))) .click(); driver.findElementByAccessibilityId("webView").click(); new WebDriverWait(driver, 30) .until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("Webview"))); findAndSwitchToWebView(); WebElement el = driver.findElementByPartialLinkText("login"); assertTrue(el.isDisplayed()); } }
@Test public void dismissAlertTest() { Supplier<Boolean> dismissAlert = () -> { driver.findElement(MobileBy.AccessibilityId(iOSAutomationText)).click(); waiting.until(alertIsPresent()); driver.switchTo().alert().dismiss(); return true; }; assertTrue(dismissAlert.get()); }
@Test public void getAlertTextTest() { driver.findElement(MobileBy.AccessibilityId(iOSAutomationText)).click(); waiting.until(alertIsPresent()); assertFalse(StringUtils.isBlank(driver.switchTo().alert().getText())); } }
@Test public void testLandscapeLeftRotation() { new WebDriverWait(driver, 20).until(ExpectedConditions .elementToBeClickable(driver.findElementById("android:id/content") .findElement(MobileBy.AccessibilityId("Graphics")))); DeviceRotation landscapeLeftRotation = new DeviceRotation(0, 0, 270); driver.rotate(landscapeLeftRotation); assertEquals(driver.rotation(), landscapeLeftRotation); }
@Test public void testPortraitUpsideDown() { new WebDriverWait(driver, 20).until(ExpectedConditions .elementToBeClickable(driver.findElementById("android:id/content") .findElement(MobileBy.AccessibilityId("Graphics")))); DeviceRotation landscapeRightRotation = new DeviceRotation(0, 0, 180); driver.rotate(landscapeRightRotation); assertEquals(driver.rotation(), landscapeRightRotation); }
@Test public void testLandscapeRightRotation() { new WebDriverWait(driver, 20).until(ExpectedConditions .elementToBeClickable(driver.findElementById("android:id/content") .findElement(MobileBy.AccessibilityId("Graphics")))); DeviceRotation landscapeRightRotation = new DeviceRotation(0, 0, 90); driver.rotate(landscapeRightRotation); assertEquals(driver.rotation(), landscapeRightRotation); }
@Test public void swipeTest() { WebDriverWait webDriverWait = new WebDriverWait(driver, 30); IOSElement slider = webDriverWait.until(driver1 -> driver.findElementByClassName("XCUIElementTypeSlider")); Dimension size = slider.getSize(); ElementOption press = element(slider, size.width / 2 + 2, size.height / 2); ElementOption move = element(slider, 1, size.height / 2); TouchAction swipe = new TouchAction(driver).press(press) .waitAction(waitOptions(ofSeconds(2))) .moveTo(move).release(); swipe.perform(); assertEquals("0%", slider.getAttribute("value")); }