Selenium Java to Advanced Framework

Chapter 2

Selenium Webdriver with Java

Robot class in Selenium Webdriver

Invoke ChromeDriver with Selenium Webdriver

Hello Welcome to Selenium Tutorial, today we will discuss Launch Chrome Browser using Selenium Webdriver.

Selenium Webdriver by default support Firefox browser, only that is the reason we did not face any issue while working with Firefox.In order to execute your script in the different browser like chrome, IE etc.

If you are working with IE browser then you should know the challenges as well which you will face while working with IE browser. This is one of the most important question in interviews as well.


We have to download separate drivers and we have to specify the path as well with the location of the driver.
Note- Selenium Webdriver supports chrome latest version.

You can refer complete YouTube video

Step 1: Download Chrome Drive

Open any browser and open http://www.seleniumhq.org/download/
Here you will get third party driver section and you can get all the external driver for different browsers. (For this navigate to Browser section, and click on documentation link of Chrome browser).

Note- Latest stable chrome version is 81.0.404469 so you will get the latest version

Image

Here you will get the driver zip file which you can extract, after extraction, you will get chromedrive.exe file

Note- Selenium provides only 32 bit but you can use the same for 64-bit machines as well. Practically I tried and it works fine

Launch Chrome Browser using Selenium Webdriver

Step 2 : Download Selenium jar files

From above seleniumhq link download selenium jar files. Please refer image below:

Image

When it is downloaded it will be in zip file, unzip it. When you unzip it you will see some executable jars both inside the lib file and outside the lib file as below.

Image
Outside ‘lib’ file
Image
Inside lib file

Step 3: Selenium first Project Creation

Once chrome & selenium jar files is downloaded, create a new java project

Inside this project create a package and then a class (make the ‘public static void main’ checked) inside the package. If you are not aware about creating project,package or class , please read my previous section (java basics section).

Once these are created right click on project – > Properties.

You will get a dialogue box. Goto Java Build Path-> Libraries -> Add External File (refer below)

Image
Image

Now when you click on Add External jars you have to add all selenium jar files that we have downloaded. (those inside ‘libs’ file and outside ‘libs’ file. If any one excutable jar is missed, it will show error while writing code or executing the code. So be carefull while adding the jar).

These jars are essential to bring selenium components into your project.

Program for Run Selenium Webdriver in chrome Browser

Now please see my below code to invoke a chrome browser and hit the url.


import org.openqa.selenium.WebDriver;
 
import org.openqa.selenium.chrome.ChromeDriver;
 
public class TestChrome {
 
public static void main(String[] args) {
 
System.setProperty("webdriver.chrome.driver", "path of the exe file\\chromedriver.exe");
 
// Initialize browser
WebDriver driver=new ChromeDriver();
 
// Open facebook
driver.get("http://www.facebook.com");
 
// Maximize browser
 
driver.manage().window().maximize();
 
}
 
}

While writing this code you will get some errors highlighted in red color. If you place your cursor on those lines, you will get suggestion to import some packages , when you click on that you will see the packages are getting added to your projects and error are gone. These suggestions are coming because you have added selenium jars. When writing your code please avoid first 2 lines like import statement, because this will automatically get populated when you import the jar while writing the code.

Image

In the first line inside static void main class, you will see System.setProperty.In Java to set variable we use setProperty method of System class so let us add the same in our program. Inorder to use chrome driver, to the property “webdriver.chrome.driver” we have to give the value ie, the path of chromedriver where you have downloaded. For eg, if downloaded in C Disk, give us “C:\\chromedriver.exe” or if inside downloads “C:\\Users\\admin\\Downloads\\chromedriver.exe”. Always remember to give // between the path or it will show you error. If you deosn’t give this property:

Your test case will fail and you will get IllegalStateException which says we need to specify the chrome driver path where it resides.

If you notice Selenium also gives a very meaningful message that we need to add some chrome variable also while running the script.

Variable name is – webdriver.chrome.driver

Now you can see the line WebDriver driver=new ChromeDriver();

Here we are creating an instance for class Webdriver, called driver. Webdriver is as inbuilt class which you get while adding selenium jar, for driver related activities. This instance ie, driver is assigned to ChromeDriver class of selenium. ChromeDriver is another inbuilt selenium class for invoking chrome browser. Thus selenium knows that chromedriver is the driver we have to intiate with Selenium Webdriver.

Now when you call this chrome browser will be invoked. To perform other actions on this chrome browser you invoked you can use the instance you created that is ‘driver’.

So to hit the URL you required just type : driver.get(“http://www.facebook.com”);

This will open Facebook login page to you.

Hope you liked this section

Next we will see how to interact with page we opened in Browser. (Go to next page).

Leave a comment