@Daniel, if we need do any prepare actions before running tests (for example, open connection to DB and prepare test data in it, or call other not-testing-here services, or put test data, that will be used in most of all tests) - we must use one of @Before
annotations. They can be very useful and flexy, good answer with code about them here.
What about @Dataprovider
- it provides data straight to tests, those need it:
@Test(dataProvider = "Authentication")
public void errorMessageOnLoginWithBadCredentials(String email, String password, String errMsg) {
User badUser = new User(email, password);
at(LoginPage.class)
.loginAs(badUser)
.errorMessage
.shouldHave(exactText(errMsg));
}
@DataProvider(name = "Authentication")
public static Object[][] credentials() {
return new Object[][]{
{" ", " ", "Username is required"},
{"user1@gmail.com", "UserTest@123", "Login and / or password do not match"},
{"user1@gmail.com", " ", "Password is required"},
{"ololo@ololo.com", "admin", "Login and / or password do not match"}
};
}
To avoid ugly syntax of Object[][] (or Iterator<Object[]>)
, you can also use @DataSupplier
(see here), adapted for using with Stream API, for example.
Hope this be useful.