Example usage for org.openqa.selenium JavascriptExecutor executeScript

List of usage examples for org.openqa.selenium JavascriptExecutor executeScript

Introduction

In this page you can find the example usage for org.openqa.selenium JavascriptExecutor executeScript.

Prototype

Object executeScript(String script, Object... args);

Source Link

Document

Executes JavaScript in the context of the currently selected frame or window.

Usage

From source file:com.liferay.cucumber.selenium.WebDriverHelper.java

License:Open Source License

protected static boolean isObscured(WebDriver webDriver, WebElement webElement) {

    WrapsDriver wrapsDriver = (WrapsDriver) webElement;

    JavascriptExecutor javascriptExecutor = (JavascriptExecutor) wrapsDriver.getWrappedDriver();

    StringBuilder sb = new StringBuilder();

    sb.append("var element = arguments[0];");
    sb.append("console.log(element);");
    sb.append("var rect = element.getBoundingClientRect();");
    sb.append("elementX = (rect.right + rect.left) / 2;");
    sb.append("elementY = (rect.top + rect.bottom) / 2;");
    sb.append("var newElement = ");
    sb.append("document.elementFromPoint(elementX, elementY);");
    sb.append("if (element == newElement) {");
    sb.append("return false;}");
    sb.append("return true;");

    Boolean obscured = (Boolean) javascriptExecutor.executeScript(sb.toString(), webElement);

    return obscured.booleanValue();
}

From source file:com.liferay.cucumber.selenium.WebDriverHelper.java

License:Open Source License

protected static void scrollWebElementIntoView(WebDriver webDriver, WebElement webElement) {

    if (!webElement.isDisplayed() || isObscured(webDriver, webElement)) {
        WrapsDriver wrapsDriver = (WrapsDriver) webElement;

        WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver();

        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) wrappedWebDriver;

        javascriptExecutor.executeScript("arguments[0].scrollIntoView(false);", webElement);
    }/*ww w.  jav a2s. co m*/
}

From source file:com.liferay.faces.test.selenium.browser.internal.BrowserDriverImpl.java

License:Open Source License

@Override
public Object executeScriptInCurrentWindow(String script, Object... args) {

    WebDriver webDriver = getWebDriver();

    if (webDriver instanceof JavascriptExecutor) {

        JavascriptExecutor javaScriptExecutor = (JavascriptExecutor) webDriver;

        return javaScriptExecutor.executeScript(script, args);
    } else {/*from   w w  w.ja va2 s .  co m*/
        throw new UnsupportedOperationException(
                "Executing JavaScript is not supported by: " + getBrowserName());
    }
}

From source file:com.mycompany.browserManipulationUtilities.BrowserScrollUtility.java

/**
 * //ww  w.j a  v  a 2s . c o  m
 * @param browser
 * @param pixelsToScrollHorizontally <- negative (left), (right) positive ->
 * @param pixelsToScrollVertically ^ negative (up), (down) positive v
 */
public static void executeScroll(WebDriver browser, int pixelsToScrollHorizontally,
        int pixelsToScrollVertically) {
    JavascriptExecutor jseBrowser = (JavascriptExecutor) browser;
    jseBrowser.executeScript(
            "window.scrollBy(" + pixelsToScrollHorizontally + ", " + pixelsToScrollVertically + ")", "");
}

From source file:com.mycompany.browserManipulationUtilities.ScreenShotUtility.java

protected static Point getViewPortTopLeftCorner(WebDriver browser) {
    JavascriptExecutor jseBrowser = (JavascriptExecutor) browser;
    Integer xOffset = ((Long) jseBrowser.executeScript("return window.pageXOffset", "")).intValue();
    Integer yOffset = ((Long) jseBrowser.executeScript("return window.pageYOffset", "")).intValue();
    Point topLeft = new Point(xOffset, yOffset);
    return topLeft;
}

From source file:com.mycompany.browserManipulationUtilities.ScreenShotUtility.java

/**
 * Gets the view port dimensions, excluding scrollbar
 * @param browser// w  ww.j  a va2 s .  co  m
 * @return 
 */
protected static Dimension getViewPortDimension(WebDriver browser) {
    JavascriptExecutor jseBrowser = (JavascriptExecutor) browser;
    Integer width = ((Long) jseBrowser.executeScript("return document.documentElement.clientWidth", ""))
            .intValue();
    Integer height = ((Long) jseBrowser.executeScript("return window.innerHeight", "")).intValue();
    Dimension viewPortDimension = new Dimension(width, height);
    return viewPortDimension;
}

