Executing the JavaScript code
The Selenium WebDriver API provides the ability to execute JavaScript code with the browser window. This is a very useful feature when tests need to interact with the page using JavaScript. Using this API, client-side JavaScript code can also be tested using Selenium WebDriver. Selenium WebDriver provides a JavascriptExecutor
interface that can be used to execute arbitrary JavaScript code within the context of the browser.
In this recipe, we will explore how to use JavascriptExecutor
to execute JavaScript code. This book has various other recipes where JavascriptExecutor
has been used to perform some advanced operations that are not yet supported by Selenium WebDriver.
How to do it...
Let's create a test that will call JavaScript code to return title and count of links (that is a count of Anchor
tags) from a page. Returning a page title can also be done by calling the driver.getTitle()
method. The following is an example code for this:
@Test public void testJavaScriptCalls...