It’s possible to master the entire Selenium TestNG framework in just two hours, but we’re here to show you how to get started. It is imperative to bear in mind that mastering a framework such as this necessitates a significant amount of time, diligent practice, and practical experience. Are you prepared to begin? We’ll focus on the main parts and steps to set up a basic framework.
Introduction & Installation
TestNG is an automation testing framework, in which NG stands for “Next Generation “.
- TestNG is inspired by JUnit. It’s an advanced part of testing.
- Using TestNG we can generate report and easily get status of test cases like the test passed, failed or skip test case.
Why to use TestNG?
Let’s understand a test case scenario.
- Let assume we have 5 test cases to test.
- We create a separate method for every test case.
- Now when we run every test case and assume the 4th test case fails.
- So in this case, only correct the 4th method, and you can run the 4th one only as the rest three is already done.
- This can be achieved only through TestNG.
- It provides the flexibility to run test cases based on choice.
Installation
- In eclipse, Select the Help option.
- From the help option, Select Eclipse Marketplace option.
- In the search bar, search for TestNG and click the installation button.
- Accept the term and condition.
- In case we get trust selected option.
- Select all trust selected option and accept the risk.
- Now eclipse will ask for restart. So click the restart button and the TestNG is installed in eclipse.
Note: Please follow the above video to install TestNG on eclipse.
Annotations
In TestNG, annotations are markers or labels that are incorporated into the code to convey instructions or metadata regarding the treatment of methods during test execution. Annotations help explain how test methods work, set up test suites, and plan what to do before and after a test. The annotations for TestNG are written in Java and are preceded by the ‘@’ symbol.
Here are some commonly used TestNG annotations and their purposes:
- @Test: Marks a method as a test method. TestNG considers methods annotated with @Test as test cases to be executed.
- @BeforeSuite: Denotes a method that should run before all tests in a test suite. Typically used for setting up global configurations or resources required for the entire suite.
- @AfterSuite: Denotes a method that should run after all tests in a test suite have executed. Used for cleanup or teardown tasks after all tests have completed.
- @BeforeTest: Marks a method to run before any test method belonging to the specified <test> tag in the testng.xml file.
- @AfterTest: Marks a method to run after all the test methods belonging to the specified <test> tag in the testng.xml file have executed.
- @BeforeClass: Indicates a method to be run before the first test method in the current class runs.
- @AfterClass: Indicates a method to be run after all the test methods in the current class have run.
- @BeforeMethod: Denotes a method that should run before each test method execution.
- @AfterMethod: Denotes a method that should run after each test method execution.
- @DataProvider: Specifies a method as a data provider for test methods. It supplies data to the test methods.
- @Parameters: Marks parameters that should be passed to a test method. Used in conjunction with the <parameters> tag in the testng.xml file.
These annotations let you control the test execution flow, setup, and teardown procedures, making TestNG test automation efficient and flexible.
Priority in TestNG
- In TestNG, the priority attribute can be used to specify the execution order of test methods within a test class. The sequence in which test methods are executed can be controlled by assigning priorities to test methods. TestNG tests methods in order of priority, starting with the lowest priority value.
- If two or more test methods are of the same priority, TestNG will run them in a random order.
- It is important to note that using priorities for test methods should be used sparingly and only when necessary. Relying heavily on priorities can lead to test code that is hard to maintain and understand.
- Instead, it’s often better to structure your tests in a way that they can run independently of each other. Priorities can be useful for cases where certain tests must be executed in a specific order due to dependencies or setup requirements.
Dependencies in TestNG
- In TestNG, dependencies let you specify relationships between test methods, so that certain methods are executed only after other methods are successful. This feature is especially useful when you need to establish a sequence of operations or when one test method depends on the output of another.
- It is also possible to specify dependencies for groups of test methods using the attribute dependsOnGroups. Additionally, you can control the execution order using the attribute in conjunction with dependencies.priority
- By utilizing dependencies, it is possible to establish a logical sequence of test execution and ensure that tests are executed in the appropriate sequence, in accordance with the requirements of your test scenarios. This helps in maintaining test integrity and reliability.
Grouping in TestNG
- Grouping in TestNG allows you to categorize your test methods into logical groups, making it easier to run subsets of tests based on their classification. Grouping is particularly useful when you want to run specific sets of tests, such as smoke tests, regression tests, or tests related to a specific feature. Grouping is particularly useful when you want to run specific sets of tests related to a specific feature.
- By using grouping, you can organize your tests more effectively and run specific sets of tests based on their purpose or characteristics, making your test suite easier to manage and maintain.
Listeners in TestNG
TestNG listeners allow you to customize and enhance the behavior of your test execution by providing hooks into different stages of the test lifecycle. You can do this by providing hooks into different stages of the test lifecycle. Listeners are used to perform actions such as logging, reporting, and test manipulation before, during, and after test execution. TestNG offers a variety of built-in listeners, and it is also possible to develop your own customized listeners to meet your specific requirements.
Here are some commonly used TestNG listeners:
- ITestListener: This listener interface provides methods to perform actions before and after a test, as well as before and after each test method within a test class. It’s commonly used for logging, reporting, and capturing test results.
- ISuiteListener: This interface allows you to perform actions at the beginning and end of a suite of tests. It’s useful for setting up global configurations or resources required for the entire suite.
- IInvokedMethodListener: This interface provides methods to perform actions before and after each test method invocation. It can be used for dynamic test case manipulation or to implement custom retry mechanisms.
- IReporter: This interface allows you to customize the test report generation process. You can implement your own logic to generate custom reports or integrate TestNG with third-party reporting tools.
- IAnnotationTransformer: This interface enables you to modify test annotations (such as @Test, @BeforeMethod, @AfterMethod) dynamically at runtime. It’s useful for parameterizing tests or excluding certain methods based on runtime conditions.
Extent reports in TestNG
ExtentReports is a widely utilized reporting library for Java-based test automation frameworks, such as TestNG. It provides HTML reports with detailed information about test results, logs, screenshots, and more. By integrating ExtentReports with TestNG, it is possible to generate visually appealing and informative reports for your test suites.