From source file:com.mycompany.newseleniumtest.coba.java

public void latihan(String browser) throws InterruptedException {
    //Instantiate the webdriver object:
    WebDriver driver = new FirefoxDriver();
    JavascriptExecutor jse = (JavascriptExecutor) driver;

    //Open the web
    driver.get("http://dev.uangteman.com/admin/");
    driver.manage().window().maximize();

    Thread.sleep(1500);//from w w w  . j  av a 2  s .  c  o m
    driver.findElement(By.name("bu_name")).sendKeys("rahmat_cs");
    driver.findElement(By.name("bu_passwd")).sendKeys("testing");
    driver.findElement(By.cssSelector("button[class='btn btn-orange btn-squared pull-right ladda-button']"))
            .click();

    Thread.sleep(2500);
    jse.executeScript("window.scrollBy(0,1500)", "");
    jse.executeScript("window.scroll(0,1500)", "");

    WebElement scroll = driver.findElement(By.xpath(
            "/html/body/div[2]/div[2]/div/div[2]/div/div/div[3]/div/div[2]/div/form/table/tbody/tr[1]/td[12]/span"));
    JavascriptExecutor js = (JavascriptExecutor) driver;
    jse.executeScript(
            "document.getElementByxpath('/html/body/div[2]/div[2]/div/div[2]/div/div/div[3]/div/div[2]/div/form/table/tbody/tr[1]/td[12]/span').scrollLeft += 250",
            "");

    //Get page title in selenium webdriver
    String actual = driver.getTitle();

}

From source file:com.novartis.opensource.yada.test.YADAAdminTest.java

License:Apache License

/**
 * Test App Manager new query for new app (prep only)
 * @param qname the name of query to test
 * @param query the sql to insert/*from  w  w w  . j  av  a  2 s  .  co  m*/
 * @param action {@code save} or {@code cancel}. Case matters as this value corresponds to a button id
 * @throws InterruptedException if any thread has interrupted the current thread
 */
private void createQueryForNewApp(String qname, String query, String action) throws InterruptedException {
    WebDriver d = getDriver();

    // restore app-mgr
    d.findElement(By.id("app")).click();
    new WebDriverWait(d, 20).until(ExpectedConditions.presenceOfElementLocated(By.id("app-mgr")));
    // click app's 'queries' button
    d.findElement(By.id("app-qname-" + this.app)).click();

    validateMenues();
    openQueryEditor();
    validateQueryEditor();

    // populate fields
    d.findElement(By.id("query-name")).sendKeys(qname);
    d.findElement(By.id("query-comments")).sendKeys("Testing comments");
    // codemirror
    WebElement e = d.findElement(By.cssSelector("div.CodeMirror"));
    JavascriptExecutor js = (JavascriptExecutor) d;
    js.executeScript("arguments[0].CodeMirror.setValue(\"" + query + "\");", e);

    d.findElement(By.id("button-" + action)).click();
}

From source file:com.numenta.htmit.mobile.test.behavior.TestUtilities.java

License:Open Source License

public static void swipe(WebDriver driver, boolean forward) throws InterruptedException {
    double startXValue, endXValue;
    if (forward) {
        startXValue = 0.95;//from   ww  w  .j a  va  2s .c  o m
        endXValue = 0.05;
    } else {
        startXValue = 0.05;
        endXValue = 0.95;
    }
    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap<String, Double> swipeObject = new HashMap<String, Double>();
    swipeObject.put("startX", startXValue);
    swipeObject.put("startY", 0.5);
    swipeObject.put("endX", endXValue);
    swipeObject.put("endY", 0.5);
    swipeObject.put("duration", 1.8);
    js.executeScript("mobile: swipe", swipeObject);
}

From source file:com.numenta.htmit.mobile.test.behavior.TestUtilities.java

License:Open Source License

public static void longPressOnInstance(WebDriver driver) throws InterruptedException {
    WebElement clickInstance = driver.findElement(INSTANCE);
    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap<String, String> tapObject = new HashMap<String, String>();
    tapObject.put("element", ((RemoteWebElement) clickInstance).getId());
    js.executeScript("mobile: longClick", tapObject);
}