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
@DataProvidermethod that returnsObject[][], 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_usersees “Welcome back”,locked_usersees “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.
Related interview questions
- Which locator strategies do you use in Selenium, and how do you choose?
- How do you handle StaleElementReferenceException in Selenium?
- How do you implement the Page Object Model, and why do many teams avoid PageFactory?
- How do you make a Selenium framework thread-safe for parallel execution? Explain ThreadLocal.