0
0
Selenium Javatesting~10 mins

Why data separation improves test coverage in Selenium Java - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test demonstrates how separating test data from test logic helps improve test coverage by allowing multiple data sets to be tested easily without changing the test code.

Test Code - JUnit + Selenium
Selenium Java
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class LoginTest {

    // Test data separated from test logic
    private static final String[][] testData = {
        {"user1", "pass1", "Welcome user1!"},
        {"user2", "pass2", "Welcome user2!"},
        {"user3", "wrongpass", "Invalid credentials"}
    };

    @Test
    public void testLoginWithMultipleData() {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        for (String[] data : testData) {
            String username = data[0];
            String password = data[1];
            String expectedMessage = data[2];

            driver.get("https://example.com/login");

            WebElement userField = driver.findElement(By.id("username"));
            WebElement passField = driver.findElement(By.id("password"));
            WebElement loginButton = driver.findElement(By.id("loginBtn"));

            userField.clear();
            userField.sendKeys(username);
            passField.clear();
            passField.sendKeys(password);
            loginButton.click();

            WebElement message = driver.findElement(By.id("message"));
            assertEquals(expectedMessage, message.getText());
        }

        driver.quit();
    }
}
Execution Trace - 12 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to https://example.com/login for first data setLogin page is loaded with username and password fields-PASS
3Finds username and password input fields and login buttonInput fields and button are visible and interactable-PASS
4Enters username 'user1' and password 'pass1', clicks loginForm submitted, waiting for response message-PASS
5Finds message element and verifies text equals 'Welcome user1!'Message displayed on pageAssert message text == 'Welcome user1!'PASS
6Repeats navigation and input for second data set: 'user2', 'pass2'Login page loaded again-PASS
7Enters username 'user2' and password 'pass2', clicks loginForm submitted, waiting for response message-PASS
8Verifies message text equals 'Welcome user2!'Message displayed on pageAssert message text == 'Welcome user2!'PASS
9Repeats navigation and input for third data set: 'user3', 'wrongpass'Login page loaded again-PASS
10Enters username 'user3' and password 'wrongpass', clicks loginForm submitted, waiting for response message-PASS
11Verifies message text equals 'Invalid credentials'Error message displayed on pageAssert message text == 'Invalid credentials'PASS
12Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: If the message text does not match the expected message for any data set
Execution Trace Quiz - 3 Questions
Test your understanding
Why does separating test data from test logic help improve test coverage?
AIt makes the test run faster
BIt allows running the same test with multiple data sets easily
CIt removes the need for assertions
DIt hides test failures
Key Result
Separating test data from test logic allows easy reuse of the same test steps with different inputs, increasing test coverage without duplicating code.