Example usage for org.openqa.selenium.remote RemoteWebDriver executeScript

List of usage examples for org.openqa.selenium.remote RemoteWebDriver executeScript

Introduction

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

Prototype

@Override
    public Object executeScript(String script, Object... args) 

Source Link

Usage

From source file:com.google.gwt.benchmark.compileserver.server.manager.WebDriverRunner.java

License:Apache License

@Override
public void run() {
    logger.info("Starting webdriver for " + url);

    DesiredCapabilities capabilities = createCapabilities(config);
    RemoteWebDriver driver = null;
    try {// ww  w.j  av a 2 s.  c  o m
        driver = new RemoteWebDriver(hubURL, capabilities);
        driver.navigate().to(url);

        long startMs = System.currentTimeMillis();

        // Initial wait since IE11 has issues running JS before the page has loaded
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ignored) {
        }

        // Wait till the benchmark has finished running.
        while (true) {
            boolean isReady = (Boolean) driver.executeScript(IS_READY_JS, new Object[] {});
            if (isReady) {
                break;
            }
            if (System.currentTimeMillis() - startMs > TIMEOUT_MS) {
                this.failed = true;
                logger.info("Timeout webdriver for " + url);

                failed = true;
                errorMessage = "Timeout";
                return;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException ignored) {
            }
        }

        // Read and report status.
        boolean failed = (Boolean) driver.executeScript(IS_FAILED_JS, new Object[] {});
        if (failed) {
            this.failed = true;
            this.errorMessage = "Benchmark failed to run in browser - Benchmarkframework reported a failure";
            logger.info("Benchmark failed to run for " + url);
        } else {

            result = ((Number) driver.executeScript(GET_RESULT_JS, new Object[] {})).doubleValue();
            done = true;
        }
    } catch (Exception e) {
        logger.log(Level.INFO, "Error while running webdriver for " + url, e);
        failed = true;
        errorMessage = "Unexpected excpetion during webdriver run: " + e.getMessage();
    } finally {
        if (driver != null) {
            driver.quit();
        }
    }
}

From source file:org.xframium.utility.BrowserCacheLogic.java

License:Open Source License

/**
 * clears the Safari browser Cache for IOS environment
 * @param driver - RemoteWebDriver instance
 * @throws Exception/*  www  . j  a v  a  2  s. c om*/
 */
public static void clearSafariIOSCache(RemoteWebDriver driver) throws Exception {
    HashMap<String, Object> params = new HashMap();

    String currentContext = getCurrentContextHandle(driver);

    try {
        switchToContext(driver, "NATIVE_APP");

        //
        // get os version
        //

        params.put("property", "osVersion");
        String osVerStr = (String) driver.executeScript("mobile:handset:info", params);
        int[] osVer = parseVersion(osVerStr);

        //
        // Launch Settings Application on it's main page
        //

        params.clear();
        params.put("name", "Settings");
        try {
            driver.executeScript("mobile:application:close", params);
        } catch (Exception e) {
        }
        driver.executeScript("mobile:application:open", params);
        sleep(1000);

        //
        // Make sure we're on the home page
        //

        clickIfPresent(driver, "//button[@name='Settings']");

        //
        // Scroll to Top
        //

        params.clear();
        params.put("location", "60%,1%");
        driver.executeScript("mobile:touch:tap", params);

        //
        // swipe to expose safari and click once exposed
        //

        boolean found = false;
        int count = 0;

        while ((!found) && (count < 10)) {
            found = clickIfPresent(driver, "//cell[@name='Safari']");

            if (!found) {
                params.clear();
                params.put("start", "50%,75%");
                params.put("end", "50%,25%");
                driver.executeScript("mobile:touch:swipe", params);

                ++count;
            }
        }

        //
        // swipe to bottom
        //

        params.clear();
        params.put("start", "50%,90%");
        params.put("end", "50%,10%");
        for (int i = 0; i < 3; i++) {
            driver.executeScript("mobile:touch:swipe", params);
        }

        //
        // clear Cache
        //

        params.clear();
        params.put("value", "//*[starts-with(text(),'Clear History')]");
        params.put("framework", "perfectoMobile");
        driver.executeScript("mobile:application.element:click", params);
        params.put("value",
                "//*[(@class='UIAButton' or @class='UIATableCell') and starts-with(@label,'Clear') and @isvisible='true']");
        driver.executeScript("mobile:application.element:click", params);

        //
        // below version 8 need to clear also data
        //

        if (osVer[0] < 8) {
            params.put("value", "//*[starts-with(text(),'Clear Cookies')]");
            params.put("framework", "perfectoMobile");
            driver.executeScript("mobile:application.element:click", params);
            params.put("value",
                    "//*[(@class='UIAButton' or @class='UIATableCell') and starts-with(@label,'Clear') and @isvisible='true']");
            params.put("framework", "perfectoMobile");
            driver.executeScript("mobile:application.element:click", params);
        }

        //
        // Close Settings
        //

        params.clear();
        params.put("name", "Settings");

        driver.executeScript("mobile:application:close", params);
    } finally {
        switchToContext(driver, currentContext);
    }
}

