Monday, 2 May 2016

How TestNG makes Selenium tests easier

So far we had been doing Selenium tests without generating a proper format for the test results. From this point on, we shall tackle how to make these reports using a test framework called TestNG.
TestNG is a testing framework that overcomes the limitations of another popular testing framework called JUnit. The "NG" means "Next Generation". Most Selenium users use this more than JUnit because of its advantages. There are so many features of TestNG, but we will only focus on the most important ones that we can use in Selenium.

Advantages of TestNG over JUnit

There are three major advantages of TestNG over JUnit:
  •         Annotations are easier to understand
  •         Test cases can be grouped more easily
  •         Parallel testing is possible
Annotations in TestNG are lines of code that can control how the method below them will be executed. They are always preceded by the @ symbol. A very early and quick example is the one shown below.
Annotations will be discussed later in the section named "Annotations used in TestNG", so it is perfectly ok if you do not understand the above example just yet. It is just important to note for now that annotations in TestNG are easier to code and understand than in JUnit.
The ability to run tests in parallel is available in TestNG but not in JUnit, so it is the more preferred framework of testers using Selenium Grid.
TestNG can generate reports based on our Selenium test results.
  • WebDriver has no native mechanism for generating reports.
  • TestNG can generate the report in a readable format like the one shown below.
TestNG simplifies the way the tests are coded
  • There is no more need for a static main method in our tests. The sequence of actions is regulated by easy-to-understand annotations that do not require methods to be static.

  • Uncaught exceptions are automatically handled by TestNG without terminating the test prematurely. These exceptions are reported as failed steps in the report.

Installing TestNG in Eclipse

Step 1
  • Launch Eclipse.
  • On the menu bar, click Help.
  • Choose the "Install New Software..." option.
Step 2
In the Install dialog box, click the Add button
Step 3
  1. In "Name", type TestNG.
  2. In "Location", type http://beust.com/eclipse.
  3. Click OK
Step 4
  • Notice that "TestNG - http://beust.com/eclipse" was populated onto the "Work with:" textbox.
  • Check the "TestNG" check box as shown below, then click Next.
  • Note: In the latest Eclipse (Kepler) you don't have a checkbox for TestNG, instead you click on question mark (help) icon which will open up the form, and you can select all and installation will continue as for the remaining instructions. Thanks Jana for the tip!
Step 5
  • Click Next again on the succeeding dialog box until you reach the License Agreement dialog.
  • Click "I accept the terms of the license agreement" then click Finish.
Step 6
Wait for the installation to finish
If you encounter a Security warning, just click OK
Step 7
When Eclipse prompts you for a restart, just click Yes.
Step 8
After the restart, verify if TestNG was indeed successfully installed. Click Window > Preferences and see if TestNG is included on the Preferences list.

3 comments:

  1. My friend Suggest me this blog and I can say this is the best blog to get the basic knowledge.Thank you so much for this Selenium Training in Chennai blog

    ReplyDelete
  2. Best blog.Got to learn new things.Thanks for this Blog SAP HR Training in Chennai

    ReplyDelete
  3. Great place to learn about the new things and got great knowledgeSAP MM Training in Chennai

    ReplyDelete