Running Tests on Firefox
Step 1: Setup Test Environment
1. Install Java on your system.
2. After installing Java on your system or already installing, verify the installation by opening a command prompt and typing:
Java -version
3. Install Eclipse IDE on your system.
4. Install Selenium WebDriver for Java on your system.
Step 2: Download GeckoDriver
1. Go to the Mozilla GeckoDriver GitHub page.
2. Download the latest version of GeckoDriver that is compatible with your installed version of Firefox. Extract the zip file and copy the path of the geckodriver.exe (e.g., D:/geckodriver-v0.33.0-win64/geckodriver.exe).
Step 3: Configure the Project
1. Create a new Java project in Eclipse IDE. In Eclipse, go to File > New > Java Project. Give a name to your project, such as `SeleniumTest`, and click Finish.
2. Add the downloaded Selenium JAR files to your Java project’s build path. Select all the jar files and click Apply and close.
3. Create a new Java class in the project. In Eclipse, right-click on the src folder under your project name and select New > Class. Give a name to your class, such as `TestFirefox`, and click Finish.
3. Import the required packages
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
4. Set the system property for FirefoxDriver (path to firefoxdriver executable).
(e.g.,D:/geckodriver-v0.33.0-win64/geckodriver.exe).
System.setProperty("webdriver.gecko.driver", "D:/geckodriver-v0.33.0-win64/geckodriver.exe");
5. Create an instance of FirefoxDriver.
WebDriver driver = new FirefoxDriver();
Below is the Java program to search on Firefox:
Java
// Java program to search on Firefox import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class TestFirefox { public static void main(String[] args) { // Set the path of the geckodriver executable System.setProperty( "webdriver.gecko.driver" , "D:/geckodriver-v0.33.0-win64/geckodriver.exe" ); // Create an instance of FirefoxDriver class WebDriver driver = new FirefoxDriver(); // Navigate to a web page driver.get( "https://www.google.com/" ); // Find the search box element by its name WebElement element = driver.findElement(By.name( "q" )); // Enter a search query element.sendKeys( "w3wiki" ); // Submit the form element.submit(); // Wait for the page to load try { Thread.sleep( 5000 ); } catch (InterruptedException e) { e.printStackTrace(); } // Find all the search result links using XPath List<WebElement> searchResults = driver.findElements(By.xpath( "//div[@class='tF2Cxc']//a[@href]" )); // Check if there are search results if (searchResults.size() > 0 ) { // Click on the first link (index 0) searchResults.get( 0 ).click(); } else { System.out.println( "No search results found." ); } // Get the title of the current page String title = driver.getTitle(); // Print the title System.out.println( "Page Title: " + title); // Close the browser driver.quit(); } } |
Output:
How to Run Selenium Test on Firefox?
Selenium is a popular open-source tool for automating web browser interactions. Firefox is a fast and secure web browser developed by Mozilla. Firefox uses GeckoDriver to control and interact with Firefox during automated testing. GeckoDriver is a component of the Selenium automation framework that allows communication between the Selenium WebDriver and the Firefox web browser.