Skip to content
SDETRole
SDETRole

Data-driven login with a TestNG DataProvider

intermediate TestNG Challenge 12 of 13

LoginTest has three tests that are identical except for the username, the password and the expected flash message. BaseTest provides driver and baseUrl. Make it data-driven.

Requirements

  • Add a @DataProvider method that returns Object[][], one row per case: {username, password, expectedMessage}.
  • Replace the three test methods with one @Test(dataProvider = "...") method that takes those values as parameters.
  • Keep all three cases: standard_user sees “Welcome back”, locked_user sees “Your account is locked”, and a wrong password shows “Invalid username or password”.

Expected output

One `@DataProvider` method returns an `Object[][]` with three rows of `{username, password, expectedMessage}`. A single `@Test(dataProvider = "...")` method takes those three parameters, and TestNG reports it as three test invocations.

Checks (5)

  • ○Declares a @DataProvider method
  • ○The DataProvider returns Object[][]
  • ○The test uses @Test(dataProvider = "...")
  • ○The data-driven test method takes parameters
  • ○The duplicated test methods are gone (only one @Test)
Solution.javaValidated · not executed

Ctrl/⌘ + Enter to validate · your code is saved in this browser

Results

Validate your code against the checks.

Type to search across SDETRole.

Full-text search by Pagefind · runs in your browser Open search page →