public ByText(String elementText) { super(".//*/text()[" + NORMALIZE_SPACE_XPATH + " = " + Quotes.escape(elementText) + "]/parent::*"); this.elementText = elementText; }
/** * Repeatedly applies this instance's input value to the given function until one of the following occurs: * the function returns neither null nor false, * the function throws an unignored exception, * the timeout expires * * Useful in situations where FluentAdapter#await is too specific * (for example to check against page source) * * @param wait generic {@code FluentWait<WebDriver>} instance * @param f function to execute */ public <T>T waitUntil(FluentWait<WebDriver> wait, Function<WebDriver, T> f) { return wait.until(f); }
Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down"))); select.deselectAll(); select.selectByVisibleText("Value1");
// 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();
private void selectOptionsByTexts(WebElementSource selectField, String[] texts) { Select select = new Select(selectField.getWebElement()); for (String text : texts) { try { select.selectByVisibleText(text); } catch (NoSuchElementException e) { throw new ElementNotFound(selectField.driver(), selectField.getSearchCriteria() + "/option[text:" + text + ']', exist, e); } } }
@Override public List<WebElement> getElements() { return new Select(selectElement.getWebElement()).getAllSelectedOptions(); }
@Override public SelenideElement execute(SelenideElement proxy, WebElementSource selectElement, Object[] args) { return wrap(selectElement.driver(), new Select(selectElement.getWebElement()).getFirstSelectedOption()); } }
private void selectOptionsByIndexes(WebElementSource selectField, int[] indexes) { Select select = new Select(selectField.getWebElement()); for (int index : indexes) { try { select.selectByIndex(index); } catch (NoSuchElementException e) { throw new ElementNotFound(selectField.driver(), selectField.getSearchCriteria() + "/option[index:" + index + ']', exist, e); } } } }
/** * Creates a generic {@code FluentWait<WebDriver>} instance * using the underlying web driver. * * @return the webdriver contained in a fluent wait. */ public FluentWait<WebDriver> fluentWait() { return new FluentWait<WebDriver>(super.getDriver()); }
private void selectOptionByValue(WebElementSource selectField, Select select, String value) { try { select.selectByValue(value); } catch (NoSuchElementException e) { throw new ElementNotFound(selectField.driver(), selectField.getSearchCriteria() + "/option[value:" + value + ']', exist, e); } } }
/** * Repeatedly applies this instance's input value to the given function until one of the following occurs: * the function returns neither null nor false, * the function throws an unignored exception, * the default timeout expires * * useful in situations where FluentAdapter#await is too specific * (for example to check against page source or title) * * @param f function to execute */ public <T>T waitUntil(Function<WebDriver, T> f) { FluentWait<WebDriver> wait = fluentWait().withTimeout(3000, TimeUnit.MILLISECONDS); return waitUntil(wait,f); }
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until( ExpectedConditions.visibilityOfElementLocated(By.id("someid")));
private String getSelectedOptionsTexts(WebElement element) { List<WebElement> selectedOptions = new Select(element).getAllSelectedOptions(); StringBuilder sb = new StringBuilder(); for (WebElement selectedOption : selectedOptions) { sb.append(selectedOption.getText()); } return sb.toString(); }
/** * Repeatedly applies this instance's input value to the given function until one of the following occurs: * the function returns neither null nor false, * the function throws an unignored exception, * the timeout expires * * Useful in situations where FluentAdapter#await is too specific * (for example to check against page source) * * @param <T> the return type * @param wait generic {@code FluentWait<WebDriver>} instance * @param f function to execute * @return the return value */ public <T>T waitUntil(FluentWait<WebDriver> wait, Function<WebDriver, T> f) { return wait.until(f); }
/** * Creates a generic {@code FluentWait<WebDriver>} instance * using the underlying web driver */ public FluentWait<WebDriver> fluentWait() { return new FluentWait<WebDriver>(super.getDriver()); }
public WithText(String elementText) { super(".//*/text()[contains(" + NORMALIZE_SPACE_XPATH + ", " + Quotes.escape(elementText) + ")]/parent::*"); this.elementText = elementText; }
WebDriverWait wait = new WebDriverWait(driver, 10); Alert alert = wait.until(ExpectedConditions.alertIsPresent()); alert.authenticateUsing(new UserAndPassword(**username**, **password**));
/** * Repeatedly applies this instance's input value to the given function until one of the following occurs: * the function returns neither null nor false, * the function throws an unignored exception, * the timeout expires * * Useful in situations where FluentAdapter#await is too specific * (for example to check against page source) * * @param <T> the return type * @param wait generic {@code FluentWait<WebDriver>} instance * @param f function to execute * @return the return value */ public <T>T waitUntil(FluentWait<WebDriver> wait, Function<WebDriver, T> f) { return wait.until(f); }
/** * Creates a generic {@code FluentWait<WebDriver>} instance * using the underlying web driver. * * @return the webdriver contained in a fluent wait. */ public FluentWait<WebDriver> fluentWait() { return new FluentWait<WebDriver>(super.getDriver()); }
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 } }