Setting up Internet Explorer Driver Server
We saw how to automate the Firefox browser in previous recipes. Using Firefox was straightforward. In order to execute test scripts on the Internet Explorer browser, we need to use InternetExplorerDriver
and a standalone Internet Explorer Driver Server executable.
Let's setup InternetExplorerDriver
and create tests for testing the search feature on Internet Explorer.
Getting ready
You need to download Internet Explorer Driver Server from http://docs.seleniumhq.org/download/. It is available in both 32bit and 64bit versions.
After downloading the IEDriver server, unzip and copy the file to the same directory in which the scripts are stored.
How to do it...
Let's create a test that uses Internet Explorer Driver Server with the following steps:
- In Eclipse, create a new folder named
drivers
in the src/test/resources folder of the SeleniumCookbook project. Copy theIEDriverServer.exe
file to this folder, as shown in the following screenshot: - Add a new test and name it as
GoogleSearchTestOnIE
, and add the following code:package com.secookbook.examples.chapter01; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; import org.junit.*; import static org.junit.Assert.*; public class GoogleSearchTestOnIE { private WebDriver driver; @Before public void setUp() { System.setProperty("webdriver.ie.driver", "src/test/resources/drivers/IEDriverServer.exe"); DesiredCapabilities caps = DesiredCapabilities.internetExplorer(); caps.setCapability( InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); // Launch Internet Explorer driver = new InternetExplorerDriver(caps); // Maximize the browser window driver.manage().window().maximize(); // Navigate to Google driver.get("http://www.google.com"); } @Test public void testGoogleSearch() { // Find the text input element by its name WebElement element = driver.findElement(By.name("q")); // Enter something to search for element.sendKeys("Selenium testing tools cookbook"); // Now submit the form. WebDriver will find // the form for us from the element element.submit(); // Google's search is rendered dynamically with JavaScript. // Wait for the page to load, timeout after 10 seconds new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase() .startsWith("selenium testing tools cookbook"); } }); assertEquals("Selenium testing tools cookbook - Google Search", driver.getTitle()); } @After public void tearDown() throws Exception { // Close the browser driver.quit(); } }
Execute this test and you will see the Internet Explorer window being launched and all the steps executed.
How it works...
Internet Explorer Driver Server is a stand-alone server executable that implements WebDriver's JSON-wire protocol, which works as a glue between the test script and Internet Explorer, as shown in following diagram:
The tests should specify the path of the IEDriverServer
executable before creating the instance of Internet Explorer. This is done by setting the webdriver.ie.driver
property as shown in following code:
System.setProperty("webdriver.ie.driver", "src/test/resources/drivers/IEDriverServer.exe");
Tip
We can also specify a path externally through the Dwebdriver.ie.driver
option using Maven command line options. In this case, we don't need to set this property in test.
Internet Explorer Driver Server supports automating major IE versions on Windows XP, Vista, Windows 7, and Windows 8 operating systems.
Note
For more information on
InternetExplorerDriver
, please visit https://code.google.com/p/selenium/wiki/InternetExplorerDriver.
We need to create an instance of the InternetExplorerDriver
class, which will connect to the Internet Explorer Driver Server to launch the Internet Explorer, shown as follows. It will then run the Selenium commands, which we will call by using various WebDriver
and WebElement
methods from the test script:
DesiredCapabilities caps = new DesiredCapabilities().internetExplorer(); caps.setCapability( InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); // Launch Internet Explorer driver = new InternetExplorerDriver(caps);
We used the DesiredCapabilities
class to set the INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS
capability, which defines to ignore the browser protected mode settings during the start by IEDriverServer.
There's more…
Selenium provides the ability to run tests on remote machines by using the RemoteWebDriver
class. We can configure any browser that Selenium supports for executing tests on a remote machine. To run tests on a remote machine, we need to run the Selenium Server and the Internet Explorer Driver Server on a remote machine and use RemoteWebDriverClass
, as shown in the following code sample:
DesiredCapabilities caps = new DesiredCapabilities().internetExplorer(); caps.setCapability( InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); // Connect with Remote Selenium Server with specified URL and capabilities driver = new RemoteWebDriver(new URL("http://192.168.1.3:4444/hub/wd"), caps);
We can connect any browser to a remote server using the preceding method.