Search in sources :

Example 16 with JavascriptExecutor

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

the class AbstractHtmlEngine method fireEvent.

/**
     * Explicitly simulate an event, to trigger the corresponding "onevent" handler.
     * @param element the target element
     * @param eventName the name of the event to fire e.g. click, blur, focus, keydown, keyup, keypress...
     */
@PublicAtsApi
public void fireEvent(UiElement element, String eventName) {
    HtmlNavigator.getInstance().navigateToFrame(webDriver, element);
    String xpath = element.getElementProperties().getInternalProperty(HtmlElementLocatorBuilder.PROPERTY_ELEMENT_LOCATOR);
    String css = element.getElementProperty("_css");
    WebElement webElement = null;
    if (!StringUtils.isNullOrEmpty(css)) {
        webElement = webDriver.findElement(By.cssSelector(css));
    } else {
        webElement = webDriver.findElement(By.xpath(xpath));
    }
    StringBuilder builder = new StringBuilder();
    // @formatter:off
    builder.append("function triggerEvent(element, eventType, canBubble, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown) {\n" + "        canBubble = (typeof(canBubble) == undefined) ? true : canBubble;\n" + "        if (element.fireEvent && element.ownerDocument && element.ownerDocument.createEventObject) { // IE\n" + "            var evt = this.createEventObject(element, controlKeyDown, altKeyDown, shiftKeyDown, metaKeyDown);\n" + "            element.fireEvent('on' + eventType, evt);\n" + "        } else {\n" + "            var evt = document.createEvent('HTMLEvents');\n" + "            try {\n" + "                evt.shiftKey = shiftKeyDown;\n" + "                evt.metaKey = metaKeyDown;\n" + "                evt.altKey = altKeyDown;\n" + "                evt.ctrlKey = controlKeyDown;\n" + "            } catch (e) {\n" + "                // Nothing sane to do\n" + "            }\n" + "            evt.initEvent(eventType, canBubble, true);\n" + "            element.dispatchEvent(evt);\n" + "        }\n" + "    }\n");
    // @formatter:on
    builder.append("triggerEvent(arguments[0],'").append(eventName).append("',false);");
    ((JavascriptExecutor) webDriver).executeScript(builder.toString(), webElement);
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) WebElement(org.openqa.selenium.WebElement) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 17 with JavascriptExecutor

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

the class RealHtmlTable 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 RealHtmlElementState(this).waitToBecomeExisting();
    WebElement table = RealHtmlElementLocator.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 {
                        // proceed in the regular way by returning the data visible to the user
                        tableData[iRow][iColumn] = ((RemoteWebElement) objectWebElement).getText();
                    }
                }
            }
        }
    } else {
        log.warn("We could not get the content of table declared as: " + this.toString());
    }
    return tableData;
}
Also used : RealHtmlElementState(com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) List(java.util.List) WebElement(org.openqa.selenium.WebElement) RemoteWebElement(org.openqa.selenium.remote.RemoteWebElement) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 18 with JavascriptExecutor

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

the class RealHtmlTable method setFieldValue.

/**
     * Set value of specified table field
     *
     * @param value the new table cell value
     * @param row the field row starting at 0
     * @param column the field column starting at 0
     * @return
     */
@Override
@PublicAtsApi
public void setFieldValue(String value, int row, int column) {
    new RealHtmlElementState(this).waitToBecomeExisting();
    WebElement table = RealHtmlElementLocator.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\"; }" + "table.rows[row].cells[col].textContent = '" + value + "';";
    ((JavascriptExecutor) webDriver).executeScript(script, table, row, column);
}
Also used : RealHtmlElementState(com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) WebElement(org.openqa.selenium.WebElement) RemoteWebElement(org.openqa.selenium.remote.RemoteWebElement) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 19 with JavascriptExecutor

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

the class RealHtmlTable 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
     */
