0
0
Selenium Javatesting~10 mins

DataProvider with external data in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses a DataProvider to supply multiple sets of login credentials from an external source. It verifies that the login page accepts valid credentials and shows a welcome message after login.

Test Code - TestNG
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
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.manage().window().maximize();
    }

    @DataProvider(name = "loginData")
    public Object[][] getData() {
        // Simulate external data source, e.g., CSV or DB
        return new Object[][] {
            {"user1", "pass1"},
            {"user2", "pass2"}
        };
    }

    @Test(dataProvider = "loginData")
    public void testLogin(String username, String password) {
        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 welcomeMsg = driver.findElement(By.id("welcomeMessage"));
        Assert.assertTrue(welcomeMsg.isDisplayed(), "Welcome message should be displayed after login");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is maximized and ready-PASS
2Navigates to https://example.com/loginLogin page is loaded with username, password fields and login button-PASS
3Finds username and password input fields and login buttonAll elements are present and interactable-PASS
4Enters username 'user1' and password 'pass1', then clicks loginLogin form submitted-PASS
5Finds welcome message element after loginWelcome message is displayed on the pageAssert welcome message is displayedPASS
6Repeats steps 2-5 with username 'user2' and password 'pass2'Login page reloads and accepts second credentialsAssert welcome message is displayedPASS
7Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Welcome message element is not found or not displayed after login
Execution Trace Quiz - 3 Questions
Test your understanding
What does the DataProvider supply to the test method?
ABrowser configuration settings
BMultiple sets of login credentials
CExpected welcome messages
DTimeout durations
Key Result
Using a DataProvider with external data allows running the same test multiple times with different inputs, improving test coverage and reusability.