Search in sources :

Example 21 with JavascriptExecutor

use of org.openqa.selenium.JavascriptExecutor in project nutch by apache.

the class DefalultMultiInteractionHandler method processDriver.

public String processDriver(WebDriver driver) {
    // loop and get multiple pages in this string
    String accumulatedData = "";
    try {
        // append the string to the last page's driver
        JavascriptExecutor jsx = (JavascriptExecutor) driver;
        jsx.executeScript("document.body.innerHTML=document.body.innerHTML " + accumulatedData + ";");
    } catch (Exception e) {
        LOG.info(StringUtils.stringifyException(e));
    }
    return accumulatedData;
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor)

Example 22 with JavascriptExecutor

use of org.openqa.selenium.JavascriptExecutor in project nutch by apache.

the class DefaultClickAllAjaxLinksHandler method processDriver.

public String processDriver(WebDriver driver) {
    String accumulatedData = "";
    try {
        driver.findElement(By.tagName("body")).getAttribute("innerHTML");
        Configuration conf = NutchConfiguration.create();
        new WebDriverWait(driver, conf.getLong("libselenium.page.load.delay", 3));
        List<WebElement> atags = driver.findElements(By.tagName("a"));
        int numberofajaxlinks = atags.size();
        for (int i = 0; i < numberofajaxlinks; i++) {
            if (atags.get(i).getAttribute("href") != null && atags.get(i).getAttribute("href").equals("javascript:void(null);")) {
                atags.get(i).click();
                if (i == numberofajaxlinks - 1) {
                    // append everything to the driver in the last round
                    JavascriptExecutor jsx = (JavascriptExecutor) driver;
                    jsx.executeScript("document.body.innerHTML=document.body.innerHTML " + accumulatedData + ";");
                    continue;
                }
                accumulatedData += driver.findElement(By.tagName("body")).getAttribute("innerHTML");
                // refreshing the handlers as the page was interacted with
                driver.navigate().refresh();
                new WebDriverWait(driver, conf.getLong("libselenium.page.load.delay", 3));
                atags = driver.findElements(By.tagName("a"));
            }
        }
    } catch (Exception e) {
        LOG.info(StringUtils.stringifyException(e));
    }
    return accumulatedData;
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) NutchConfiguration(org.apache.nutch.util.NutchConfiguration) Configuration(org.apache.hadoop.conf.Configuration) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) WebElement(org.openqa.selenium.WebElement)

Example 23 with JavascriptExecutor

use of org.openqa.selenium.JavascriptExecutor in project syndesis-qe by syndesisio.

the class CommonSteps method scrollTo.

/**
 * Scroll the webpage.
 *
 * @param topBottom possible values: top, bottom
 * @param leftRight possible values: left, right
 * @returns {Promise<any>}
 */
@When("^scroll \"([^\"]*)\" \"([^\"]*)\"$")
public void scrollTo(String topBottom, String leftRight) {
    WebDriver driver = WebDriverRunner.getWebDriver();
    JavascriptExecutor jse = (JavascriptExecutor) driver;
    int x = 0;
    int y = 0;
    Long width = (Long) jse.executeScript("return $(document).width()");
    Long height = (Long) jse.executeScript("return $(document).height()");
    if (leftRight.equals("right")) {
        y = width.intValue();
    }
    if (topBottom.equals("bottom")) {
        x = height.intValue();
    }
    jse.executeScript("(browserX, browserY) => window.scrollTo(browserX, browserY)", x, y);
}
Also used : WebDriver(org.openqa.selenium.WebDriver) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) When(cucumber.api.java.en.When)

Example 24 with JavascriptExecutor

use of org.openqa.selenium.JavascriptExecutor in project ats-framework by Axway.

the class HiddenHtmlTable method getAllValues.

/**
     * Get the values of all table cells.</br>
     * 
     * <b>Note:</b> If a table cell contains a checkbox - we will return 'checked' or 'notchecked' value.
     * 
     * @return a two dimensional array containing all table cell values
     */
