Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

TestNG Tutorials 69 : Rerun Failed Test Method Using IRetryAnalyzer Interface – Implementing With Listener – IAnnotationTransformer

Posted on 03/16/2025 By admin

Hello Folks,

We have already learnt about IRetryAnalyzer interface in previous post. We know why we use it and how can we use it.

From previous post, you know that we can pass implemented class of IRetryAnalyzer as an value to attribute named “retryAnalyzer” at @Test annotation. You need to add it each and every @Test annotation method if you want retry mechanism for each test method. It might be difficult if you have a large number of Test methods. If you want to remove the logic, you may need to remove it from each @Test method. 

So there is an another way of implementing the same if you want to add retry mechanism for each and every Test method. That way is using Listeners named IAnnotationTransformer. IAnnotationTransformer is a powerful listener provided by TestNG to give you a chance to modify a TestNG annotation read from your test classes. You can get or change the values you need by calling any of the getter/setters on the ITest interface.  This interface has only method named “transform”.

Method “transform has an parameter of type ITestAnnotation interface which has method to set (setRetryAnalyzer) and get (getRetryAnalyzer) RetryAnalyzer at run time. You need to pass your implemented class of IRetryAnalyzer interface as an argument to setRetryAnalyzer() method.

Step to add IRetryAnalyzer as a Listener:

package RetryAnalyzer;

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class RetrySetup implements IRetryAnalyzer{
        
        // Variable to keep counter of retry
        private int retryCount = 0;
        
        // Variable to define max retry count
        private static final int maxRetryCount = 3;

        @Override
        public boolean retry(ITestResult result) {
                // We need to implement a cap, otherwise execution might go indefinite loop. 
                if(retryCount < maxRetryCount)
                {
                        retryCount++;
                        System.out.println("Retrying Test method : "+result.getName() + " for " + retryCount +" times. ");
                        return true;
                }
                return false;
        }

}
2. Create an implemented class of IAnnotationTransformer interface and override transform method as :
package RetryAnalyzer;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

public class MyAnnotationTransfer implements IAnnotationTransformer{

        @Override
        public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
                annotation.setRetryAnalyzer(RetrySetup.class);
                
        }

}

3. Create a TestNG class with some @Test method.
package RetryAnalyzer;

import org.testng.Assert;
import org.testng.annotations.Test;

public class RetryTest {
        
        private static int counter= 0;

        @Test
        public void method1()
        {
                counter++;
                if(counter
Uncategorized

Post navigation

Previous Post: TestNG Tutorials
Next Post: Method 2: getCssValue() : What, When and How to use?

Related Posts

selenium testng tutorials Uncategorized
selenium webdriver Uncategorized
Handling Frames/IFrames In Selenium WebDriver : Part 3 Uncategorized
March 21, 2019 – Make Selenium Easy Uncategorized
Cucumber Tutorial 1 – Setup a Basic Maven-Cucumber 5 -Junit Project in Eclipse Uncategorized
Part 3: Usages Of Javascripts In Selenium: Problem You Might Face While Executing Javascript Commands In Selenium Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark