Open-Source Frameworks Supported by Selenium WebDriver
Selenium WebDriver integrates with several open-source testing frameworks to provide assertions, test lifecycle management, and reporting.
Primary frameworks:
1. JUnit The classic Java unit testing framework. Simple and widely used.
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*;
public class LoginTest {
@Before
public void setUp() {
driver = new ChromeDriver();
}
@Test
public void testLogin() {
driver.get("https://example.com/login");
assertEquals("Login Page", driver.getTitle());
}
@After
public void tearDown() {
driver.quit();
}
}
2. TestNG (most popular for Selenium) Feature-rich testing framework with parallel execution, grouping, and data providers.
import org.testng.annotations.*;
import org.testng.Assert;
public class LoginTest {
@BeforeClass
public void setUp() { driver = new ChromeDriver(); }
@Test(groups = "regression", dataProvider = "loginData")
public void loginTest(String user, String pass) {
Assert.assertEquals(driver.getTitle(), "Dashboard");
}
@AfterClass
public void tearDown() { driver.quit(); }
}
3. Cucumber (BDD — Behavior-Driven Development) Write tests in plain English (Gherkin syntax) with step definitions in Java.
Feature: Login functionality
Scenario: Valid login
Given I am on the login page
When I enter "admin" and "password"
Then I should see "Dashboard"
@Given("I am on the login page")
public void iAmOnLoginPage() {
driver.get("https://example.com/login");
}
4. JBehave (BDD framework) Another BDD framework similar to Cucumber, uses story files.
Given I navigate to login page
When I enter username admin and password password
Then I should be redirected to dashboard
Framework comparison:
| Framework | Style | Best for |
|---|---|---|
| JUnit | Annotation-based | Simple unit/integration tests |
| TestNG | Annotation-based | Complex Selenium suites |
| Cucumber | BDD (Gherkin) | Non-technical stakeholders |
| JBehave | BDD (story files) | BDD with story-format specs |
Key list:
- ✓JUnit
- ✓TestNG
- ✓CUCUMBER
- ✓JBEHAVE
