TestNG Tutorials

Passing data to DataProvider from Excel sheet

In this example we will see how to pass the data to Dataproviders by reading the data from excel sheet. DataProvider helps to send multiple sets of data to a test method. But here we need to make sure that the array returned by the dataprovider should match with the test method parameters.

We will write a simple program in which we will validate login screen by taking multiple usernames and passwords. The annotated method must return object[][] where each object[] can be assigned to the test method one as username and the other parameter as password.

Take Screenshot and place it in a folder with Test Class name

In this tutorial, we will see taking a screen shot with test name and placing it in a folder by creating a folder with Test Class Name.

As we are taking screen shots on failure, we need to add logic in creating a screen shot and naming it with test name and place it in its test class name respectively.

We can get the test class name using 'result.getInstanceName()'. Test class name looks some thing like "com.pack.sample.HomePage". But we need to create a folder name with test class name i.e 'HomePage'.

Retry executing only Failed Tests using TestNG

There may be many reasons for a Test case getting failed, may be due to element not found or time out exception or stale element exception etc. Normally in automation after executing scripts/tests, we will check for the results and if the test fails because of above reasons we will re-run then again.

Instead of that we can ask testNG to execute the failed test cases again for X (we can define) number of times and check for the updated results.

Assertions in TestNG

What is Assertion????
Asserts helps us to verify the conditions of the test and decide whether test has failed or passed. A test is considered successful ONLY if it is completed without throwing any exception. There is a difference between SoftAssert and Hard Assert.

Let us focus more on using Assertions (Hard assert). Also check SoftAssert example in TestNG for more details on soft asserts.

Preserver Order in Testng

If you want your classes / methods to be run in an unpredictable order, then we should go for preserve-order attribute in testng. In TestNg bydefault the preserve-order attribute will be set to 'true', this means, TestNG will run your tests in the order they are found in the XML file.

We will try to execute the below example, by taking three classes. For the first one, We will set the preserve-order attribute to false and check for the the Output.

Create three classes as
ClassOne.java
ClassTwo.java
ClassThree.java

Parallel Execution of Classes in TestNG

TestNG provides an ability to run test classes in parallel. By using parallel execution of classes, each class will be started and executed simultaneously in different threads.

Let us look at basic example for Parallel Execution of Classes using testng.xml.

We will create a Two classes with Two test methods each and try to execute in different threads.

Create class and name it as : TestParallelClassOne.java

Parallel Execution of test methods in TestNG

TestNG provides multiple ways to execute tests in separate threads. In testng.xml, if we set 'parallel' attribute on the tag to 'tests', testNG will run all the ‘@Test’ methods in tag in the same thread, but each tag will be in a separate thread.

If we want to run methods/classes in separate threads, we need to set 'parallel' attribute on the tag to 'methods' / 'classes'

Parameterization in TestNG using testng.xml

TestNG allows the user to pass values to test methods as arguments by using parameter annotations through testng.xml file.

Some times it may be required for us to pass values to test methods during run time. Like we can pass user name and password through testng.xml instead of hard coding it in testmethods. or we can pass browser name as parameter to execute in specific browser.

Let us now try to understand parameterization with a basic example.