Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Working with different browsers in selenium - Testingpool

Working with different browsers in selenium

In the previous post, we have seen about Selenium WebDriver and its comparison with Selenium RC. In this post, we will show how to work with different browsers in selenium.

We will see working of following browsers.

  • Mozilla Firefox
  • Chrome
  • Internet Explorer
  • Safari on Mac

browser in selenium

The important point to note is that we have to keep in mind the browser compatibility with Selenium WebDriver. Now, the WebDriver latest version is 2.47 which supports Firefox version 39, Internet Explorer 11, Chrome 44 and Safari 5.1 +. Sometimes, we get surprised why selenium is not working with browser, it may be selenium does not support that browser version.

Prerequisite:

It is assumed that you have already installed and configure selenium with Eclipse and created a project(Explained here.) Next is to create a class into your project where you will write the code.For creating the class, right click on the source folder and choose option ‘class’.

Create class in eclipse

Fig 1.0

A new window will open, where you can give package name and class name. Check checkbox ‘public static void main(String[] args)’ and click finish. If you want to know about what is ‘public static void main(String[] args)’,Click here.

Class name

Fig 1.1


 

How to work with Firefox driver?:

You need to create the object of class ‘FirefoxDriver’. We can create the object the way as shown below.

FirefoxDriver driver = new FirefoxDriver(); //Creating object for Firefox driver

But the recommended way would be to create object with reference of WebDriver interface. This would help if you want to create instance of any other browser like chrome, IE to the same object(e.g. driver) in the current class.

WebDriver driver = new FirefoxDriver();

Code for Opening Firefox Browser ( Selenium version less than 3.0 ):

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenBrowsers {
	private static WebDriver driver;
	public static void main(String[] args) {
		//Open the firefox browser
		 driver = new FirefoxDriver();		
	}
}

To run the class ” Right Click on the class –> Run As –> Java Application”.

Result: Firefox browser should open.

Code for Opening Firefox Browser ( Selenium version  3.0 or above ):

If you are working with Selenium 3.0 and above, the above code will not work for you and will fail with the exception java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property;. In that case, You will need ‘Gecko driver’ which helps Selenium to interact with Firefox.

What is GeckoDriver?

Let’s first understand , what is Gecko? Gecko is nothing but a browser engine developed by Mozilla. Firefox browser uses it.

Geckodriver is designed to drive gecko-based web browser, like Mozilla Firefox. Selenium 3 enabled geckodriver as the default WebDriver implementation for Firefox. With release of Firefox 47, FirefoxDriver has stopped supporting Selenium interaction. So, GeckoDriver has come into picture.

How to Use GeckoDriver?

GeckoDriver is an executable(exe) file which you can download as shown below.

  1. Go to Selenium download page – Click here
  2. Scroll down to Mozilla GeckoDriver link and click on the Latest

 

 

 

 

 

 

3. You will be redirected to the geckoDriver releases page where you can download the GeckoDriver file.

4. Unzip the file and store it at any location.

5. Now, you need to configure the Geckodriver location in system properties as shown below and then launch the Firefox.

6. You have to set the Geckodriver path against “webdriver.gecko.driver”.

System.setProperty("webdriver.gecko.driver", "C:\\selenium\\drivers");

WebDriver driver = new FirefoxDriver();

Another way to use GeckoDriver (By setting the Environment variable):

We can configure the GeckoDriver location under System’s Environment variable as shown below.

  1. Right click on ‘This PC’ and select properties

         2. Click on ‘Advance System Settings’. A ‘System Properties’ window will open.

 

3. Click on ‘Environement Variables..’ button. It will open ‘Environment variables’ window. We need to select variable named ‘Path’ and edit it with location details of GeckoDriver (be very careful while editing it)

In my case, it is located at ‘C:\Selenium\Drivers’.

 

4. Click OK on all opened windows.

Interesting part is that, now we don’t need to write system properties in code. We can directly launch Firefox with FirefoxDriver statement.

WebDriver driver = new FirefoxDriver();

 


 

How to open Chrome browser?

Before creating the object for ChromeDriver , we need to download one executable driver file for chrome driver. You can download the latest version of executable file for chrome driver here. It will download  a zip file.

Chrome driverChrome driver exe

Extract the zip file, we need to set the path of chrome executable driver in to system properties. The best place to keep executable driver will be at your project path. It will be helpful when you want to export your project to some other system or location, you don’t need to worry about attaching the executable driver again and again.

chrome driver at project path

Note: Chrome executable driver should be placed under the project.

Need to set the path at system property:

System.setProperty(“webdriver.chrome.driver”, System.getProperty(“user.dir”)+”\\chromedriver.exe”);

Explanation of above statement: 

  • System.setPrperty : It is used to set the path of executable driver. It is in the form of key and value where the key is ‘webdriver.chrome.driver’ and value is the path of driver.
  • System.getProperty(“user.dir”) : It will return the path where the current project lies. ‘user.dir’ is the unix command which refers current directory.

Object of ChromeDriver:

WebDriver driver = new ChromeDriver();

Code for opening Chrome browser:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenBrowsers {
	private static WebDriver driver;
	public static void main(String[] args) {
		
		System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\chromedriver.exe");
		//Open the Chrome browser
		 driver = new ChromeDriver();		
	}
}

To run the class ” Right Click on the class –> Run As –> Java Application”.

Result: Chrome browser should open.


 

How to open Internet Explorer Browser?

As we have downloaded chrome executable driver, same way we need to download for internet explorer. You can download it here  as per you browser 64 bit or 32 bit.

Internet explorer driver

After download , extract the zip file and place it at project level as we did for chrome driver. We also need to set the path of Internet explorer executable driver to the System property.

Internet explorer set property

Set the System Property :

System.setProperty(“webdriver.ie.driver”, System.getProperty(“user.dir”)+”\\IEDriverServer.exe”);

Object for Internet Explorer Driver:

WebDriver driver = new InternetExplorerDriver();

Code for opening Internet Explorer browser:

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

public class OpenBrowsers {
	private static WebDriver driver;
	public static void main(String[] args) {
		
		System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\IEDriverServer.exe");
		//Open the IE browser
		 driver = new InternetExplorerDriver();		
	}
}

To run the class ” Right Click on the class –> Run As –> Java Application”.

Result: Internet Explorer browser should open.


 

How to open Safari browser on Mac OS?

I have created a video for this where step by step procedure is given to work with safari on MAC.


Ask Question
Have any question or suggestion for us?Please feel free to post in  Q&A Forum

 

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

3 Responses

  1. Dan says:

    Hi…

    Great information.

    I had a question. What is the difference between the IE WebDriver and Selenium core jar files? (2) Are they both considered Selenium? (3) What is the difference between WebDriver and the Selenium classes provided in the JARs?

    Thank you!

    • In Simple terms, WebDriver is an Interface,and we are defining a reference variable(driver) whose type is an interface. An object that is assigned to it, must be an instance of a class(InternetExplorerDriver) that implements the interface to use its methods etc.

  1. August 16, 2015

    […] the previous post, we have seen how to work with different browsers in selenium. In this post,we will see after opening browser how to open the application into that opened […]