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());
}
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);
}
}
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);
}
}
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);
}
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();");
}
Aggregations