TestNG Reporters

TestNG is a Framework and so far we have already seen the many different powerful features of TestNG. It almost gives you all the important things you are required to complete the Framework.

TestNG Reporter Logs

TestNG also gives us the logging facility for the test. For example during the running of test case user wants some information to be logged in the console. Information could be any detail depends upon the purpose. Keeping this in mind that we are using Selenium for testing, we need the information which helps the User to understand the test steps or any failure during the test case execution. With the help of TestNG Logs it is possible to enable logging during the Selenium test case execution.

In selenium there are two types of logging. High level logging and Low level logging. In low level logging you try to produce logs for the every step you take or every action you make in your automation script. In high level logging you just try to capture main events of your test.

Everybody has their own style of logging and I have mine too. I am also a big fan of Log4j logging and that’s why I do not mix log4j logging with testng logging but on the same side I make to use of both of its. I perform low level logging with log4j and high level logging with testng reporter logs.

How to do it…

1) Write a test case for Sign In application and implement Log4j logging on every step.

2) Insert Reporter logs on the main events of the test.

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

packageautomationFramework;

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;

publicclassReporterLogs{

privatestaticWebDriver driver;

privatestaticLogger Log=Logger.getLogger(Log.class.getName());

@Test

publicstaticvoidtest(){

DOMConfigurator.configure("log4j.xml");

driver=newFirefoxDriver();

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.

3) Run the test by right click on the test case script and select Run As > TestNG Test.

Your Log4j logging output will look like this:

But your Reporters log will look like this:

Log4j logging will help you to report a bug or steps taken during the test, on the other hand reporters log will help you to share the test status with leadership. As leadership is just interested in the test results, not the test steps.

I also use reporter’s logs on the verification during the test. For example

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 Asserts

TestNG also gives us the power to take decisions in the middle of the test run with the help of Asserts. With this we can put various checkpoints in the test. Asserts are the most popular and frequently used methods while creating Selenium Scripts.In selenium there will be many situations in the test where you just like to check the presence of an element. All you need to do is to put an assert statement on to it to verify its existence.

Different Asserts Statements

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

packageautomationFramework;

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;

publicclassAsserts{

privatestaticWebDriver driver;

@Test

publicvoidf(){

driver=newFirefoxDriver();

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

Note: Assert true statement fails the test and stop the execution of the test, if the actual output is false. Assert.assertFalse() works opposite of Assert.assertTrue(). It means that if you want your test to continue only if when some certain element is not present on the page. You will use Assert false, so it will fail the test in case of the element present on the page.

2) Assert.assertEquals()

1

2

3

4

5

6

7

8

9

10

11

12

13

@Test

publicvoidtest(){

StringsValue="Lakshay Sharma";

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

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

System.out.println(sValue);

}

It also works the same way like assert true and assert fail. It will also stop the execution, if the value is not equal and carry on the execution, if the value is equal.