Search in sources :

Example 1 with WrapsDriver

use of org.openqa.selenium.WrapsDriver in project acceptance-test-harness by jenkinsci.

the class Scroller method scrollIntoView.

/**
 * The framework is expected to take care of the correct scrolling. When you are tempted to scroll from PageObjects
 * or tests, there is likely a framework problem to be fixed.
 */
public void scrollIntoView(WebElement e, WebDriver driver) {
    if (driver instanceof HtmlUnitDriver || (driver instanceof WrapsDriver && ((WrapsDriver) driver).getWrappedDriver() instanceof HtmlUnitDriver)) {
        return;
    }
    WebElement element = e;
    if (Objects.equals(element.getTagName(), "option")) {
        // scroll select into view not option
        element = e.findElement(By.xpath(".."));
    }
    final int eYCoord = element.getLocation().getY();
    final int eXCoord = element.getLocation().getX();
    final String id = element.getAttribute("id");
    final JavascriptExecutor executor = (JavascriptExecutor) driver;
    // Wait until web element is successfully scrolled.
    try {
        new Wait<>(Boolean.TRUE).withTimeout(5, // Wall-clock time
        TimeUnit.SECONDS).until(() -> (Boolean) executor.executeScript(scrollJs, eYCoord, eXCoord, id));
    } catch (TimeoutException ex) {
        // Scrolling failed, but sometimes the element to click is already visible, let the test continue and eventually fail later
        // This log message should be sufficient to diagnose the issue
        LOGGER.log(Level.WARNING, "Scrolling failed, letting the test to continue anyways, but \"Element is not clickable\" error will likely be thrown", ex);
    }
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) WebElement(org.openqa.selenium.WebElement) Wait(org.jenkinsci.test.acceptance.junit.Wait) WrapsDriver(org.openqa.selenium.WrapsDriver) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) TimeoutException(org.openqa.selenium.TimeoutException)

Aggregations

Wait (org.jenkinsci.test.acceptance.junit.Wait)1 JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)1 TimeoutException (org.openqa.selenium.TimeoutException)1 WebElement (org.openqa.selenium.WebElement)1 WrapsDriver (org.openqa.selenium.WrapsDriver)1 HtmlUnitDriver (org.openqa.selenium.htmlunit.HtmlUnitDriver)1