Thursday, October 4, 2018

First WebDriver Script on Firefox browser : Selenium WebDriver tutorial

Hi guys, In this post, I am going to discuss how to execute first selenium script in Firefox.


first selenium script in firefox


 Pre-requisite

  • Make sure you have already installed Eclipse IDE.
  • Make sure you have already configured WebDriver-jar file with your java project.
  • Make sure you have downloaded the latest version of WebDriver-jar file. Otherwise, you will get a compiler error.
  • Make sure you have downloaded the driver for Firefox. I will show you below how to do it.

Note - If you don't know how to configureWebDriver-jar with your java project, then refer this link -  ConfigureWebDriver-jar with your java project


Package?

It is a collection of classes, sub-packages, and Interfaces
Suppose, you have declared a package called com.facebook and under this, you created one class called Facebook, where you have defined one method login() to login to Facebook. Now, you have created one more package called com.test under whichyou created one class called TestApplication. Now you want to write one method that will click on the 'Home' tab of the Facebook application. Definitely, you have to log in first then only you can go to the 'Home' tab and click on it. So, what you can do you can just import com.facebook package and create one object of the Facebook class and with the help of this object, you can simply call the method login(). Once you logged in,  you can reach to 'Home' tab and click on it. 

This is what you will do most of the time while creating Selenium script. We will import packages which contain some specific classes to use their methods in the script.

How to run Selenium WebDriver script in Firefox browser


Point to note - 

  • If your browser version is 47+, then you have to download 'gecko driver'. If it is below 47,  then no need to download.
Version above 47  
1. Open Eclipse.
2. Create one Java Project for example SeleniumTutorial.

3. Right click on SeleniumTutorial and navigate as shown below - 



4. Give package name for example com.selenium and click on Finish.
5. In the same way, right click on com.selenium and create one class for example SeleniumTutorial.



6. Finally, your class will be created and will be shown as - 



How to download geckoDriver?


 Refer this link download gecko driver
  • Download the latest version zip file based on 32-bit or 64-bit supported by your PC and also based on your OS. 



  • I am using window 64 bit. Open the downloaded file and extract it. It will look like


  • Open the extracted folder and copy the path of the file. We are going to use it in our program.

Now, we will start writing script


1. Under the main() function write  -
 -  System.setProperty("webdriver.gecko.driver", "F:\software\firefox\geckodriver-v0.21.0-win64");

System.setProperty has two parameters -

  • First parameter - We pass the name of the driver.
  • Second parameter - We pass the path of the extracted jar file. 




Note - If you notice you are getting an error. The reason is that Eclipse IDE does not understand single '\'. So, we have to provide '\\' and append the provided path with the file name with .exe extension as shown below -




Now, you won't get any error.


2.  Next, Write below statement -

- WebDriver driver = new FirefoxDriver();

Here, 'driver' is the reference variable of WebDriver Interface. An object of FirefoxDriver class has been created and reference to the object is assigned to 'driver'. Mozilla provides 'GeckoDriver' to control Firefox browser while automating. Since the reference to the object of FirefoxDriver is assigned to 'driver', now 'driver' has the control over the Firefox browser.

Note - You will get two errors. We have used an Interface and a Class and as discussed above, an Interface and Class implementations reside in a particular package, we need to import those respective packages. If you hover over where the errors are showing, you will be suggested with some actions to perform. what you need to do is to import below two packages -

  • org.openqa.selenium.WebDriver;
  • org.openqa.selenium.ChromeDriver;

3. We will open the Google homepage.
-  driver.get("https://www.google.com");

Note:- get() - command is used to open a specific URL in the opened browser.


4. Now, we will close the browser.
driver.quit();


Complete Code -
 package com.selenium;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class SeleniumTutorial {  
  public static void main(String args[])  
  {  
  System.setProperty("webdriver.gecko.driver", "F:\\software\\firefox\\geckodriver-v0.21.0-win64\\geckodriver.exe");  
  WebDriver driver = new FirefoxDriver();  
  driver.get("https://www.google.com/");  
  driver.quit();  
  }  
 }  


Version below 47


You don't need to download and set the path of GeckoDriver in your program. Simply remove System.setProperty() from the above program and execute it. It will run.

That all guys!!! 

In my next post, I will discuss how to create selenium script to execute in chrome.


Do comment in the comment box and share your views about the post. If you have any query, please comment on it. It will help me to reach to you and help you to resolve your query.



No comments:

Post a Comment