Executing selenium tests in JUnit testing framework

To use Selenium with JUnit, you will have to add below dependencies.



Then you can add below base test class. In this class, we have written before and after methods that are used to launch and close the webdriver.

package simple_selenium_tests;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BaseTest {

    public WebDriver driver;

    public void setUp() {
        System.out.println("Launching browser");
        driver = new FirefoxDriver();

    public void cleanup(){
        System.out.println("Closing the browser");

Then we can write actual tests as shown in below example. Note that below class extends the BaseTest class. So driver instance (inherited from BaseTest) can be used directly in the test.

package simple_selenium_tests;

import org.junit.Assert;
import org.junit.Test;
import seleniumtests.BaseTest;

public class SeleniumTest extends BaseTest {

    public void verifyTitle(){
                contains("Free Software Tutorials"));


You may also like...