Running Selenium tests on Internet Explorer Browser


The main advantage of Selenium testing is the cross browser testing. So test once written for a browser, can be run on other supported browser as well (off course with little changes :-) ).

Let's see how we can run selenium java test on Internet Explorer browser. For this we need - IEDriverServer.exe (it can be downloaded from here): This is Internet Explorer driver server which is needed by Selenium to execute tests on Internet Explorer browser.

We should first follow these steps to make sure that IE is setup correctly so that it can be handled by Selenium properly. 

In our script, we will be explicitly mentioning which IEDriverServer.exe to use to run tests. Below is a simple test we can run to understand what it takes to run a test on Internet Explorer.

Basic steps for creating script:
  1. Create a TestNG class in eclipse
  2. While creating TestNG class, select these annotations - @BeforeMethod, @AfterMethod
  3. Code for the script would as given below – Click on code to use Arrow keys from keyboard for horizontal/vertical scrolling.

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;

public class IEDriverTest {
   WebDriver driver = null;
   String userName = "mercury";
   String password = "mercury";

   @Test
   public void f() {
      driver.findElement(By.xpath(".//*[@name = 'userName']")).sendKeys(userName);
      driver.findElement(By.xpath(".//*[@name = 'password']")).sendKeys(password);
      driver.findElement(By.xpath(".//*[@name='login']")).click();
   }
   @BeforeMethod
   public void beforeMethod() {
      System.setProperty("webdriver.ie.driver",
      "D:\\path\\to\\IEDriver\\IEDriverServer.exe");
      DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
      capabilities.setCapability("requireWindowFocus", true);
      driver = new InternetExplorerDriver(capabilities);
      driver.get("http://newtours.demoaut.com/");
      driver.manage().window().maximize();
   }

   @AfterMethod
   public void afterMethod() {
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.quit();
   }

}

Output:
Once you run the script, it will open Firefox browser and navigate to Mercury Travels site and login to the site. After this it will wait for 10 seconds and then browser will get closed.

Script Code Explanation:
Let's understand what logic is implemented in beforeMethod() function. This is were main action is taking place. In order to use internet explorer as our browser for this test, we have to explicitly mention in the code that use IEDriverServer.exe. We accomplish this by setting "webdriver.ie.driver"property by using System class method setProperty().


System.setProperty("webdriver.ie.driver",
   "D:\\path\\to\\IEDriver\\IEDriverServer.exe");

Next, we set capabilities of Internet Explorer browser, by using below code -

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability("requireWindowFocus", true);

As per Jim Evans blog who is Selenium Project Internet Explorer Driver Maintainer, it is highly discouraged to use INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS capability, you can find details about this here.

We need to set these capabilities because Internet Explorer browser has two version present  - 32-bit and 64-bit. For this reason IEDriverServer.exe also has two versions - 32-bit and 64-bit versions.

How to make sure which version of IE is present on your machine: Run IE, go to task manager and in Processes tab, check iexplore.exe process - if process name is like iexplore.exe *32 then your IE is 32-bit.

We have to make sure that we match versions of IE and IEDriverServer.exe otherwise we will run into snail typing issue. Now what is this snail typing issue - selenium inputs data into textboxes at very slow rate, it inputs a character and waits for 3-4 seconds to input next and so on. So if we mix up the versions then selenium tests run very very slow on IE.
Notes:
  1. It would be better approach to match the versions of IE and IEDriverServer.exe files.
  2. You can find above selenium test on my GitHub project and you can fork the repository to make your own changes to the code.
Don't forget to put comment about your doubts/questions/feedback!
Enjoy and Happy Testing!!

Comments

Popular posts from this blog

Simple Data Driven Framework script

Reading and writting data from .xlsx spreadsheet using Apache POI API

Generalized Apache POI script for reading and writing to .xlsx files