Home » Testing Tutorial » Selenium Tutorial » Running Selenium Test on Internet Explorer Browser

Running Selenium Test on Internet Explorer Browser


Reading Time - 11 mins

Introduction

Selenium is a multi-browser automation tool. Hence, like Firefox and Chrome browsers, Selenium automates on the Internet Explorer browser too.  Therefore, the same Selenium code works on all browsers. The browser-specific browser drivers help in achieving this feature of Selenium. To sum up, different browser-specific drivers run the Selenium script, on different browsers. In this tutorial, we will learn how to execute the Selenium code on the IE browser.

We will use our First Selenium Test case script to run on the IE browser to keep it simple as our objective right now is to learn how to execute Selenium automation on the IE browser.

Pre-requisites

The prerequisites to execute tests on IE browser tutorial are:

In case you have missed any of the above set up, please refer to the individual tutorials available in our course for them.

Set up for the IE Browser

IE Browser was the most widely accepted browser by Microsoft until Firefox and Chrome came into the picture. It is commonly used on Windows machines for web applications. Let us learn how we can run Selenium code on the IE browser.  To invoke the IE browser, we need to assign our WebDriver driver object as IEDriver.

WebDriver driver = new InternetExplorerDriver();

We need to inlcude the org.openqa.selenium.ie package to resolve the compilation error.

READ  Running with Mobile User agents

After including the import statement, if you run your script and it does not find the IE driver, it would throw the java.lang.IllegalStateException as shown in the image below:

Note: If you have not yet executed the script, you can do so by right-clicking on the java class > Run As > Java Application.

The error clearly says that path to driver executable should be set. Hence, to execute the Selenium script on the IE browser, we need to set the path to IE driver executable. We will perform the following steps in this tutorial:

  • Download IE driver
  • Set the Path variable or set the system property in the script
  • Execute the code

Download IE driver  

  1. First, check the version of the IE browser installed in your machine. You can do so by Settings > About InternetExplorer. It will show the current version installed.

2. The next step is, to download the IE browser driver. For that, we need to go to the download section of the Selenium official site https://docs.seleniumhq.org/download/. Click on the appropriate link as per the windows configuration.

3. For windows, it will download a zip file IEDriverServer_xnn_n.n.n where n stands for version information. Save this zip file to an appropriate location.

4. Extract the content of the saved zip file in a folder and you will get IE driver executable.

Now after we have downloaded the IE driver executable successfully, we can tell the Selenium script where to locate it. There are two methods of doing it.

  1. Set the Path variable to point to the driver location in the system. I have stored the IE driver at C:\softwares\drivers in my system. So I can set Path = C:\softwares\drivers and my Selenium script will be able to locate it without any code changes in the script. This method is demonstrated in detail in First Selenium Test case tutorial. Please refer to this tutorial if you wish to follow this first method.
  2. Set the driver location in the Selenium script using System.setProperty() method. You can also do the code changes in the Selenium test script to point to the location of the driver executable. This method is demonstrated below:
READ  Configure Eclipse with WebDriver

Code Changes

For the second method, we will set a system property “webdriver.IE.driver” which points to the location of IE driver executable.

//Set the system property for IE driver
System.setProperty(“webdriver.ie.driver”,”C:\\softwares\\drivers\\IEDriverServer.exe”);

package seleniumAutomationTests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyFirstTest {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    //Set the system property for IE driver
    System.setProperty("webdriver.ie.driver","C:\\softwares\\drivers\\IEDriverServer.exe");
          
    //declare instance of WebDriver and run using IEdriver
    WebDriver driver = new InternetExplorerDriver();
        
    //load the webpage of application under test
    driver.get("https://www.facebook.com");
    
    //get the title of the page in a string variable
    String pageTitle = driver.getTitle();
    
    //print the page title on console
    System.out.println(pageTitle);
    
    //close the browser
    driver.close();

  }

}

Execution

To execute the script, right-click on the java class name and go to Run As > Java Application. This will start the execution of the automation script. It will

  • IE driver will start the IE browser.
  • Launch the application under test on the browser.
  • Fetch the value of the page title of the web page and store it in a string.
  • Print the page title on the console
  • Close the web page and finish the execution of the script.

Checking the output 

When we start the execution of our script, the application under test will be started in a new IE browser window. It will perform the commands provided in the script, in our case fetch the page title and the window will be closed by driver.close() method.

We can observe the output of the script on the console window at the bottom of the Eclipse IDE. It will print the page title here.

READ  The First Selenium test case

 

Common exceptions encountered:

  1. IllegalStateException: It is thrown when the path to driver executable is not set. Resolve it either by providing the location of IE driver executable using System.setProperty or by setting the Path environment variable.
  2. Browser zoom level was set to x: It is thrown when the IE windows zoom level is set to any value other than 100%. It can be resolved by going to settings and making the zoom level 100%.
  3. Unexpected error launching Internet Explorer. Protected Mode must be set to the same value: To resolve this error, we need to set the security settings to the same value in the Internet options.

With Internet Explorer, we face several new challenges as compared to other browsers like Chrome and Firefox. We have covered the issues faced with the IE browser in a separate tutorial on Challenges with IE browser.

Conclusion

In this tutorial, we learned about how to set up the IE driver for executing Selenium code on the IE browser. Next, we will see what are the several issues we face while automating on the IE browser using Selenium and their resolutions.

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions