Advantages of using data driven framework:
E.g: using invalid login scenario as an example.
1. define a negative login method on LgoinPage.java
//Find the locator of the error message
By loginErrorLocator = By.id("flash-messages");
//Define a negativeLogin method
public void negativeLogin(String username, String password) {
System.out.println("Entering username and password:");
find(usernameLocator).sendKeys(username);
find(passwordLocator).sendKeys(password);
System.out.println("Click Logon button");
find(submitLocator).click();
//invoke the waitLoginError method to wait for error appear
waitLoginError();
}
//define a method to wait erro msg appear, invoke the waitForVisibilityOf method defefined in baseObject.java
public void waitLoginError() {
waitForVisibilityOf(loginErrorLocator, 5);
}
//define get error msg method. invoke find method defined in baseObject.java
public String getLoginError() {
return find(loginErrorLocator).getText();
}
2. Define the data provider method on baseTest.java (this base test includes setup. teardown..)
@DataProvider(name = "negativeLoginData")
public static Object[][] negativeLoginData(){
return new Object[][] {
{"IncorrectUserName", "Password!", "Your username is invalid!"},
{"username","incorrectPassword", "Your password is invalid!"}
};
}
3. Define test method to use the dataProvider
@Test(dataProvider="negativeLoginData")
public void netgativeLoginTest(String username, String password, String expectedErrorMessage ) {
LoginPage lp = new LoginPage(driver);
lp.open();
lp.negativeLogin(username, password);
String errorMessage = lp.getLoginError();
//verification
Assert.assertTrue(errorMessage.contains(expectedErrorMessage), "Actual and expected error messages are different.\nExpected:"+expectedErrorMessage+"Actual:"+errorMessage);
}
4. Update the testng.xml to include the negative test case
e.g.
<test name="NegativeLogin Test">
<classes>
<class name="com.test.FirstTest">
<methods>
<include name="netgativeLoginTest"></include>
</methods>
</class>
</classes>
</test>
Data Driven Testing - Using DataProvider
原文:https://www.cnblogs.com/amy2012/p/11624064.html