Search in sources :

Example 31 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)

Example 32 with JavascriptExecutor

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

the class MobileElement method scrollTo.

/**
     * Scroll to element (at the center of the screen)
     *
     * @return this mobile element which allows chained actions
     */
@SuppressWarnings("unchecked")
@PublicAtsApi
public T scrollTo() {
    try {
        if (MobileElementFinder.getElementContext(this).toUpperCase().startsWith("WEBVIEW")) {
            // in WEBVIEWs the target element exists, while in the NATIVE context it doesn't until we scroll to it
            new MobileElementState(this).waitToBecomeExisting();
            Dimension screenDimensions = ((MobileDriver) getUiDriver()).getScreenDimensions();
            WebElement element = MobileElementFinder.findElement(appiumDriver, this);
            // window.scrollTo(0, element.getLocation().y);    -->  will scroll the element to top-left
            int scrollToY = 0;
            int screenCenter = screenDimensions.getHeight() / 2 + element.getSize().height / 2;
            if (element.getLocation().y < screenCenter) {
                // the element is located after the screen center if we scroll to (0, element.getLocation().y)
                // because it is near the bottom of the application => we can't center it, but it is OK on that position
                scrollToY = element.getLocation().y;
            } else {
                scrollToY = element.getLocation().y - screenCenter;
            }
            ((JavascriptExecutor) appiumDriver).executeScript("window.scrollTo(0," + scrollToY + ")");
        } else {
            if (getElementProperty("name") != null) {
                // only works for NATIVE context
                appiumDriver.scrollTo(getElementProperty("name"));
            }
        }
        return (T) this;
    } catch (Exception e) {
        throw new MobileOperationException(this, "scrollTo", e);
    }
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) MobileDriver(com.axway.ats.uiengine.MobileDriver) MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) Dimension(org.openqa.selenium.Dimension) WebElement(org.openqa.selenium.WebElement) MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) VerificationException(com.axway.ats.uiengine.exceptions.VerificationException) MobileElementState(com.axway.ats.uiengine.utilities.mobile.MobileElementState) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 33 with JavascriptExecutor

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

the class HtmlFileBrowse method setFileInputValue.

/**
    *
    * @param webDriver {@link WebDriver} instance
    * @param value the file input value to set
    */
protected void setFileInputValue(WebDriver webDriver, String value) {
    String locator = this.getElementProperties().getInternalProperty(HtmlElementLocatorBuilder.PROPERTY_ELEMENT_LOCATOR);
    String css = this.getElementProperty("_css");
    WebElement element = null;
    if (!StringUtils.isNullOrEmpty(css)) {
        element = webDriver.findElement(By.cssSelector(css));
    } else {
        element = webDriver.findElement(By.xpath(locator));
    }
    try {
        element.sendKeys(value);
    } catch (ElementNotVisibleException enve) {
        if (!UiEngineConfigurator.getInstance().isWorkWithInvisibleElements()) {
            throw enve;
        }
        // try to make the element visible overriding some CSS properties
        // but keep in mind that it can be still invisible using another CSS and/or JavaScript techniques
        String styleAttrValue = element.getAttribute("style");
        JavascriptExecutor jsExec = (JavascriptExecutor) webDriver;
        try {
            jsExec.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "display:'block'; visibility:'visible'; top:'auto'; left:'auto'; z-index:999;" + "height:'auto'; width:'auto';");
            element.sendKeys(value);
        } finally {
            jsExec.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, styleAttrValue);
        }
    } catch (InvalidElementStateException e) {
        throw new SeleniumOperationException(e.getMessage(), e);
    }
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) WebElement(org.openqa.selenium.WebElement) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) InvalidElementStateException(org.openqa.selenium.InvalidElementStateException) ElementNotVisibleException(org.openqa.selenium.ElementNotVisibleException)

Example 34 with JavascriptExecutor

use of org.openqa.selenium.JavascriptExecutor in project java.webdriver by sayems.

the class ClickWithJavascript method accept.

/**
     * Uses {@code JavascriptExecutor} with the specified driver to perform a click on the specified element.
     *
     * @param driver  the {@code WebDriver} to use with {@code JavascriptExecutor} for performing the click
     * @param element the {@code WebElement} to click
     */
public void accept(WebDriver driver, WebElement element) {
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].click();", element);
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor)

Example 35 with JavascriptExecutor

use of org.openqa.selenium.JavascriptExecutor in project java.webdriver by sayems.

the class Click method main.

public static void main(String[] args) {
    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.navigate().to("http://www.google.com");
    JavascriptExecutor jse = (JavascriptExecutor) driver;
    driver.findElement(By.id("gbqfq")).sendKeys("Selenium");
    jse.executeScript("document.getElementById('gbqfba').click();");
}
Also used : WebDriver(org.openqa.selenium.WebDriver) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver)

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