@Override
@PublicAtsApi
public String getFieldValue(int row, int column) {
    new RealHtmlElementState(this).waitToBecomeExisting();
    WebElement table = RealHtmlElementLocator.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];";
    Object value = ((JavascriptExecutor) webDriver).executeScript(script, table, row, column);
    if (value instanceof WebElement) {
        return ((WebElement) value).getText().trim();
    }
    return null;
}
Also used : RealHtmlElementState(com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) WebElement(org.openqa.selenium.WebElement) RemoteWebElement(org.openqa.selenium.remote.RemoteWebElement) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 20 with JavascriptExecutor

use of org.openqa.selenium.JavascriptExecutor in project ghostdriver by detro.

the class CookieTest method shouldBeAbleToCreateCookieViaJavascriptOnGoogle.

@Test
public void shouldBeAbleToCreateCookieViaJavascriptOnGoogle() {
    String ckey = "cookiekey";
    String cval = "cookieval";
    WebDriver d = getDriver();
    d.get("http://www.google.com");
    JavascriptExecutor js = (JavascriptExecutor) d;
    // Of course, no cookie yet(!)
    Cookie c = d.manage().getCookieNamed(ckey);
    assertNull(c);
    // Attempt to create cookie on multiple Google domains
    js.executeScript("javascript:(" + "function() {" + "   cook = document.cookie;" + "   begin = cook.indexOf('" + ckey + "=');" + "   var val;" + "   if (begin !== -1) {" + "       var end = cook.indexOf(\";\",begin);" + "       if (end === -1)" + "           end=cook.length;" + "       val=cook.substring(begin+11,end);" + "   }" + "   val = ['" + cval + "'];" + "   if (val) {" + "       var d=Array('com','co.jp','ca','fr','de','co.uk','it','es','com.br');" + "       for (var i = 0; i < d.length; i++) {" + "           document.cookie = '" + ckey + "='+val+';path=/;domain=.google.'+d[i]+'; ';" + "       }" + "   }" + "})();");
    c = d.manage().getCookieNamed(ckey);
    assertNotNull(c);
    assertEquals(cval, c.getValue());
    // Set cookie as empty
    js.executeScript("javascript:(" + "function() {" + "   var d = Array('com','co.jp','ca','fr','de','co.uk','it','cn','es','com.br');" + "   for(var i = 0; i < d.length; i++) {" + "       document.cookie='" + ckey + "=;path=/;domain=.google.'+d[i]+'; ';" + "   }" + "})();");
    c = d.manage().getCookieNamed(ckey);
    assertNotNull(c);
    assertEquals("", c.getValue());
}
Also used : WebDriver(org.openqa.selenium.WebDriver) Cookie(org.openqa.selenium.Cookie) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) Test(org.junit.Test)

Aggregations

JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)40 WebElement (org.openqa.selenium.WebElement)20 PublicAtsApi (com.axway.ats.common.PublicAtsApi)9 WebDriver (org.openqa.selenium.WebDriver)7 RealHtmlElementState (com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState)4 ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)4 HiddenHtmlElementState (com.axway.ats.uiengine.utilities.hiddenbrowser.HiddenHtmlElementState)3 Test (org.junit.Test)3 Dimension (org.openqa.selenium.Dimension)3 RemoteWebElement (org.openqa.selenium.remote.RemoteWebElement)3 Select (org.openqa.selenium.support.ui.Select)3 VerificationException (com.axway.ats.uiengine.exceptions.VerificationException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)2 MobileDriver (com.axway.ats.uiengine.MobileDriver)1 MobileOperationException (com.axway.ats.uiengine.exceptions.MobileOperationException)1 SeleniumOperationException (com.axway.ats.uiengine.exceptions.SeleniumOperationException)1 MobileElementState (com.axway.ats.uiengine.utilities.mobile.MobileElementState)1 SafeJavascriptWait (com.github.timurstrekalov.saga.core.webdriver.SafeJavascriptWait)1