@PublicAtsApi
public String[][] getAllValues() {
    new HiddenHtmlElementState(this).waitToBecomeExisting();
    WebElement table = HiddenHtmlElementLocator.findElement(this);
    String scriptForHtml = generateScriptForGettingTableContent(".innerHTML");
    Object returnedHtmlValue = ((JavascriptExecutor) webDriver).executeScript(scriptForHtml, table);
    String scriptForObjects = generateScriptForGettingTableContent("");
    Object returnedObjectsValue = ((JavascriptExecutor) webDriver).executeScript(scriptForObjects, table);
    String[][] tableData = null;
    if (returnedHtmlValue != null && returnedHtmlValue instanceof List && returnedObjectsValue != null && returnedObjectsValue instanceof List) {
        List<?> htmlTable = (List<?>) returnedHtmlValue;
        List<?> objectsTable = (List<?>) returnedObjectsValue;
        // allocate space for a number of rows
        tableData = new String[htmlTable.size()][];
        for (int iRow = 0; iRow < htmlTable.size(); iRow++) {
            if (htmlTable.get(iRow) instanceof List) {
                List<?> htmlRow = (List<?>) htmlTable.get(iRow);
                List<?> objectsRow = (List<?>) objectsTable.get(iRow);
                // allocate space for the cells of the current row
                tableData[iRow] = new String[htmlRow.size()];
                for (int iColumn = 0; iColumn < htmlRow.size(); iColumn++) {
                    Object htmlWebElement = htmlRow.get(iColumn);
                    Object objectWebElement = objectsRow.get(iColumn);
                    // some data cannot be presented in textual way - for example a checkbox 
                    String htmlValueString = htmlWebElement.toString().toLowerCase().replace("\r", "").replace("\n", "");
                    if (htmlValueString.matches(".*<input.*type=.*[\"|']checkbox[\"|'].*>.*")) {
                        // We assume this is a checkbox inside a table cell.
                        // We will return either 'checked' or 'notchecked'
                        tableData[iRow][iColumn] = htmlValueString.contains("checked") ? "checked" : "notchecked";
                    } else if (objectWebElement instanceof WebElement) {
                        // proceed in the regular way by returning the data visible to the user
                        tableData[iRow][iColumn] = ((WebElement) objectWebElement).getText().trim();
                    } else {
                        tableData[iRow][iColumn] = null;
                    }
                }
            }
        }
    } else {
        log.warn("We could not get the content of table declared as: " + this.toString());
    }
    return tableData;
}
Also used : HiddenHtmlElementState(com.axway.ats.uiengine.utilities.hiddenbrowser.HiddenHtmlElementState) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) List(java.util.List) WebElement(org.openqa.selenium.WebElement) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 25 with JavascriptExecutor

use of org.openqa.selenium.JavascriptExecutor in project ats-framework by Axway.

the class HiddenHtmlTable method getFieldValue.

/**
     * Get the value of the specified table field
     *
     * @param row the field row starting at 0
     * @param column the field column starting at 0
     * @return the value
     */
@Override
@PublicAtsApi
public String getFieldValue(int row, int column) {
    new HiddenHtmlElementState(this).waitToBecomeExisting();
    WebElement table = HiddenHtmlElementLocator.findElement(this);
    String script = "var table = arguments[0]; var row = arguments[1]; var col = arguments[2];" + "if (row > table.rows.length) { return \"Cannot access row \" + row + \" - table has \" + table.rows.length + \" rows\"; }" + "if (col > table.rows[row].cells.length) { return \"Cannot access column \" + col + \" - table row has \" + table.rows[row].cells.length + \" columns\"; }" + "return table.rows[row].cells[col];";
    JavascriptExecutor jsExecutor = (JavascriptExecutor) webDriver;
    Object value = jsExecutor.executeScript(script, table, row, column);
    if (value instanceof WebElement) {
        return ((WebElement) value).getText().trim();
    }
    return null;
}
Also used : HiddenHtmlElementState(com.axway.ats.uiengine.utilities.hiddenbrowser.HiddenHtmlElementState) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) WebElement(org.openqa.selenium.WebElement) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)47 WebElement (org.openqa.selenium.WebElement)25 WebDriver (org.openqa.selenium.WebDriver)10 PublicAtsApi (com.axway.ats.common.PublicAtsApi)9 RealHtmlElementState (com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState)4 Test (org.junit.Test)4 ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)4 HiddenHtmlElementState (com.axway.ats.uiengine.utilities.hiddenbrowser.HiddenHtmlElementState)3 Dimension (org.openqa.selenium.Dimension)3 RemoteWebElement (org.openqa.selenium.remote.RemoteWebElement)3 Select (org.openqa.selenium.support.ui.Select)3 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)3 VerificationException (com.axway.ats.uiengine.exceptions.VerificationException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Cookie (org.openqa.selenium.Cookie)2 TimeoutException (org.openqa.selenium.TimeoutException)2 Actions (org.openqa.selenium.interactions.Actions)2 MobileDriver (com.axway.ats.uiengine.MobileDriver)1 MobileOperationException (com.axway.ats.uiengine.exceptions.MobileOperationException)1