0
0
Selenium Javatesting~10 mins

Data providers for parameterization in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses a data provider to run the same login test with different username and password combinations. It verifies that the login page accepts valid credentials and shows the correct welcome message.

Test Code - TestNG with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class LoginTest {
    WebDriver driver;

    @BeforeClass
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("https://example.com/login");
    }

    @DataProvider(name = "loginData")
    public Object[][] loginData() {
        return new Object[][] {
            {"user1", "pass1"},
            {"user2", "pass2"}
        };
    }

    @Test(dataProvider = "loginData")
    public void testLogin(String username, String password) {
        driver.findElement(By.id("username")).clear();
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.id("password")).clear();
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.id("loginBtn")).click();

        String welcomeText = driver.findElement(By.id("welcomeMsg")).getText();
        Assert.assertTrue(welcomeText.contains(username), "Welcome message should contain username");

        driver.findElement(By.id("logoutBtn")).click();
    }

    @AfterClass
    public void tearDown() {
        driver.quit();
    }
}
Execution Trace - 12 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser opened at https://example.com/login showing login form with username, password fields and login button-PASS
2Test runs first iteration with username 'user1' and password 'pass1'Login form is visible with empty username and password fields-PASS
3Find username field, clear it, enter 'user1'Username field contains 'user1'-PASS
4Find password field, clear it, enter 'pass1'Password field contains 'pass1'-PASS
5Click login buttonPage navigates to user dashboard showing welcome messageCheck welcome message contains 'user1'PASS
6Click logout button to return to login pageLogin page is displayed again-PASS
7Test runs second iteration with username 'user2' and password 'pass2'Login form is visible with empty username and password fields-PASS
8Find username field, clear it, enter 'user2'Username field contains 'user2'-PASS
9Find password field, clear it, enter 'pass2'Password field contains 'pass2'-PASS
10Click login buttonPage navigates to user dashboard showing welcome messageCheck welcome message contains 'user2'PASS
11Click logout button to return to login pageLogin page is displayed again-PASS
12Test ends and browser closesBrowser closed-PASS
Failure Scenario
Failing Condition: Welcome message does not contain the username after login
Execution Trace Quiz - 3 Questions
Test your understanding
What does the data provider supply to the test method?
ABrowser types to test
BDifferent username and password pairs
CURLs to navigate
DTimeout values for waits
Key Result
Using data providers allows running the same test with multiple sets of data, making tests efficient and reusable without repeating code.