From source file:org.xframium.utility.BrowserCacheLogic.java

License:Open Source License

/**
 * clears the Chrome browser Cache for Android environment
 * @param driver - RemoteWebDriver instance
 * @throws Exception/*from  www.  j  a v  a 2 s. co m*/
 */
public static void clearChromeAndroidCache(RemoteWebDriver driver) throws Exception {
    HashMap<String, Object> params = new HashMap();

    String currentContext = getCurrentContextHandle(driver);

    try {
        switchToContext(driver, "NATIVE_APP");

        //
        // get os version
        //

        params.put("property", "osVersion");
        String osVerStr = (String) driver.executeScript("mobile:handset:info", params);
        int[] osVer = parseVersion(osVerStr);

        if (osVer[0] >= 5) {
            //
            // Launch Chrome
            //

            params.clear();
            params.put("name", "Chrome");
            try {
                driver.executeScript("mobile:application:close", params);
            } catch (Exception e) {
            }
            driver.executeScript("mobile:application:open", params);
            sleep(1000);

            //
            // select chrome menu
            //
            // resource-id: com.android.chrome:id/document_menu_button -- contains menu_button
            // content-desc: More options
            //

            params.clear();
            params.put("value", "//*[starts-with(@resource-id,'menu_button')]");
            params.put("framework", "perfectoMobile");
            driver.executeScript("mobile:application.element:click", params);

            //
            // select history
            //
            // text: History
            // resource-id: com.android.chrome:id/menu_item_text
            // content-desc: History
            //

            params.clear();
            params.put("value", "//*[starts-with(text(),'History')]");
            params.put("framework", "perfectoMobile");
            driver.executeScript("mobile:application.element:click", params);

            //
            // select clear browsing data
            //
            // text: Clear browsing data...
            // id: clear-browsing-data
            // cssSelector: #clear-browsing-data
            //

            params.clear();
            params.put("value", "//*[starts-with(text(),'Clear browsing')]");
            params.put("framework", "perfectoMobile");
            driver.executeScript("mobile:application.element:click", params);

            //
            // do it!
            //
            // text: Clear
            //

            params.clear();
            params.put("value", "//*[starts-with(text(),'Clear')]");
            params.put("framework", "perfectoMobile");
            driver.executeScript("mobile:application.element:click", params);

            //
            // Close Chrome
            //

            params.clear();
            params.put("name", "Chrome");

            driver.executeScript("mobile:application:close", params);
        } else {
            throw new IllegalStateException("not supported before Android 5.0");
        }
    } finally {
        switchToContext(driver, currentContext);
    }
}

From source file:org.xframium.utility.BrowserCacheLogic.java

License:Open Source License

private static boolean clickIfPresent(RemoteWebDriver driver, String xpath) {
    boolean rtn = false;

    HashMap<String, Object> params = new HashMap();
    params.put("value", xpath);
    params.put("framework", "perfectoMobile");

    String result = (String) driver.executeScript("mobile:application.element:find", params);

    if ((result != null) && (!("false".equalsIgnoreCase(result)))) {
        ////  w w  w  . j a v a2 s. c  o  m
        // This shouldn't be necessary, but I'm seeing the 'find' call finding
        // elements that are scrolled off of the screen/page.
        //

        try {
            driver.executeScript("mobile:application.element:click", params);

            rtn = true;
        } catch (Throwable e) {
        }
    }

    return rtn;
}