TestNG参数和数据源

TestNG参数

每个人都知道参数化在测试和自动化测试中的重要性。它  允许我们使用不同的输入和验证值多次自动运行测试用例。由于Selenium Webdriver更像是一个自动化测试框架,而不是一个现成的工具,因此您必须付出一些努力来支持自动化测试中的数据驱动测试。我通常更喜欢使用Microsoft Excel作为存储我的参数的格式,但我的许多粉丝都要求在TestNG数据提供程序上写一篇文章。

TestNG再次为我们提供了一个名为TestNG Parameters的有趣功能。TestNG允许您使用testng.xml将参数直接传递给测试方法。

怎么做…

让我举一个LogIn应用程序的一个非常简单的例子,其中需要用户名和密码来清除身份验证。

1)在我的演示版OnlineStore应用程序上创建一个测试来执行LogIn,它将两个字符串参数作为用户名和密码。

2)使用TestNG Annotation提供用户名和密码作为参数。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

package automationFramework;

 

import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

 

import org.openqa.selenium.WebDriver;

 

import org.openqa.selenium.firefox.FirefoxDriver;

 

import org.testng.annotations.Test;

 

import org.testng.annotations.Parameters;

 

public class TestngParameters {

 

private static WebDriver driver;

 

  @Test

 

  @Parameters({ "sUsername", "sPassword" })

 

  public void test(String sUsername, String sPassword) {

 

  driver = new FirefoxDriver();

 

      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

 

      driver.get("http://www.store.demoqa.com");

 

      driver.findElement(By.xpath(".//*[@id='account']/a")).click();

 

      driver.findElement(By.id("log")).sendKeys(sUsername);

 

      driver.findElement(By.id("pwd")).sendKeys(sPassword);

 

      driver.findElement(By.id("login")).click();

 

      driver.findElement(By.xpath(".//*[@id='account_logout']/a")).click();

 

      driver.quit();

 

  }

 

}

3)参数将从testng.xml传递值,我们将在下一步中看到。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<suite name="Suite">

 

    <test name="ToolsQA">

 

<parameter name="sUsername" value="testuser_1"/>

 

<parameter name="sPassword" value="Test@123"/>

 

<classes>

 

    <class name="automationFramework.TestngParameters" />

 

</classes>

 

    </test>

 

</suite>

现在,运行testng.xml,它将运行parameterTest方法。TestNG将尝试查找名为sUsername&sPassword的参数。

 

TestNG DataProviders

当您需要传递需要从Java创建的复杂参数或参数(复杂对象,从属性文件或数据库中读取的对象等)时,在这种情况下,可以使用Dataproviders传递参数。数据提供程序是使用@DataProvider注释的方法。数据提供程序返回一个对象数组。

让我们看看使用Dataproviders的相同登录示例。

怎么做…

1)定义方法credentials(),使用注释定义为Dataprovider。此方法返回对象数组的数组。

2)将方法test()添加到DataProviderTest类。此方法将两个字符串作为输入参数。

3)将注释@Test(dataProvider =“Authentication”)添加到此方法。属性dataProvider映射到“身份验证”。

测试将如下所示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

package automationFramework;

 

import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

 

import org.openqa.selenium.WebDriver;

 

import org.openqa.selenium.firefox.FirefoxDriver;

 

import org.testng.annotations.DataProvider;

 

import org.testng.annotations.Test;

 

public class DataProviderTest {

 

private static WebDriver driver;

 

  @DataProvider(name = "Authentication")

 

  public static Object[][] credentials() {

 

        return new Object[][] { { "testuser_1", "Test@123" }, { "testuser_1", "Test@123" }};

 

  }

 

  // Here we are calling the Data Provider object with its Name

 

  @Test(dataProvider = "Authentication")

 

  public void test(String sUsername, String sPassword) {

 

  driver = new FirefoxDriver();

 

      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

 

      driver.get("http://www.store.demoqa.com");

 

      driver.findElement(By.xpath(".//*[@id='account']/a")).click();

 

      driver.findElement(By.id("log")).sendKeys(sUsername);

 

      driver.findElement(By.id("pwd")).sendKeys(sPassword);

 

      driver.findElement(By.id("login")).click();

 

      driver.findElement(By.xpath(".//*[@id='account_logout']/a")).click();

 

      driver.quit();

 

  }

 

}

右键单击测试用例脚本运行测试,然后选择Run As > TestNG Test。给它几分钟完成执行,一旦完成,结果将在TestNg Result窗口中显示如下。

TestNG参数和数据提供者

注意: 由于测试数据提供了两次,上述测试完全执行了两次。

猜你喜欢

转载自blog.csdn.net/ProgrammerFan0101/article/details/83144270