TestNG Framework In Selenium WebDriver: A Simple Guide In 2 Points

img
Ajay Ohri
Share

Introduction

TestNG is a framework that performs automatic testing. NG in TestNG stands for Next Generation. TextNG utilizes annotations and makes end-to-end testing comfortable. TestNG Framework generates a proper report about the number of test cases that are passed, failed, and non-attended. 

  1. How to use TestNG in selenium? 
  2. TestNG Report Generation: It contains two types of report

1. How to use TestNG in selenium? 

TestNG Framework in Selenium gives an option of a TestNG-failed.xml file in the test-output folder.

  • Selenium TestNG framework:

TestNG framework with Selenium helps to generate the report that possesses the information of the number of test cases runs, test cases passed, failed, and skipped. It becomes easy to execute multiple test cases on various browsers by using TestNG. Usually, Web drivers lack a native mechanism for producing reports however, TestNG is capable of generating reports in readable formats. 

TestNG framework explains the way tests are coded. It is competent to handle the unidentified exceptions without annulling the test prematurely

  • Test Annotations: 

Annotations in testing are easy to understand which don’t require methods to be static. Annotations in TestNG, which are always preceded with @symbol, are code lines that assist to control the method execution. 

There is a parameter called “alwaysRun” which is assigned to either “true” or “false”. More than two parameters can be used by separating them with the comma. 

Now let us understand TestNG Tutorial: what does @Before and @After imply: 

@BeforeTest: Methods will be executed before the first test case in the TestNG file under these annotations. 

@AfterTest: Methods will be executed after all the test cases in the TestNG file are performed under these annotations.

@BeforeMethod: Method with this annotation will be executed before any other method in each test case. 

@AfterMethod: Method with this annotation will be performed after all the method in each test case. 

Similarly, there are various other annotations such as: 

@BeforeSuite

@AfterSuite

@BeforeGroups

@AfterGroups

@BeforeClass

@AfterClass

Prioritization:  

Multiple @Test annotations can be used in a single TestNG. Methods that are annotated by @Test are by default executed alphabetically. 

However, if there is a requirement to execute the methods in distinctive order, then the parameter “priority” can be utilized. Parameters are considered to be the keywords that remodel the annotations functioning. 

Values can be assigned to parameters by assigning a”=” attached to those parameters followed by the values. Parameters are embedded with pair of parentheses that are set after the annotations those similar to the code snippet. 

E.g: @Test(priority = 0) 

Selenium TestNG will perform the @Test annotation that posses the least priority value up to the highest. Priority values can be in random values rather than being consecutive. The execution of the TestNG HTML report is dependent on the ascending value of priority. 

  • Disabling Test Cases: 

There are usually times when there are numerous code lines that possess hundreds of test cases. In such cases, it will be very difficult to disable one test method. But it doesn’t require deleting any part of the code, instead, that test method can be disabled. 

This can be done by utilizing the parameters. 

E.g @Test(Priority=1, enabled= True)

      @Test(Priority=0, enabled= True)

      @Test(enabled=False)

In the above example, it can be observed that the test that has priority are enabled after giving the test method the true value and the one that doesn’t need to execute is given a false value. 

  • Method Dependency: 

There can be a situation where we want that a particular piece of code should be performed only after it fulfils a specific condition or after the execution of the particular method. It is possible by utilizing the “dependsOnMethod”This is considered to be the situation of method dependency where the method will be performed after the execution of another method. 

  • Grouping: 

Several methods are part of the test case in the code. Parameters such as groups can be used where a group name can be assigned to several test cases. In this way, we can choose to execute the group instead of the entire code. 

e.g: @Test(groups={“MyGroup”})

       @Test(groups={“MyGroup”})

  • TestNG Assertions: 

Pass/fail condition in test methods can be determined by using the assertions. The tests will pass/fail that is based on the true/false. 

Other commands/lines of code in that test will be skipped when the assertion fails. The next line of code will be performed when the assertion is a success.

  • Report generation: 

While we use Selenium framework TestNG or any other automation tool, we are utilizing web applications for the performance of operations. However, our purpose is not only to perform application under test. We need to test the application, locate bugs and report it to the development team or higher management. TestNG framework will generate a test-output folder at the root of the project after its execution.

2. TestNG Report Generation: It contains two types of report

Index.html: It contains information like an error, time, reporter, logs, time, TestNG XML files. 

emailable-report.html: It is the summarized report of the current test execution. It contains a Test case message in green and red highlight, where green is for pass test cases and red is for failed test cases. 

ITestListener Interface is used to customize the real-time report. Hence while executing the bunch of test cases in a TestNG suite, implementation of ITestListener interface. Corporate environment although need more customized reports. 

Conclusion

TestNG framework has many advantages over JUnit such as it is easier to understand and use annotations, easy grouping of test cases, and test cases help in the creation of parallel tests. TestNG window is considered to be more useful since it gives a graphical output of the test result. TestNG Data provider passes the parameters into the test function that transfers distinctive values in test cases in one execution. 

If you want to learn more about Java then check out Jigsaw Academy’s Master Certificate In Full Stack Development â€“ a 170 hour-long live online course. It is the first & only program on Full Stack Development with Automation and AWS Cloud. Happy learning!

ALSO READ

Related Articles

loader
Please wait while your application is being created.
Request Callback