TestNG日志和断言

本文详细介绍了TestNG框架中的报告功能和断言使用。TestNG提供了丰富的日志记录选项,如低级别和高级别日志,有助于理解和调试测试用例。在测试执行期间,Log4j用于详细日志,而TestNG Reporter日志则适用于概述测试状态。此外,文章还探讨了TestNG的断言机制,包括assertTrue、assertFalse和assertEquals,这些断言在自动化测试中扮演关键角色,确保测试正确性和流程控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TestNG报告

TestNG是一个框架,到目前为止,我们已经看到了TestNG的许多不同的强大功能。它几乎为您提供了完成框架所需的所有重要事项。

TestNG Reporter日志

TestNG还为我们提供了测试的记录功能。例如,在运行测试用例期间,用户希望在控制台中记录一些信息。信息可以是任何细节取决于目的。牢记我们正在使用Selenium进行测试,我们需要有助于用户理解测试步骤或测试用例执行期间的任何失败的信息。在TestNG Logs的帮助下,可以在Selenium测试用例执行期间启用日志记录。

在硒中有两种类型的日志记录。高级别日志记录和低级别日志记录。在低级别日志记录中,您尝试为您执行的每个步骤或在自动化脚本中执行的每个操作生成日志。在高级别日志记录中,您只需尝试捕获测试的主要事件。

每个人都有自己的伐木方式,我也有。我也是Log4j日志记录的忠实粉丝,这就是为什么我不将log4j日志记录与testng日志记录混合,而是在同一方面我使用它们。我执行与log4j的低水平日志记录高级日志记录使用TestNG记者日志。

怎么做…

1)为登录应用程序编写测试用例,并在每一步执行Log4j日志记录。

2)在报告的主要事件上插入Reporter日志。

 

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

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

package automationFramework;

 

import java.util.concurrent.TimeUnit;

 

import org.apache.log4j.Logger;

 

import org.apache.log4j.xml.DOMConfigurator;

 

import org.openqa.selenium.By;

 

import org.openqa.selenium.WebDriver;

 

import org.openqa.selenium.firefox.FirefoxDriver;

 

import org.testng.Reporter;

 

import org.testng.annotations.Test;

 

import utility.Log;

 

public class ReporterLogs {

 

private static WebDriver driver;

 

private static Logger Log = Logger.getLogger(Log.class.getName());

 

    @Test

 

public static void test() {

 

DOMConfigurator.configure("log4j.xml");

 

        driver = new FirefoxDriver();

 

        Log.info("New driver instantiated");

 

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

 

        Log.info("Implicit wait applied on the driver for 10 seconds");

 

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

 

        Log.info("Web application launched");

 

        // Our first step is complete, so we produce a main event log here for our reports.

 

        Reporter.log("Application Lauched successfully | ");

 

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

 

        Log.info("Click action performed on My Account link");

 

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

 

        Log.info("Username entered in the Username text box");

 

        driver.findElement(By.id("pwd")).sendKeys("Test@123");

 

        Log.info("Password entered in the Password text box");

 

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

 

        Log.info("Click action performed on Submit button");

 

        // Here we are done with our Second main event

 

        Reporter.log("Sign In Successful | " );

 

        driver.findElement(By.id("account_logout"));

 

        Log.info("Click action performed on Log out link");

 

        driver.quit();

 

        Log.info("Browser closed");

 

        // This is the third main event

 

        Reporter.log("User is Logged out and Application is closed | ");

 

}

 

}

3)右键单击测试用例脚本运行测试,然后选择Run As > TestNG Test

您的Log4j日志记录输出将如下所示:

TestNG的-记者-1

但您的Reporters日志将如下所示:

TestNG记者和断言

Log4j日志记录将帮助您报告测试期间的错误或步骤,另一方面,记者日志将帮助您与领导分享测试状态。领导只是对测试结果感兴趣,而不是测试步骤。

我还在测试期间使用记者的日志进行验证。例如

 

1

2

3

4

5

6

7

8

9

if(Text1.equals(Text2)){

 

Reporter.log("Verification Passed forText");

 

}else{

 

Reporter.log("Verification Failed for Text");

 

}

 

TestNG断言

TestNG还使我们能够在Asserts的帮助下在测试运行过程中做出决策。有了这个,我们可以在测试中放置各种检查点。在创建Selenium脚本时,断言是最常用和最常用的方法。 在selenium中,测试中会有很多情况,您只想检查元素的存在。您需要做的就是在其上放置一个断言语句来验证它的存在。

不同的断言语句

1)Assert.assertTrue()和Assert.assertFalse() 

 

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

package automationFramework;

 

import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

 

import org.openqa.selenium.WebDriver;

 

import org.openqa.selenium.WebElement;

 

import org.openqa.selenium.firefox.FirefoxDriver;

 

import org.testng.Assert;

 

import org.testng.annotations.Test;

 

public class Asserts {

 

private static WebDriver driver;

 

  @Test

 

  public void f() {

 

  driver = new FirefoxDriver();

 

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

 

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

 

      // Here driver will try to find out My Account link on the application

 

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

 

      //Test will only continue, if the below statement is true

 

      //This is to check whether the link is displayed or not

 

      Assert.assertTrue(myAccount.isDisplayed());

 

      //My Account will be clicked only if the above condition is true

 

      myAccount.click();

 

  }

 

}

注意:  如果实际输出为false,则断言true语句将使测试失败并停止测试的执行。Assert.assertFalse()与Assert.assertTrue()相反。这意味着,如果您希望测试仅在页面上不存在某些特定元素时继续。您将使用Assert false,因此如果页面上存在元素,它将无法通过测试。

2)Assert.assertEquals()

 

1

2

3

4

5

6

7

8

9

10

11

12

13

  @Test

 

  public void test() {

 

  String sValue = "Lakshay Sharma";

 

  System.out.println(" What is your full name");

 

  Assert.assertEquals("Lakshay Sharma", sValue);

 

  System.out.println(sValue);

 

  }

它的工作方式与assert true和assert fail相同。如果值不相等,它也将停止执行,如果值相等则继续执行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值