Selenium FireFox WebDriver Test Cases with JUnit in Java
In this example we will show you how to run Selenium FireFox WebDriver Test Cases with JUnit in Java. Most of the time the default installation of firefox is picked up. But when you need to specify the location of firefox driver you need to set the webdriver.firefox.bin
property to the path of the FireFoxDriver. You can either set this property using System.setProperty()
or you can pass it as an argument using -Dwebdriver.firefox.bin=path/to/file
.
Maven Dependencies
You need to add the following dependencies to your project.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.44.0</version>
</dependency>
Selenium FireFox WebDriver with JUnit
You need to create a FireFoxDriver implementation of the WebDriver
. You can do this by creating a new FireFoxDriver()
. After you initialized the FireFoxDriver you can do the actual testing. For simplicity we just acquire a web resource. After the test is completed we need to clean up the WebDriver.
package com.memorynotfound.test;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FireFoxSeleniumTest {
private static WebDriver driver;
@BeforeClass
public static void setUp(){
driver = new FirefoxDriver();
}
@Test
public void testFireFoxSelenium() {
driver.get("https://memorynotfound.com/");
}
@AfterClass
public static void cleanUp(){
if (driver != null) {
driver.close();
driver.quit();
}
}
}
Executing Selenium FireFox WebDriver Test Cases with Maven
When you execute the Selenium FireFox WebDriver Test Cases you can see that FireFox start up a new instance and that the page is reached.
mvn clean test
Output
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SELENIUM - firefox-example 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[...]
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.memorynotfound.test.FireFoxSeleniumTest
Starting Selenium FireFox WebDriver 2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9cf) on port 36247
Only local connections are allowed.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.492 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS (Selenium FireFox WebDriver)
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.483 s
[INFO] Finished at: 2015-02-06T19:22:13+01:00
[INFO] Final Memory: 15M/245M
[INFO] ------------------------------------------------------------------------