Thursday, 7 February 2019

Retry failed test case in testng Selenium

How can we execute failed test cases in Selenium using TestNG


There are multiple ways to execute Failed test cases in Selenium using TestNG:
  • 'testng-failed.xml'
  • IRetryAnalyzer

testng-failed.xml


If any test case is failed, testng-failed.xml is generated in the reports output directory(Target directory). We can simply run above xml to rerun the failed test cases.

Location of  testng-failed.xml




Running testng-failed.xml
In eclipse - Simply right click on xml file and run as testng


IRetryAnalyzer


TestNG has an Interface to implement to be able to have a chance to retry a failed test that is IRetryAnalyzer.

If you want TestNG to automatically retry a test whenever it fails. You can use a retry analyzer.
To retry a failed test you have bind retry analyzer to each test which you want to invoke on test failure.

How can we bind retry analyzer at Test Level

You can use retryAnalyzer attribute and assign it to implemented class of IRetryAnalyzer interface at test level. As shown below:

@Test(retryAnalyzer = RetryAnalyzerImpl.class)

After binding it to the test level, TestNG automatically invokes the retry analyzer to determine if TestNG can retry a test case again in case of failure for a specified number of times.

Complete implementation of IRetryAnalyzer class is shown below:



Sample Test class implementation is shown below:


Setup RetryAnalyzer at Class/Suite Level


We can setRetryAnalyzer in setup method - @BeforeClass, @BeforeSuite instead of @Test as shown in RetryTest.java

Sample Test class implementation is shown below:


Wednesday, 6 February 2019

Run Test Sequentially in Selenium Testng

There are multiple ways to run test sequentially in Selenium using TestNG
  • Defining each test in order in XML
  • Using 'Priority' attribute 

Lets say we have 4 test in a class as shown below. By default test will run in an unpredictable order.
package com.example;

import org.testng.annotations.Test;

public class SequentialTest {

 @Test
 public void firstTest() {
  System.out.println("1");
 }

 @Test
 public void secondTest() {
  System.out.println("2");
 }

 @Test
 public void thirdTest() {
  System.out.println("3");
 }
 
 @Test
 public void fouthTest() {
  System.out.println("4");
 }
}

How can we run test sequentially using TestNG in Selenium


Testng.XML


Using Testng.xml we can run test sequentially. By default, TestNG will run your tests in the order they are found in the XML file. If you want the classes and methods listed in this file to be run in an unpredictable order, set the preserve-order attribute to false.

By default preserve-order is true.


@Test (priority=" ")


Using 'priority' attribute we can run test sequentially using TestNG in Selenium. We can define priority for each test methods. Lower priorities will be scheduled first.