How To Use Firefox Webdriver To Launch Firefox Browser Automatically In Java

This article will tell you how to use Firefox webdriver to initiate a Firefox browser and implement basic browse page actions with selenium.

1. How To Automate Firefox Web Browser Use Selenium Webdriver In Java.

1.1 Create The Java Class FirefoxBasic.

  1. Create an eclipse maven project and add selenium dependency in the pom.xml to import selenium webdriver jar files into the maven project. You can read the articles listed in the references section of this article to learn more.
  2. The selenium Firefox driver jar file name is something like selenium-firefox-driver-3.141.59.
  3. Right-click the eclipse maven project name, click the New —> Class menu item in the popup menu to open the New Java Class dialog window.
  4. Input com.dev2qa.webdriver.firefox in the Package textbox, input FirefoxBasic in the class Name textbox, check the public static void main(String[] args) checkbox.
  5. Click the Finish button to create the java class.
  6. Below is the eclipse maven project file structure.
    > tree /F ./
    │  .classpath
    │  .gitignore
    │  .project
    │  pom.xml
    │
    ├─.settings
    │      org.eclipse.jdt.core.prefs
    │      org.eclipse.m2e.core.prefs
    │
    ├─src
    │  ├─main
    │  │  ├─java
    │  │  │  └─com
    │  │  │      └─dev2qa
    │  │  │          └─webdriver
    │  │  │              └─firefox
    │  │  │                      FirefoxBasic.java
    │  │  │
    │  │  └─resources
    │  └─test
    │      ├─java
    │      └─resources

1.2 Use Firefox WebDriver To Automate Firefox Web Browser.

  1. If you use the below java source code to start the Firefox web browser with selenium webdriver in selenium 3.
    WebDriver ffDriver = new FirefoxDriver();
  2. You will get the error The path to the driver executable must be set by the webdriver.gecko.driver system property like below.
    java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
        at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
        at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)
        at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:44)
        at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:167)
        at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
        at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:190)
        at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:147)
        at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)
        at com.dev2qa.webdriver.firefox.FirefoxBasic.StartFirefox(FirefoxBasic.java:14)
        at com.dev2qa.webdriver.firefox.FirefoxBasic.main(FirefoxBasic.java:38)
    
  3. This is because selenium 3 add some new features such as use Mozilla’s geckodriver to support the Firefox web browser.
  4. To fix this error, you should first download the geckodriver from the mozilla/geckodriver link.
  5. Then extract the zip file to a local folder such as D:\Work\Tool\geckodriver-v0.29.1-win64\geckodriver.exe.
  6. Set the above geckodriver.exe file path to the webdriver.gecko.driver system property value to the os system environment variable PATH value before starting the Firefox web browser.
    // Set the geckodriver.exe path to the system property webdriver.gecko.driver's value.
    System.setProperty("webdriver.gecko.driver", "D:\\Work\\Tool\\geckodriver-v0.29.1-win64\\geckodriver.exe");
            
    //Create Firefox webdriver instance and launch Firefox browser. 
    WebDriver ffDriver = new FirefoxDriver();
  7.  Now you can use the FirefoxDriver object to browse web page by URL.
    //Maxmize Firefox browser window.
    ffDriver.manage().window().maximize();
                
    //Browse google home page.
    ffDriver.get("https://www.google.com");
  8. Below is the full source code for this example. Right-click the source code in the eclipse editor, click Run As —> Java Application menu item to run it.
    package com.dev2qa.webdriver.firefox;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class FirefoxBasic {
        
        
        public static void StartFirefox() {
            
            try {
                
                // Set the geckodriver.exe path to the system property webdriver.gecko.driver's value.
                System.setProperty("webdriver.gecko.driver", "D:\\Work\\Tool\\geckodriver-v0.29.1-win64\\geckodriver.exe");
            
                //Create Firefox webdriver instance and launch Firefox browser. 
                WebDriver ffDriver = new FirefoxDriver(); 
                
                //Maxmize Firefox browser window 
                ffDriver.manage().window().maximize();
                
                //Browse google home page.
                ffDriver.get("https://www.google.com");
        
                //Sleep 2 seconds to wait for the page load complete
                Thread.sleep(2000);
                
                // Get the google search box object by the search box name.
                WebElement searchBox = ffDriver.findElement(By.name("q"));
                // Input search keyword text in the google search box.
                searchBox.sendKeys("selenium");
                
                // Press the keyboard Enter key to close the search tips drop-down list and search.
                searchBox.sendKeys(Keys.ENTER);
            
                // Get the google search button by the search button name.
                //WebElement searchBtn = ffDriver.findElement(By.name("btnK"));
                // Click the search button to search.
                //searchBtn.click();
        
                //Print out action success message.
                System.out.println("Open website success.");
                
                //Sleep 10 seconds to wait for the page load complete
                Thread.sleep(1000);
                
                
                // Quit and close the Firefox web browser.
                ffDriver.quit();
            }catch(Exception ex){
                
                ex.printStackTrace();
            }
        }
        
    
        public static void main(String[] args) {
    
            FirefoxBasic.StartFirefox();
    
        }
    
    }
    
  9. You can read the article How To Find Web Element Using Web Browser Inspector to learn how to get the google search box and search button object name.

References

  1. How To Create Maven Project In Eclipse
  2. How To Add Selenium Server Standalone Jar File Into Your Java Project
  3. How To Run Test Cases Using Apple Safari In Selenium Automation Script.
  4. How To Run Microsoft Internet Explorer And Edge In Selenium Webdriver Automation Script.
  5. How To Run Test Cases Using Google Chrome In Selenium Automation Script.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.