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.kuali.rice.testtools.selenium.WebDriverHighlightHelper.java

License:Educational Community License

/**
 * <p>/* w  w  w  .j  a  va2s  .co  m*/
 * Highlight given WebElement.
 * </p>
 *
 * @param webDriver to execute highlight on
 * @param webElement to highlight
 */
public void highlightElement(WebDriver webDriver, WebElement webElement) {
    if (jsHighlightEnabled && webElement != null) {
        try {
            //                System.out.println("highlighting " + webElement.toString() + " on url " + webDriver.getCurrentUrl());
            JavascriptExecutor js = (JavascriptExecutor) webDriver;
            String jsHighlight = "element = arguments[0];\n"
                    + "originalStyle = element.getAttribute('style');\n"
                    + "element.setAttribute('style', originalStyle + \"; background: " + JS_HIGHLIGHT_BACKGROUND
                    + "; border: 2px solid " + JS_HIGHLIGHT_BOARDER + ";\");\n" + "setTimeout(function(){\n"
                    + "    element.setAttribute('style', originalStyle);\n" + "}, "
                    + System.getProperty(JS_HIGHLIGHT_MS_PROPERTY, JS_HIGHLIGHT_MS + "") + ");";
            js.executeScript(jsHighlight, webElement);
        } catch (Throwable t) {
            System.out.println("Throwable during javascript highlight element");
            t.printStackTrace();
        }
    }
}

From source file:org.kuali.rice.testtools.selenium.WebDriverUtils.java

License:Educational Community License

/**
 * <p>/*from  w w w  .  jav  a 2 s. c  o  m*/
 * Highlight given WebElement.
 * </p>
 *
 * @param webDriver to execute highlight on
 * @param webElement to highlight
 */
public static void highlightElement(WebDriver webDriver, WebElement webElement) {
    if (jsHighlightEnabled && webElement != null) {
        try {
            //                System.out.println("highlighting " + webElement.toString() + " on url " + webDriver.getCurrentUrl());
            JavascriptExecutor js = (JavascriptExecutor) webDriver;
            String jsHighlight = "element = arguments[0];\n"
                    + "originalStyle = element.getAttribute('style');\n"
                    + "element.setAttribute('style', originalStyle + \"; background: " + JS_HIGHLIGHT_BACKGROUND
                    + "; border: 2px solid " + JS_HIGHLIGHT_BOARDER + ";\");\n" + "setTimeout(function(){\n"
                    + "    element.setAttribute('style', originalStyle);\n" + "}, "
                    + System.getProperty(JS_HIGHLIGHT_MS_PROPERTY, JS_HIGHLIGHT_MS + "") + ");";
            js.executeScript(jsHighlight, webElement);
        } catch (Throwable t) {
            System.out.println("Throwable during javascript highlight element");
            t.printStackTrace();
        }
    }
}

From source file:org.neo4j.server.webdriver.WebadminWebdriverLibrary.java

License:Open Source License

public Object executeScript(String script, Object... args) {
    if (d instanceof JavascriptExecutor) {
        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) d;
        return javascriptExecutor.executeScript(script, args);
    } else {// www.ja v  a  2  s. c o  m
        throw new RuntimeException("Arbitrary script execution is only available for WebDrivers that implement "
                + "the JavascriptExecutor interface.");
    }
}

From source file:org.nuxeo.functionaltests.Locator.java

License:Apache License

/**
 * Forces a click on an element, to workaround non-effective clicks in miscellaneous situations, after having
 * scrolled to it./*from w w w.j a va 2  s .  co  m*/
 *
 * @param executor the javascript executor, usually {@link WebDriver}
 * @param element the element to scroll to
 * @return true if element is clickable
 * @since 8.3
 */
public static final boolean scrollAndForceClick(WebElement element) {
    JavascriptExecutor executor = (JavascriptExecutor) AbstractTest.driver;
    scrollToElement(element);
    try {
        // forced click to workaround non-effective clicks in miscellaneous situations
        executor.executeScript("arguments[0].click();", element);
        return true;
    } catch (WebDriverException e) {
        if (e.getMessage().contains("Element is not clickable at point")) {
            log.debug("Element is not clickable yet");
            return false;
        }
        throw e;
    }
}

From source file:org.richfaces.fragment.common.Utils.java

License:Open Source License

/**
 * Executes jQuery command on input element. E.g. to trigger click use jQ("click()", element).
 *
 * @param cmd command to be executed/*from   ww w . ja  v a2  s .  com*/
 * @param element element on which the command will be executed
 */
public static void jQ(JavascriptExecutor executor, String cmd, WebElement element) {
    Preconditions.checkNotNull(executor, "The executor cannot be null.");
    Preconditions.checkNotNull(cmd, "The command cannot be null.");
    Preconditions.checkNotNull(element, "The element cannot be null.");
    String jQueryCmd = String.format("jQuery(arguments[0]).%s", cmd);
    executor.executeScript(jQueryCmd, unwrap(element));
}

From source file:org.richfaces.fragment.common.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// ww w . j  a  v a  2 s.  c o m
 * @param element element on which the command will be executed
 */
public static String returningJQ(JavascriptExecutor executor, String cmd, WebElement element) {
    Preconditions.checkNotNull(executor, "The executor cannot be null.");
    Preconditions.checkNotNull(cmd, "The command cannot be null.");
    Preconditions.checkNotNull(element, "The element cannot be null.");
    String jQueryCmd = String.format("return jQuery(arguments[0]).%s;", cmd);
    return String.valueOf(executor.executeScript(jQueryCmd, unwrap(element)));
}

From source file:org.richfaces.tests.metamer.ftest.AbstractWebDriverTest.java

License:Open Source License

/**
 * Executes JavaScript script.//from   www  .j  a  va 2  s. co  m
 *
 * @param script whole command that will be executed
 * @param args
 * @return may return a value or null (if expected (non-returning script) or
 * if returning script fails)
 */
protected Object executeJS(String script, Object... args) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    return js.executeScript(script, args);
}

From source file:org.richfaces.tests.metamer.ftest.AbstractWebDriverTest.java

License:Open Source License

/**
 * Tries to execute JavaScript script for few times with some wait time
 * between tries and expecting a predicted result. Method waits for expected
 * string defined in @expectedValue. Returns single trimmed String with
 * expected value or what it found or null.
 *
 * @param expectedValue expected return value of javaScript
 * @param script whole JavaScript that will be executed
 * @param args//from   www  . j a va  2s  .c  o m
 * @return single and trimmed string or null
 */
protected String expectedReturnJS(String script, String expectedValue, Object... args) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    String result = null;
    for (int i = 0; i < TRIES; i++) {
        Object executeScript = js.executeScript(script, args);
        if (executeScript != null) {
            result = ((String) js.executeScript(script, args)).trim();
            if (result.equals(expectedValue)) {
                break;
            }
        }
        waiting(MINOR_WAIT_TIME);
    }
    return result;
}

From source file:org.richfaces.tests.page.fragments.impl.input.TextInputComponentImpl.java

License:Open Source License

@Override
public TextInputComponent clear(ClearType clearType) {
    int valueLength = root.getAttribute("value").length();
    Actions builder = new Actions(getWebDriver());
    JavascriptExecutor executor = (JavascriptExecutor) getWebDriver();
    switch (clearType) {
    case BACKSPACE:
        for (int i = 0; i < valueLength; i++) {
            builder.sendKeys(root, Keys.BACK_SPACE);
        }//  w ww  .  j av  a  2  s . c  o m
        builder.build().perform();
        break;
    case DELETE:
        String ctrlADel = Keys.chord(Keys.CONTROL, "a", Keys.DELETE);
        builder.sendKeys(root, ctrlADel);
        builder.build().perform();
        break;
    case ESCAPE_SQ:
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < valueLength; i++) {
            sb.append("\b");
        }
        root.sendKeys(sb.toString());
        root.click();
        break;
    case JS:
        executor.executeScript("jQuery(arguments[0]).val('')", root);
        break;
    case WD:
        root.clear();
        break;
    default:
        throw new UnsupportedOperationException("Unknown type of clear method " + clearType);
    }
    return this;
}

From source file:org.richfaces.tests.page.fragments.impl.Utils.java

License:Open Source License

/**
 * Executes jQuery command on input element. E.g. to trigger click use jQ("click()", element).
 * @param cmd command to be executed//from w ww  . jav  a  2  s .  c o m
 * @param element element on which the command will be executed
 */
public static void jQ(String cmd, WebElement element) {
    JavascriptExecutor executor = (JavascriptExecutor) GrapheneContext.getProxy();
    String jQueryCmd = String.format("jQuery(arguments[0]).%s", cmd);
    executor.executeScript(jQueryCmd, element);
}