Retry failed Test Cases in Selenium WebDriver using TestNG - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Saturday, 27 June 2015

Retry failed Test Cases in Selenium WebDriver using TestNG

Sometimes while executing automated Test Cases some of the Test Cases gets failed and we try to Re-Run the test cases.

With TestNG we can Re-Run the failed TestCases X number of times.

For Re-Run the failed test case we need to implement TestNG IRetryAnalyzer.

Below is the sample code

Create a Retry Class



package testSuite;

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

public class Retry implements IRetryAnalyzer {
    private int retryCount = 0;
    private int maxRetryCount = 4;

// Below method returns 'true' if the test method has to be retried else 'false'
//and it takes the 'Result' as parameter of the test method that just ran
    public boolean retry(ITestResult result) {
        if (retryCount < maxRetryCount) {
            System.out.println("Retrying test " + result.getName() + " with status "
                    + getResultStatusName(result.getStatus()) + " for the " + (retryCount+1) + " time(s).");
            retryCount++;
            return true;
        }
        return false;
    }
   
    public String getResultStatusName(int status) {
                String resultName = null;
                if(status==1)
                                resultName = "SUCCESS";
                if(status==2)
                                resultName = "FAILURE";
                if(status==3)
                                resultName = "SKIP";
                                return resultName;
    }

}


Create an other class 'RetryListener' by implementing 'IAnnotationTransformer'. We need to setRetryAnalyzer for iTestAnnotation. In the example below, add the above class name.


package testSuite;

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

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

public class RetryListener implements IAnnotationTransformer {

                @Override
                public void transform(ITestAnnotation testannotation, Class testClass,
                                                Constructor testConstructor, Method testMethod)         {
                                IRetryAnalyzer retry = testannotation.getRetryAnalyzer();

                                if (retry == null)                {
                                                testannotation.setRetryAnalyzer(Retry.class);
                                }

                }
}


Now create a Class containing Test Cases
package testSuite;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestNGExampleTests {
       WebDriver driver;
       String baseURL = "https://www.linkedin.com/";

       @BeforeClass
       public void setup() {
              driver = new FirefoxDriver();
              driver.manage().window().maximize();
       }

       @Test(priority=1)
       public void verifyLoginPageText() {
              driver.navigate().to(baseURL);
              System.out.println("Verify login page test started");
              WebElement element = driver.findElement(By.cssSelector(".header>h2"));
              String headerText = element.getText();
              Assert.assertEquals(headerText, "Get stted – it’s free.");//assert will fail and test case will be retried
       }

       @Test(priority=2)
       public void verifyForgotPasswordPage() {
              driver.navigate().to(baseURL);
              System.out.println("Verify Forgot password page test started");
              WebElement element = driver.findElement(By.linkText("Forgot your password?"));
              element.click();
              WebElement pageTextElement = driver.findElement(By.cssSelector(".flow-login-content>fieldset>h1"));
              String pageText = pageTextElement.getText();
              Assert.assertEquals(pageText, "Wron text");//assert will fail and test case will be retried
              //Assert.assertEquals(pageText, "Change your password");
       }
      
      
}


We need to add the Listener to testng.xml file. Below is syntax to add listener for RetryListener.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  
 
  <test name="Test">
    <classes>
      <class name="testSuite.TestNGExampleTests"/>
       <listeners>
        <listener class-name="testSuite.RetryListener"/>
  </listeners>
 
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->


Output

[TestNG] Running:
  C:\Users\neeraj.bakhtani\Desktop\myKeywordFW\testng.xml

Verify login page test started
Retrying test verifyLoginPageText with status FAILURE for the 1 time(s).
Verify login page test started
Retrying test verifyLoginPageText with status FAILURE for the 2 time(s).
Verify login page test started
Retrying test verifyLoginPageText with status FAILURE for the 3 time(s).
Verify login page test started
Retrying test verifyLoginPageText with status FAILURE for the 4 time(s).
Verify login page test started
Verify Forgot password page test started
Retrying test verifyForgotPasswordPage with status FAILURE for the 1 time(s).
Verify Forgot password page test started
Retrying test verifyForgotPasswordPage with status FAILURE for the 2 time(s).
Verify Forgot password page test started
Retrying test verifyForgotPasswordPage with status FAILURE for the 3 time(s).
Verify Forgot password page test started
Retrying test verifyForgotPasswordPage with status FAILURE for the 4 time(s).
Verify Forgot password page test started

===============================================
Suite
Total tests run: 10, Failures: 10, Skips: 0
=============================================== 

4 comments:

  1. It provide null pointer error while running

    ReplyDelete
  2. Hi Kunal,
    Just for your reference.The above code is runnable and shown as a sample.
    U have to implement the above requirement according to you framework.
    Pls check on which line it is giving Null Pointer.
    Or pls provide some more details to debug the same.

    ReplyDelete
  3. You can give try to sample here as well..
    http://easytestautomation.blogspot.in/2015/06/selenium-rerun-failed-test-scripts.html

    ReplyDelete
  4. I have multiple test cases. It re runs only one test case. if first test case got failed it re runs it but when it move to next and that test case fails it doesnt re run it.

    ReplyDelete