首页 > 其他 > 详细

Data Driven Testing - Using DataProvider

时间:2019-10-05 11:13:53      阅读:75      评论:0      收藏:0      [点我收藏+]

Advantages of using data driven framework:

  • Re-usable code
  • Better test coverage
  • Less Maintenance

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!