Examples of Using TestNG Annotations in Selenium
DeviQA Logo

Examples of Using TestNG Annotations in Selenium

By on 2017-03-08T00:00:00.000Z
Examples of Using TestNG Annotations in Selenium

What is TestNG?

TestNG is a framework for testing which come to correct deficiencies of another well-known testing JUnit framework. The "NG" indicates "Next Generation". A majority of Selenium users use it of its advantages over JUnit. There are a lot of various features of TestNG, but we will consider only the major ones.

Advantages of TestNG over JUnit

Let's learn TestNG advantages over JUnit:

  • Annotations are easier to understand
  • More easily grouping of test cases
  • Testing in parallel threads
  • Annotations in TestNG represent code lines that can manage how the method below them will be executed. The @ symbol is always set before annotation. Small examples are shown below.

    There are two examples of annotation

    Why is necessary to use TestNG in Selenium?

    • Test reports can be generated in a convenient format for reading, which are based on test results of Selenium.
    • WebDriver, unfortunately, does not have a built-in report generation function.

    For instance:

    Test reports

    TestNG makes easier testing.

    The main method is no longer necessary. Priority of tests regulates by annotations for which is optionally methods were static.

    Usual structure(somewhat difficult to read)

    Testing structure (easier to understand)

    TestNG automatically catches the uncaught exceptions without fully executing a test. These exceptions are written as failed steps in the test report.

    Writing an example of a test case

    Let us write a simple test that checks if Mercury Tours' homepage is expected.

    An example of a test case

    Take a look at the some things:

  • A main() method is not required if you use TestNG
  • Using static methods is not necessarily
  • * The @Test annotation was used. @Test is used to identify that the method under it is the test. In this instance, the verifyHomepageTitle() method is a test method.
  • For using TestNG annotations in Selenium, it is necessary to import the package org.testng.annotations.*.
  • The Assert class was used. The Assert class is designed to perform operations of verification in TestNG. For using it, it is * necessary to import the org.testng.Assert package.
  • You can have several test cases (that's why, several @Test annotations) in a one TestNG file.

    Running the Test

    For executing the test, you need to just run the file in Eclipse. Two outputs will be provided - Console and TestNG Results window.

    Testing Results Windows

    Console Window

    TestNG Annotations

    Now review some popular annotations more in details.


    To run the methods in desired sequence, it is necessary to use the "priority" parameter.

    • A value must be set to the each parameter. It is placed after "=".
    • The parameters are in parentheses and placed immediately after the annotation. Example:
    • The parameters

      The picture below shows the sequence in which the methods will execute.

      The sequence in which the methods will execute

      The sequence of executing methods will be shown in the test report.

      The test report. N

      Multiple Parameters

      Besides the priority, abstract test has one the boolean parameter "alwaysRun". When multiple parameters, they are separated by a comma.

      Methods in chronological

      @BeforeTest and @AfterTest

      Multiple Parameters

      Let's look at the selenium examples below.

      @BeforeTest and @AfterTest

      Based on the logic and the above example, we can assume in what order the methods will be executed:

    • 1st - launchBrowser()
    • 2nd - verifyHomepageTitle()
    • 3rd - terminateBrowser()
    • The sequence of annotations can be changed without breaking the chronological order of execution the methods.

      selenium examples

      Execute the code above and see the such result:

      The sequence of annotations

      **@BeforeMethod and @AfterMethod**

      Results after executing the tests

      In the example below, we will verify the presence of two links in Mercury Tours.

      @BeforeMethod and @AfterMethod executing the test

      The order of execution of the action will be the such:

    • Open the homepage and check its title.
    • Click "REGISTER" link and check the title of its page.
    • Return to the homepage and check if the title is still correct.
    • Click "SUPPORT" link and check the title of its page.
    • Return to the homepage and check if the title is still correct.
    • The code below shows how **@BeforeMethod and @AfterMethod** are used to efficiently run the scenario mentioned before.

      links in Mercury Tours

      The test report should be generated by TestNG with the following sequence:

      @BeforeMethod and @AfterMethod are used to efficiently run the scenario

      Summarize, **@BeforeMethod** should hold the methods that are needed to execute before every test while **@AfterMethod** should hold the methods that are needed to run after each test.

      Summary of TestNG Annotations

      TestNG is more flexible because of its annotations. What do they offer to us?

      So there are controlled annotation TestNG:

      1. Annotations **@BeforeSuite**, **@AfterSuite** Indicate the methods that are executed once before / after execution of all tests. It is convenient to have any difficult settings common to all tests, for example, you can create a pool of database connections.
      2. Annotations **@BeforeGroups**, **@AfterGroups** refer to methods that run before / after the first / last test belonging to a given group.
      3. Annotations **@BeforeClass**, **@AfterClass** define the methods that are executed once before / after execution of all tests in the class. The most suitable for testing of a specific service, which does not change its status as a result of the test.
      4. Annotations **@BeforeTest**, **@AfterTest** define methods that are executed once before / after the execution of the test (the one that includes the test classes, not to be confused with the test methods). Here you can store the settings of a group of interrelated services or a service if it is tested by several test classes.
      5. Methods with **@BeforeMethod**, **@AfterMethod** annotations will be executed before/after each test method.

      All of these annotations have the following options:

      • enabled - can be temporarily disabled by setting the value to false
      • groups - define, for which groups will be executed
      • inheritGroups - if true (and the default value is true), the method will inherit the group of the test class
      • timeOut - the time after which "fall down" method and drags along all dependent tests from its description - the name used in the report
      • dependsOnMethods - methods from which they are depended, will first be executed, and then this method
      • dependsOnGroups - the groups from which they are depended
      • alwaysRun - if set to true, will always be called regardless of which groups it belongs to, not applicable to **@BeforeGroups**, **@AfterGroups**.