Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Setting up WebDriver for Firefox using Selenium in Java involves several steps. Below is a detailed guide with examples to help you get started:

Prerequisites

  1. Java Development Kit (JDK): Make sure you have JDK installed. You can download it from here.
  2. Eclipse IDE (or any other Java IDE): You can download it from here.
  3. Maven: Ensure Maven is installed for dependency management. You can download it from here.

Steps to Set Up WebDriver for Firefox

Step 1: Set Up Your Project

1. Create a Maven Project in Eclipse:

  • Open Eclipse IDE.
  • Go to ‘File‘ -> ‘New‘ -> ‘Maven Project‘.
  • Select a workspace location and click ‘Next‘.
  • Choose an ‘archetype‘, such as ‘maven-archetype-quickstart‘, and click ‘Next‘.
  • Provide ‘Group Id‘ and ‘Artifact Id‘, and click ‘Finish‘.

2. Add Selenium and WebDriver Manager Dependencies:

Open the ‘pom.xml‘ file and add the following dependencies:

				
					<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.2</version>
    </dependency>
    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

				
			

Step 2: Write the Selenium Test

1. Create a Java Class:

  • Right-click on src/main/java -> New -> Class.
  • Name it FirefoxTest and click Finish.

2. Write the Test Code:

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class FirefoxTest {
    public static void main(String[] args) {
        // Setup WebDriverManager
        WebDriverManager.firefoxdriver().setup();
        
        // Initialize Firefox WebDriver
        WebDriver driver = new FirefoxDriver();
        
        // Open a website
        driver.get("https://www.example.com");
        
        // Print the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        
        // Close the browser
        driver.quit();
    }
}

				
			

Step 3: Run the Test

1. Run the FirefoxTest class:

  • Right-click on the FirefoxTest.java file.
  • Select Run As -> Java Application.

2. Observe the Output:

  • The Firefox browser should launch, navigate to https://www.example.com, print the title of the page, and then close.

Explanation of the Code

1. WebDriverManager Setup:

				
					WebDriverManager.firefoxdriver().setup();

				
			

This line uses WebDriverManager to download and set up the appropriate Firefox driver version automatically, avoiding manual download and configuration.

2. Initialize Firefox WebDriver:

				
					WebDriver driver = new FirefoxDriver();

				
			

This initializes the ‘FirefoxDriver‘, which is used to control the Firefox browser.

3. Navigate to a Website:

				
					driver.get("https://www.example.com");

				
			

This opens the specified URL in the Firefox browser.

4. Get and Print Page Title:

				
					System.out.println("Page title is: " + driver.getTitle());

				
			

This retrieves the title of the current page and prints it to the console.

5. Close the Browser:

				
					driver.quit();

				
			

This closes the browser and ends the WebDriver session.

Additional Tips

  • Geckodriver Path: If you want to specify the path to the ‘geckodriver‘ manually, you can set it using:
				
					System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");

				
			

This is generally unnecessary when using WebDriverManager.

  • Implicit Waits: To handle dynamic web elements, you can add implicit waits:
				
					driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

				
			
  • Explicit Waits: For more control over waits, you can use explicit waits:
				
					WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")));

				
			

Following these steps and tips will help you set up and run Selenium WebDriver tests with Firefox in Java effectively.

Scroll to Top