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.

1 comment:

  1. How can I write the Javascript code (with Syntax) mentioned above in Java using WebDriver?

    ReplyDelete