Showing posts with label javascriptexecutor. Show all posts
Showing posts with label javascriptexecutor. Show all posts

Friday, May 25, 2012

JavascriptExecutor methods

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", 
 webElement);
 
((JavascriptExecutor) driver)
.executeScript("arguments[0].setAttribute('style', 'color: yellow; border: 2px solid yellow;
');", element); 
 
((JavascriptExecutor) driver).executeScript("arguments[0].click;", 
 webElement);
 
check image has loaded
 
List<WebElement> allImages = driver.findElements(By.tagName("img"));
for (WebElement image : allImages) {
  boolean loaded = ((JavaScriptExecutor) driver).executeScript(
      "return arguments[0].complete", image);
  if (!loaded) {
    // Your error handling here.
  }
}  

Tuesday, August 2, 2011

Javascript and WebDriver

It is possible using webdriver to handle javascript. For this it uses interface JavascriptExecutor.

1. Sample code for getting page title

WebDriver driver = new FirefoxDriver();
JavascriptExecutor js =  (JavascriptExecutor)driver;
js.executeScript("return document.title");


2. Executing a javascript function declared.

Sample Page Code

<script>
function showAlert(){
alert('success');
}
</script>


Sample code

WebDriver driver = new FirefoxDriver();
JavascriptExecutor js =  (JavascriptExecutor)driver;
js.executeScript("showAlert()");
On executing above code, the alert will be shown.