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() throws Exception {
  WebDriver driver = new ChromeDriver();
  driver.get("http://www.google.com");
  try {
    JavascriptExecutor js = (JavascriptExecutor) driver;

    String title = (String) js.executeScript("return document.title");
    assertEquals("Google", title);

    long links = (Long) js
        .executeScript("var links = document.getElementsByTagName('A'); return links.length");
    assertEquals(42, links);
  } finally {
    driver.quit();
  }
}

How it works...

By casting the WebDriver instance to a JavascriptExecutor interface, we can execute the JavaScript code in Selenium WebDriver:

JavascriptExecutor js = (JavascriptExecutor) driver;

In the following example, a single line of JavaScript code is executed to return the title of the page displayed in the driver. The JavascriptExecutor interface provides the executeScript() method to which we need to pass the JavaScript code:

String title = (String) js.executeScript("return document.title");

Tip

When returning values from the JavaScript code, we need to use the return keyword. Based on the type of return value, we need to cast the executeScript() method. For decimal values, Double can be used; for non-decimal numeric values, Long can be used; and for Boolean values, Boolean can be used. If JavaScript code is returning an HTML element, then WebElement can be used. For text values, String can be used. If a list of objects is returned, then any of the values will work based on the type of objects. Otherwise, a null will be returned.

In the following example, we execute a multiline JavaScript code to retrieve the count of links on a page:

long links = (Long) js.executeScript("var links = document.getElementsByTagName('A'); return links.length");

There's more...

Arguments can also be a passed to the JavaScript code being executed by using the executeScript() method. In the following example, we want to set the value of an element. A special arguments array will be used inside the JavaScript code, as shown in the following code:

js.executeScript("document.getElementByID('name').value = arguments[0]","John");
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.93.12