Thursday, 9 February 2017

SELENIUM: How to Re-Run failed Test cases Automaically in Selenium Using IRetryAnalyzer interface


How to Re-Run failed Test cases Automaically in Selenium Using IRetryAnalyzer interface:



We know that we can execute only the failed test cases using testng-failed.xml.But it is mandatory to re-run this testng-failed.xml manually.

In order to re-run failed test cases automatically we have one interface whicha ic availble in org.testng.IRetryAnalyzer interface.

Purpose of Re running failed test cases if it failed?
While we are working with internal applications, we may notice that our test is failing unnecessarily at different point and once you re-run the same test it passes without any changes
There are multiple reasons why the test fails.
1.Due to the network issue.
2.Due to application downtime.
3.Due to loading issue
....etc.

Note: If the script is failing due to locators,or some other coding errors and some valid reason then we are responsiblity to make modification in our scripts.


Example:
Step1: Write our Script in one class and write @Test(retryAnalyzer=className.class) .Here  we need to give class name with .class exension


package com.rameshsoft.rameshselenium;


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

public class TestScript1 {
         
          @Test
          public void demo()
          {
                   System.out.println("running demo");
          }
         
          @Test(retryAnalyzer=ReRunAgainTest.class)

          public void method()
          {
                  
                   System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");

                   WebDriver driver=new FirefoxDriver();
                  
                   driver.manage().window().maximize();
                  
                   driver.get("https://www.gmail.com");
                  
                   String expTitle="Facebook";
                  
          Assert.assertTrue(expTitle.equalsIgnoreCase(driver.getTitle()));
         
         
         
          }
}



Step2:Now write a separate Class which will implement IRetryAnalyer interface and add unimplemented method

 


package com.rameshsoft.rameshselenium;

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

public class ReRunAgainTest implements IRetryAnalyzer
 {
int minimum=0;
int maximum=2;

          @Override
          public boolean retry(ITestResult result) {
                  
                   if(minimum<=maximum)
                   {
                   System.out.println("Failed Test case name is:"+result.getName());
                   System.out.println("Re-running the test case count is :"+(minimum+1));
                  
                   minimum++;
                   return true;
                  
                   }

return false;
          }
         
}

No comments:

Post a Comment