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:org.richfaces.tests.page.fragments.impl.Utils.java

License:Open Source License

/**
 * Executes returning jQuery command on input element. E.g. to get a position
 * of element from top of the page use returningjQ("position().top", element).
 * @param cmd command to be executed/*from ww  w  .j  a  v a 2  s  .com*/
 * @param element element on which the command will be executed
 */
public static String returningJQ(String cmd, WebElement element) {
    JavascriptExecutor executor = (JavascriptExecutor) GrapheneContext.getProxy();
    String jQueryCmd = String.format("x = jQuery(arguments[0]).%s ; return x;", cmd);
    return String.valueOf(executor.executeScript(jQueryCmd, element));
}

From source file:org.specrunner.webdriver.actions.PluginBlur.java

License:Open Source License

@Override
protected void process(IContext context, IResultSet result, WebDriver client, WebElement element)
        throws PluginException {
    JavascriptExecutor js = (JavascriptExecutor) client;
    js.executeScript("arguments[0].focus(); arguments[0].blur(); return true;", element);
    result.addResult(Success.INSTANCE, context.peek());
}

From source file:org.specrunner.webdriver.impl.PrepareDefault.java

License:Open Source License

@Override
public void prepare(AbstractPluginFind source, WebDriver client, WebElement... elements) {
    if (client instanceof JavascriptExecutor) {
        JavascriptExecutor executor = (JavascriptExecutor) client;
        if (elements != null) {
            for (WebElement e : elements) {
                if (e != null) {
                    try {
                        Point location = e.getLocation();
                        if (location != null) {
                            executor.executeScript("arguments[0].focus()", e);
                            executor.executeScript(
                                    "window.scrollTo(" + location.getX() + "," + location.getY() + ")");
                        }// w w  w . j  av  a2  s  .  c  o m
                    } catch (WebDriverException ex) {
                        if (UtilLog.LOG.isTraceEnabled()) {
                            UtilLog.LOG.trace(ex.getMessage(), e);
                        }
                    }
                }
            }
        }
    }
}

From source file:org.sugarcrm.voodoodriver.Browser.java

License:Apache License

/**
 * Execute javascript in the browser./*from   w  w  w  . jav  a2s. c  o m*/
 *
 * This method executes a javascript string in the context of the
 * browser.  During execution, a variable, "CONTROL", is created
 * for use by the script.
 *
 * @param script  The javascript to run in the browser.
 * @param element The Element to use on the page as the CONTROL var.
 * @return the {@link Object} returned by the javascript code
 */

public Object executeJS(String script, WebElement element) {
    JavascriptExecutor js = (JavascriptExecutor) this.Driver;

    try {
        if (element != null) {
            return js.executeScript(script, element);
        } else {
            return js.executeScript(script);
        }
    } catch (org.openqa.selenium.WebDriverException e) {
        this.reporter.Warn("Exception during javascript execution: " + e);
    }

    return null;
}

From source file:org.usapi.BaseApplication.java

License:Apache License

/**
 * Scroll the browser window/content by the specified number of pixels
 * Note: For this method to work, the visible property of the window's scrollbar must be set to true!
 * @param x: Required.  How many pixels to scroll by, along the x-axis (horizontal)
 * @param y: Required.  How many pixels to scroll by, along the y-axis (vertical)
 *//* w  w w.j a va 2s .  com*/
public void scrollBy(int x, int y) {
    JavascriptExecutor jsx = (JavascriptExecutor) getWebDriver();
    jsx.executeScript("window.scrollBy(" + x + "," + y + ")", "");
}

From source file:org.webtestingexplorer.identifiers.XpathWebElementIdentifier.java

License:Open Source License

public XpathWebElementIdentifier(WebDriver driver, WebElement element) {
    super(null);/*from ww  w .  ja va2s .  c  o  m*/
    StringBuilder jsString = new StringBuilder();
    jsString.append(JavaScriptUtil.getJavaScriptFromFile("/getXpathFromWebElement.js"));

    // Execute js
    JavascriptExecutor js = (JavascriptExecutor) driver;
    xpath = (String) js.executeScript(jsString.toString(), element);
}