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); } }
public static void assertTableRowCount(final String[][] expectedData, final DataTableComponent dataTable, final WebDriver webDriver, final long timeout) { try { new WebDriverWait(webDriver, timeout).until(new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver input) { return expectedData.length == dataTable.getRowCount(); } }); } catch (TimeoutException e) { fail(String.format("Wrong row count for the table. Expected: %d, Actual: %d", expectedData.length, dataTable.getRowCount())); } }
public <E> E waitFor(ExpectedCondition<E> expectedCondition) { WebDriverWait wait = new WebDriverWait(browser.driver, TestProperties.TEST_TIMEOUT); return wait.until(expectedCondition); }
/** * 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()); } }
/** * Returns if a {@value CHANGE_EVENT} event has not been fired for the element to which the hook is associated. * Note that this only detects the presence of firing of {@value CHANGE_EVENT} events and not does not keep track of * how many {@value CHANGE_EVENT} events are fired. */ private boolean isChangeEventNotFired() { WebDriverWait wait = new WebDriverWait(browser.driver, MAXIMUM_SECONDS_REQUIRED_FOR_ALL_CPUS_TO_FIRE_EVENT); try { wait.until(ExpectedConditions.attributeContains(By.tagName("body"), HOOK_ATTRIBUTE, "true")); return false; } catch (TimeoutException e) { return true; } }
@Test public void setValueTest() { WebDriverWait wait = new WebDriverWait(driver, 20); IOSElement slider = wait.until(driver1 -> driver1.findElement(By.className("XCUIElementTypeSlider"))); slider.setValue("0%"); assertEquals("0%", slider.getAttribute("value")); } }
@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 nativeWebTapSettingTest() { driver.get("https://saucelabs.com/test/guinea-pig"); // do a click with nativeWebTap turned on, and assert we get to the right page driver.nativeWebTap(true); WebElement el = driver.findElementById("i am a link"); el.click(); assertTrue(new WebDriverWait(driver, 30) .until(ExpectedConditions.titleIs("I am another page title - Sauce Labs"))); driver.navigate().back(); // now do a click with it turned off and assert the same behavior assertTrue(new WebDriverWait(driver, 30) .until(ExpectedConditions.titleIs("I am a page title - Sauce Labs"))); driver.nativeWebTap(false); el = driver.findElementById("i am a link"); el.click(); assertTrue(new WebDriverWait(driver, 30) .until(ExpectedConditions.titleIs("I am another page title - Sauce Labs"))); } }
@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 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 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")); }
@Test public void multiTouchTest() { MobileElement e = driver.findElementByAccessibilityId("ComputeSumButton"); MobileElement e2 = driver.findElementByAccessibilityId("show alert"); TouchAction tap1 = new TouchAction(driver).tap(tapOptions().withElement(element(e))); TouchAction tap2 = new TouchAction(driver).tap(tapOptions().withElement(element(e2))); new MultiTouchAction(driver).add(tap1).add(tap2).perform(); WebDriverWait waiting = new WebDriverWait(driver, 10000); assertNotNull(waiting.until(alertIsPresent())); driver.switchTo().alert().accept(); }