TestNG 使用 @Factory 和 @DataProvider [英] TestNG Using @Factory and @DataProvider

查看:32
本文介绍了TestNG 使用 @Factory 和 @DataProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用 TestNG 和 Java 编程非常陌生,我对一起使用 @Factory 和 @DataProvider 有疑问.

I'm very new using TestNG and Java programming in general and I have a question in regards to using @Factory and @DataProvider together.

我想测试多次提交具有不同输入数据的网络表单.

I want to test submitting a web form multiple times with different input data every time.

我有以下代码:

public class SolicitudEmpleo extends LaunchBrowser { 

private String campoDni;
private String firstName;
private String lastName;

@Factory (dataProvider="dataProviderMethod")
public SolicitudEmpleo(String campoDni, String firstName, String lastName){
    this.campoDni=campoDni;
    this.firstName = firstName;
    this.lastName = lastName;
}

@DataProvider
public static Object[][] dataProviderMethod() {
return new Object[][] { {"000007", "Bill", "Gates"}, {"000008", "Stack", "Overflow"} };

}

当我运行测试用例时,数据提供者将始终使用数组中包含的最后一个值.例如,如果我现在运行测试,它会在 Web 表单中输入值:000008"、Stack"、Overflow",而完全忽略000007"、Bill"、Gates".

When I run my test cases, the data provider will always use the last values contained in the array. For example, if I run the test now it will input the values: "000008", "Stack", "Overflow" into the web form and completely ignore "000007", "Bill", "Gates".

谢谢!!

我在函数中打印了所有三个字符串:

I printed all three Strings in the function:

@Factory (dataProvider="dataProviderMethod")
    public SolicitudEmpleo(String campoDni, String firstName, String lastName){
        this.campoDni=campoDni;
        this.firstName = firstName;
        this.lastName = lastName;

        System.out.println(campoDni);
        System.out.println(firstName);
        System.out.println(lastName);
    }

控制台:

000007账单盖茨000008堆溢出

000007 Bill Gates 000008 Stack Overflow

编辑 2:我将发布带有测试用例的整个代码,以便你们可以看看是否能发现错误:

EDIT 2: I'll post the entire code with the test cases so you guys can take a look and see if you can spot the mistake:

 public class SolicitudEmpleo extends LaunchBrowser { 

    private String campoDni;
    private String firstName;
    private String lastName;

    @Factory (dataProvider="dataProviderMethod")
    public SolicitudEmpleo(String campoDni, String firstName, String lastName){
        this.campoDni=campoDni;
        this.firstName = firstName;
        this.lastName = lastName;

        System.out.println(campoDni);
        System.out.println(firstName);
        System.out.println(lastName);
    }

    @DataProvider
    public static Object[][] dataProviderMethod() {
    return new Object[][] { {"000007", "Bill", "Gates"}, {"000008", "Stack", "Overflow"} };

    }


@Test (priority = 1)
public void testCase1() throws InterruptedException {

    Thread.sleep(3000);
    WebElement addButton = driver.findElement(By.xpath("//*[@id='pt1:r1:0:b3_2']/a/span"));
    addButton.click();

}

@Test (priority = 2)
public void testCase2() throws InterruptedException { 

    Thread.sleep(5000);
    Select dropdown = new Select (driver.findElement(By.id("pt1:r1:1:s2:soc1A::content")));
    dropdown.selectByVisibleText("Dropdown");

    Thread.sleep(5000);
    WebElement campoDNI = driver.findElement(By.xpath("//*[@id='pt1:r1:1:s2:it1::content']"));
    campoDNI.sendKeys(campoDni);

    WebElement verifyButton = driver.findElement(By.xpath("//*[@id='pt1:r1:1:s2:b1']/span"));
    verifyButton.click();
    Thread.sleep(5000);

}

@Test (priority = 3)
public void testCase3() throws InterruptedException {

    WebElement firstNameElement = driver.findElement(By.xpath("//*[@id='pt1:r1:2:it2::content']"));
    firstNameElement.sendKeys(firstName);

    WebElement lastNameElement = driver.findElement(By.xpath("//*[@id='pt1:r1:2:it3::content']"));
    lastNameElement.sendKeys(lastName);

}

@Test (priority = 4)
public void testCase4() throws InterruptedException {

    Thread.sleep(2000);
    WebElement saveFormButton = driver.findElement(By.xpath("//*[@id='pt1:r1:2:b12']/a/span"));
    saveFormButton.click();
    Thread.sleep(6000);

}   
  }

<小时>

public class LaunchBrowser {

WebDriver driver;

@BeforeSuite()
public void launchBrowser() throws InterruptedException {
    System.setProperty("webdriver.chrome.driver", "/Users/user/Documents/selenium-2.40.0/chromedriver");
    driver = new ChromeDriver();
    driver.get("http://webform.jsf");
    driver.findElement(By.id("pt1:pt_s1:itUsuario::content")).sendKeys("username");
    driver.findElement(By.id("pt1:pt_s1:itClave::content")).sendKeys("password");
    driver.findElement(By.id("pt1:pt_s1:itClave::content")).sendKeys(Keys.ENTER);
    Thread.sleep(5000);
}

 }

此外,此测试的 XML 文件:

Also, the XML file for this test:

<test name="参数测试一"><类><class name="test.classes.SolicitudEmpleo"></class></classes></测试></suite>

推荐答案

运行以下代码:

public class SolicitudEmpleo extends LaunchBrowser{
    private String campoDni;
    private String firstName;
    private String lastName;

    @Factory (dataProvider="dataProviderMethod")
    public SolicitudEmpleo(String campoDni, String firstName, String lastName){
        this.campoDni=campoDni;
        this.firstName = firstName;
        this.lastName = lastName;
        System.out.println("data number"+campoDni);
    }

    @DataProvider
    public static Object[][] dataProviderMethod() {
        return new Object[][] { {"000007", "Bill", "Gates"}, {"000008", "Stack", "Overflow"} };
    }
    @Test (priority = 1)
    public void testCase1(){
        System.out.println("Test 1");  
    }
    @Test (priority = 2)
    public void testCase2() throws InterruptedException { 
        System.out.println("Test 2 "+campoDni);
    }
    @Test (priority = 3)
    public void testCase3() throws InterruptedException {
        System.out.println("Test 3 "+firstName+","+lastName);
    }
    @Test (priority = 4)
    public void testCase4() throws InterruptedException {
        System.out.println("Test 4");
    }   
}
public class LaunchBrowser {
    @BeforeSuite()
    public void launchBrowser() throws InterruptedException {
        System.out.println("Launching");
    }
}

产生以下输出:

data number000007
data number000008
Launching
Test 1
Test 1
Test 2 000007
Test 2 000008
Test 3 Bill,Gates
Test 3 Stack,Overflow
Test 4
Test 4

因此,问题不在于 TestNG.它必须与 WebDriver 相关,并且可能与您正在使用的站点有关.

Therefore, the problem is not with TestNG. It must be something with WebDriver, and probably the site you are using.

这篇关于TestNG 使用 @Factory 和 @DataProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