In last post, we have done installation of JAVA and Eclipse. We have downloaded Selenium standalone jar file as well. Now, we will learn

  1. Setup of a java project in eclipse with selenium.
  2. Opening a chrome browser and perform some basic actions.

First we do little setup of eclipse.

Step 1: Create a folder(work space in terms of eclipse) in your local system and name it say “MakeSeleniumEasyLearnings”.

Step 2: Open Eclipse. It will ask for location of work space. Select the location of newly created work space in step 1. After completing of step 2, you will get eclipse window as below. You can notice work space name in eclipse header.

BlankWorkSpace

Step 3: Navigate to File->New->Project…

new project

Step 4: Select Java Project and click on Next.

selectjavaproject

Step 5: Give the project name and click on Finish. Note here that even you have installed JDK 8 , but you can easily select any JRE version to work for. It is required when any java program has been developed using old versions of java and for better result, you want to execute with properly supported java version only. You can change java version form here any time.

javaprojectname

Step 6: Now expand the project. Do right click on src and navigate to New->Package.

javaPackaage

Step 7: Give package name as “browserExamples” and click on Finish. You will see a package is created under source.

Concept: Package is used in Java to group related classes.

packagename

Step 8: Do right click on project and navigate to New->Folder. And give the folder name as jarfiles.

jarfiles

jarfilesfolder

Step 9: Copy the “selenium-server-standalone-3.3.1.jar” which we have downloaded and paste it by right clicking on jarfiles folder.

seleniumjarfiles

Step 10: Since, we have added an external jar files. So to work with it, we must add this jar file to java build path. We can do this by right click on jar file -> Build Path- > Add to build path.

You will notice after adding in to build path, selenium jar file will be shown under Referenced libraries.

addtobuildpath.png

So, above steps are required to run out first selenium script.

We know, selenium automates browser. So first step should be opening a browser. In this post, now we will learn:

  1. How to open Google Chrome browser.
  2. Open a URL which is used mostly to check internet connection is working or not. :-p
  3. Close the browser.

Let’s start:

Step 1: Right click on package named “browserExample” and navigate to New->Class. Give the class name as “OpeningChromeBrowser”. Check the check box of “public static void main(String args[]). Click on Finish.

class.png

classname.png

Step 2:

Concept:

  1. Selenium provides a driver class for each browser to allow programmer to work on it. For example, for chrome we have chrome driver, firefox driver for firefox etc. So, to open a browser we need to use that browser driver class. Here we need to open chrome browser so we will use chrome driver.
  2. Every browser driver is a class and when we create an object of that driver class, it will open that browser window during execution.
  3. We create an object of a class using new keyword with constructor.
  4. Execution of java program starts from main method.
  5. We use semicolon to end a java statement.

Now write below code inside main method:

ChromeDriver driver = new ChromeDriver();

When you write this statement, you will get red lines on ChromeDriver. It is because, we have not imported the class from package. To do so, just mouse hover on ChromeDriver and select import from as shown below:

import.png

Step 3:

Concept:

  1. To open any URL, we use a method called get(String url).
  2. Pass URL with http or https otherwise you will get exception.(Will discuss later).

Now write below code:

driver.get(“https://www.google.com”);

Step 4:

Concept:

  1. We need to close the browser after opening URL. For this we can use close() or quit() methods. As of now, we will use quit() method. Difference between these two, we will learn later.

Now write below line of code:

driver.quit();

So final code after step 3 will be as below:

package browserExamples;

import org.openqa.selenium.chrome.ChromeDriver;

public class OpeningChromeBrowser {

public static void main(String[] args) {

ChromeDriver driver = new ChromeDriver();
driver.get(“https://www.google.com”);
driver.close();

}

}

Step 4: Run the java program. Right click on program and navigate to Run As-> Java Application. We are expecting that our program will perform desired operations.

runas.png

exception.png

But ye kya ho gya??? It is giving Exception.

Exception is:

Exception in thread “main” java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:111)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:302)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:124)
at browserExamples.OpeningChromeBrowser.main(OpeningChromeBrowser.java:9)

Solution of handling above exception:

I hope, you remember architecture of selenium webdriver. A java program can not directly communicate to a browser. It needs executable file(driver as explained in selenium architecture)  of that browser. Browser executable file is given by browser itself. You just need to download it and use it. Solution of above exception is given in exception only.

The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html

Go to URL http://chromedriver.storage.googleapis.com/index.html and download latest version of chrome executable file. Note here that executable file is also platform dependent. So download correct file. I will download for windows. Chrome executable file will work for both 32 bits and 64 bits machine.

chromeexe.png

chromeexewin.png

It will be in zip format. You need to extract it to get .exe file.

Now, Do right click on project and navigate to New->Folder. And give the folder name as exefiles. Note here I am using folder system to categorized files. It is be useful while developing framework.

Now copy the chromedriver.exe file and paste in to exefiles folder.

Next, as per above exception, we need to set system property as well which can be done using below line of code:

System.setProperty(“webdriver.chrome.driver”, “./exefiles/chromedriver.exe”);

Concept:

  1. System is a java class.
  2. setProperty is a method which can be used to set property value at system level. It works as key value pair.
  3. Key you can see in exception you got above. generally it is “webdriver.<brosername>.driver.
  4. Value is path of browser executable. You can give relative or absolute path .

We are done now. Run the program and see kuch hota bhi hai ki ni abki baar.

Yes!! We did this time. I ran first time without setting property to make you understand the  architecture of selenium webdriver. Whenever you will get this exception in future, you can figure out your mistake quickly.

It is a very long post, but I hope you would have understand well. Kindly comment or mail me if you face any problem or have any suggestion to improve quality of my posts.

Thank you.

 

 

 

 

 

 

 

 

6 thoughts on “Setup of selenium project and How to open a chrome browser

Leave a Reply