Open In App

How to Run Gecko Driver in Selenium Using Java?

Improve
Improve
Like Article
Like
Save
Share
Report

Selenium is a well-known software used for software testing purposes. Selenium is consists of three parts. One is Selenium IDE, one is Selenium Webdriver & the last one is Selenium Grid. Among these Selenium Webdriver is the most important one. Using Webdriver, online website testing can be done. There are three main web drivers present. For the Chrome browser, ChromeDriver is present. For the Firefox browser, Gecko Driver is applicable. And for Microsoft Edge, there will be MSEdgeDriver present. In this article, the process of running Gecko Webdriver is implemented. This simple Java program can be run.

Pre-Requisites:

  1. For running GeckoDriver, the Java jdk version must be installed in the machine previously.
  2. The latest version of Firefox should be installed.
  3. It is preferable to install Eclipse IDE on the machine so that running this code will be easier.
  4. The most important prerequisite is latest GeckoDriver should be downloaded on the machine.

Approach

  • Here, using GeckoDriver, the home page of Google is going to open. For, that some methods need to be imported.
  • First, the Google home page link is to be stored in a string.
  • Then in the program, the property of the browser is to be set. setProperty() method is going to be used here.
  • In the setProperty() method, the first argument is to be the Webdriver, which is to be going to use. Here, using GeckoDriver specifically that argument have to be passed. And in the second argument, the location of the GeckoDriver.exe is to be passed.

Note: In this case, GeckoDriver.exe is stored in Eclipse, so maybe the location seems different. But also, a complete File Explorer path can also be passed.

  • Then a new object called driver should be implemented which is a type of WebDriver. Here, in this case, it will be GeckoDriver.
  • Then using that driver object, the get() method will be used. This get() method of WebDrivers helps to open some URLs provided. Here the home page of Google is going to be opened. So, only the string where the URL has been stored will be passed. Executing this method will go to open a new Firefox window.
  • Then the sleep() method is going to be implemented. This delays the programs for some time. So that the output can be visible easily.
  • At last, the opened Firefox window has to be closed. For that reason, quit() method is going to be implemented.

Below is the complete implementation of the above approach:

Java




// Importing All Necessary Items
import java.io.*;
import java.lang.Thread;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
 
public class FirefoxHomePage {
    public static void main(String[] args)
    {
        // Try-Catch Block For Implementing Sleep Method
        try {
            // String Where Home Page URL Is Stored
            String baseUrl = "https://www.google.com/";
           
            // Implementation of SetProperty Method
            System.setProperty(
                "webdriver.gecko.driver",
                "test/resources/geckodriver.exe");
           
            // Creating New Object driver Of Webdriver
            WebDriver driver = new FirefoxDriver();
           
            // Calling the Home Page By Using Get() Method
            driver.get(baseUrl);
           
            // Delaying The Output
            Thread.sleep(2000);
           
            // Closing The Opened Window
            driver.quit();
        }
        catch (Exception e) {
            // Catching The Exception
            System.out.println(e);
        }
    }
}


Output:

If the above code is run, then a new Firefox window will be opened. This open window will be controlled by GeckoDriver.exe. In this new window, a golden color stripe can be visible in the search bar of Firefox.

Output

 

Hence, the program runs successfully. 



Last Updated : 14 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads