TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: How to Run Selenium Tests in Firefox
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Selenium Tutorial

How to Run Selenium Tests in Firefox

Last updated: Jun 02, 2024 11:31 am
By Meenakshi Agarwal
Share
8 Min Read
Selenium 3 Project for Firefox using Geckodriver
SHARE

Presenting today is the step-by-step tutorial for creating a Selenium 3 project to run UI automation tests in Firefox using Geckodriver. Let’s begin the interactive tutorial with a brief overview of Selenium 3 and the Geckodriver.

Contents
What is GeckoDriver and how does it work?How to move from Selenium 2.0 to Selenium 3.0?1. Use system property to specify the Geckodriver path.2. Add a persistent environment variable to set the Geckodriver path.3. Add the Geckodriver path via the browser’s desired capabilities.TestNG Example – Launch Firefox using Geckodriver.Summary – Selenium Project for Firefox using Geckodriver

Selenium is the most favored UI automation tool for QA engineers. And it is continuously evolving since its inception in 2004. So far, Selenium 2 which introduced the WebDriver interface was the most famous version as it added native browser automation support.

Now, we have Selenium 3 recently launched with a list of new features. And, in this post, we’ll cover how to create a Selenium 3 project for Firefox using Geckodriver in Java.

Introduction to GeckoDriver

In Selenium 3, one of the major changes is that you can no longer access Firefox directly from the code. Instead, like the Chrome driver, now there is the new Gecko driver which you need to use for Firefox. So, it’s important to learn about the Geckodriver.

Please note if you are preparing for a Selenium testing interview, you must go through the following 35 Selenium interview questions at least once.

Selenium 3 Project for Firefox using Geckodriver

What is GeckoDriver and how does it work?

It’s a composite term that combines Gecko and Driver. Gecko is a proprietary web browser engine designed and developed by Mozilla.

GeckoDriver is the component that facilitates Selenium 3 tests to run in the Firefox browser. So basically, it replaced the default FF driver implementation in Selenium 3. It aimed to avoid compatibility issues that you might have seen with Selenium 2 and new versions of Firefox. Now Mozilla will also have to update the Geckodriver with every new release of its browser.

GeckoDriver in its physical form is an executable program that starts a server to accept commands from Selenium 3 tests. It uses the Marionette automation protocol to communicate with the browser by acting as a proxy. Hence, we may call it Marionette driver as well.

Note – There are several changes made to the Selenium 3 which you should check. Refer to Selenium 3 Changes.

How to move from Selenium 2.0 to Selenium 3.0?

First of all, you would need to download the latest version of the Selenium 3.0 package. And then, you will have to replace the Selenium 2.0 references with 3.0 libs under your project in Eclipse.

But there are some more steps you need to do. Because if you try to run the application, then it will give java.lang.IllegalStateException. The description of the error is as follows.

The path to the driver executable must be set by webdriver.gecko.driver system property.

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; 
	at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
	at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
	at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:38)
        ...

So, for a successful migration, first of all, you should download the latest version of the Geckodriver.

Click here to download the latest Geckodriver.

Next, you need to provide the Geckodriver path to the application. For this, you can adapt any of the following three approaches.

  1. Use system property to specify the Geckodriver path
  2. Add a persistent environment variable to set the Geckodriver path
  3. Add Geckodriver path via the browser’s desired capabilities

1. Use system property to specify the Geckodriver path.

The new Geckodriver for Selenium 3 accepts the following system property to set from the Java code.

System.setProperty("webdriver.gecko.driver", "Geckodriver Executable Path");

Below is the full code to launch the Firefox using Geckodriver.

package seleniumPrograms;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium3_test {
 
	public static void main(String[] args) throws InterruptedException {
		System.setProperty("webdriver.gecko.driver", "C:\\Selenium3\\work\\tools\\geckodriver.exe");
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
 
		Thread.sleep(5000);
		driver.quit();
	}
}

You can execute the above program in Eclipse and check if it is working all right. It’ll start Firefox and open the Google website.

2. Add a persistent environment variable to set the Geckodriver path.

Another simple method that one can use is by appending the path of Geckodriver in the PATH environment variable.

You can quickly do it from the command line using the SETX command in Windows 7.

setx PATH "C:\Selenium3\work\tools\geckodriver.exe;%PATH%;"

The above command will update the PATH in the global context. The significance of this method is that you won’t need to set the system property in each Selenium test.

Your code will work without setting up the "webdriver.gecko.driver" Property.

3. Add the Geckodriver path via the browser’s desired capabilities.

Alternatively, we can make use of the Marionette capability parameter to run our Selenium3 tests. It is very easy to set it from the Java code.

Please check out the below Java code. With this method, it is also possible to run your tests remotely.

package seleniumPrograms;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Selenium3_test {
 
	public static void main(String[] args) throws InterruptedException {
		DesiredCapabilities dc = DesiredCapabilities.firefox();
		dc.setCapability("marionette", true);
		WebDriver driver = new FirefoxDriver(dc);
		driver.get("http://www.google.com");
 
		Thread.sleep(5000);
		driver.quit();
	}
}

TestNG Example – Launch Firefox using Geckodriver.

If any of you would like to use Geckodriver with a TestNG project, then refer to the below code.

package com.local;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class Selenium3_test {
	
	String geckoPath = "<Provide Gecko Driver Executable Path>";
	public WebDriver driver;
	
	@Test
	public void initSession() {
		System.out.println("Starting Firefox browser"); 
		System.setProperty("webdriver.gecko.driver", geckoPath + "geckodriver.exe");
		driver = new FirefoxDriver();
	}

	@Test
	public void openURL() {
		driver.navigate().to("http://www.google.com/");
	}
	
	@Test
	public void endSession() {
		if(driver!=null) {
		    driver.close();
		}
	}
}

Also, if you are using the Maven tool to build your project, then you’ll need to update the POM file in your project.

Please see the sample POM file given below.

<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-java</artifactId>
   <version>3.0.1</version>
</dependency>

Summary – Selenium Project for Firefox using Geckodriver

We used Selenium 3 for this tutorial. However, you can use the code with the latest version of Selenium. If you encounter issues, we’ll help you resolve them.

Lastly, support our site if you want us to continue sharing knowledge. Share this post on social media (Facebook/Twitter) if you gained some knowledge from this tutorial.

Keep Learning,
TechBeamers.

You Might Also Like

20 Demo Sites for Automation Testing

Page Object Model (POM) and Page Factory Guide in Selenium Java

Selenium 4 With Python Guide

Selenium 4 Relative Locators Guide

Selenium Version 4 Features – What’s New?

TAGGED:Advanced Selenium Tutorialselenium example code
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article C# Object-Oriented Interview Questions Set-2 C# Object-Oriented Interview Questions Set-2
Next Article 25 C# OOPs Interview Questions for Programmers C# OOPs Interview: The Best 25 Questions

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x