TestNG with 2 Testcase with data provider

时间:2018-05-23 07:40:38

标签: selenium testng testng-dataprovider

我对TestNG有疑问。我有一些用于测试的数据集,但我想在数据集中执行每个数据的完整测试周期。我怎么能这样做?

这是我的代码

@DataProvider(name = "login")
public static Object[][] usernamePassword() {
    // username, password, isSukses
    return new Object[][] { 
        { "coin", "coin123", true }, 
        { "coin", "coin", false },
        { "username", "password", false } };
}

 @Test(description = "Fill the Login Details", priority = 2)
public void FillLoginDetails(String user, String pass, boolean isSuccess) throws Exception {

    try {
        // Get the username element
        WebElement username = driver.findElement(By.id("uid"));
        username.sendKeys(user);

        // Get the password element
        WebElement password = driver.findElement(By.id("pwd"));
        password.sendKeys(pass);

        Thread.sleep(1000);

        TestNGResults.put("3", new Object[] { 2d, "Fill Login form data (Username/Password)",
                "Login details gets filled", "Pass" });

    } catch (Exception e) {
        TestNGResults.put("3",
                new Object[] { 2d, "Fill Login form data (Username/Password)", "Login form gets filled", "Fail" });
        Assert.assertTrue(false);
    }
}

@Test(description = "Perform Login", priority = 3)
public void doLogin() throws Exception {

    try {
        // Click on the Login button
        WebElement login = driver.findElement(By.id("login-button"));
        login.click();

        Thread.sleep(1000);
        // Assert the user login by checking the Online user
        WebElement onlineuser = driver.findElement(By.cssSelector("h3.paddedleft > strong.ng-binding"));
        AssertJUnit.assertEquals("Welcome, " + "coin", onlineuser.getText());
        TestNGResults.put("4",
                new Object[] { 3d, "Click Login and verify welcome message", "Login success", "Pass" });
    } catch (Exception e) {
        TestNGResults.put("4",
                new Object[] { 3d, "Click Login and verify welcome message", "Login success", "Fail" });
        Assert.assertTrue(false);
    }
}

如何测试这样,

  

测试优先级2(数据1),

     

测试优先级3(数据1),

           

测试优先级2(数据2),

     

测试优先级3(数据2),

           

测试优先级2(数据3),

     

测试优先级3(数据3),

???

2 个答案:

答案 0 :(得分:0)

您可以使用Factory Concepts实现上述场景。但是测试数据需要移动到不同的类,并且需要在测试类中进行一些修改。

请检查以下网址,您将获得清晰的想法。

http://fruzenshtein.com/testng-dataprovider-run-tests-sequentially/

Executing multiple test sequentially with different parameters testng

为您的参数化数据

创建一个实例变量
String user; 
String pass;
boolean isSuccess;

执行构造函数使用上述参数进行重载并初始化实例变量

//Assumed the Class Name as LoginTest

LoginTest(String user, String pass, boolean isSuccess){
this.user=user;
this.pass=pass;
this.isSuccess=isSuccess;
}

为数据提供者创建另一个类

public class SampleFactory {

        @Factory(dataProvider="login")
        public Object[] createInstances((String userName, String password, boolean isSuccess) {
            return new Object[] {new LoginTest(userName, password,isSuccess)};
        }

        @DataProvider(name = "login")
        public static Object[][] usernamePassword() {
            // username, password, isSukses
            return new Object[][] { 
                { "coin", "coin123", true }, 
                { "coin", "coin", false },
                { "username", "password", false } };
        }

    }

配置并运行TestNG XML

在testng xml中单独添加SampleFactory类并运行测试。

答案 1 :(得分:0)

您的测试缺少dataProvider =“login”

@Test(dataProvider =“login”,description =“Perform Login”,priority = 3)