retry failed case works in Eclipse, doesn't when run from command line

13 views
Skip to first unread message

Greg Martz

unread,
Jul 21, 2015, 1:50:06 PM7/21/15
to testng-users
This is driving me crazy!  Can anyone assist me with this?  I'm running a retry routine with the listener.  When I run the xml from within Eclipse, it retries just fine.  However, when I run the same xml from a command line with a batch file, it never even starts the retrylistener...  Here is the code:

run.bat

set classpath=.\bin;libs\log4j\*;libs\selenium\*;libs\selenium\libs\*
@echo off
java org.testng.TestNG SuiteEndToEndTests.xml -d %HOMEDRIVE%%HOMEPATH%\desktop\BPMSLogs

----------------------------------------------------------------------------------------------------

RetryListener.java

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
{
    public void transform(ITestAnnotation annotation, Class testClass,
            Constructor testConstructor, Method testMethod)
    {
        IRetryAnalyzer retry = annotation.getRetryAnalyzer();
        if (retry == null)
        {
            annotation.setRetryAnalyzer(Retry.class);
        }
    }
}

-------------------------------------------------------------------------------------------------

Retry.java

package com.bpms.utility;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class Retry implements IRetryAnalyzer
{
    private int retryCount        = 0;
    private int maxRetryCount     = 5;   // retry a failed test 5 additional times

    @Override
    public boolean retry(ITestResult result)
    {
System.out.println("**********onTestFailure");
    String desktopDirectory = System.getProperty("user.home") + "\\Desktop\\BPMSLogs\\";
String testMethodName = result.getName().toString().trim();
String screenShotName = testMethodName + "_FAILRETRY_" + System.currentTimeMillis() + ".png";
String imagePath;
    WebDriver driver = com.bpms.utility.Utility.driver;


if (retryCount <= maxRetryCount)
        {
     if (((RemoteWebDriver) driver).getSessionId() != null)
    {
    retryCount++;
    //Take a snapshot of the screen for the failure
    imagePath = Utility.takeScreenShot(driver, desktopDirectory, screenShotName);
    Log.error("Screenshot can be found at : " + imagePath);
    Log.error(result.getInstanceName() + " Failed with the following: " + result.getThrowable());
    Log.error("****Test Failed - Retrying****");
    }
           return true;
        }
        return false;
    }
}

--------------------------------------------------------------------------------------

TestListener.java

package com.bpms.utility;

import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class TestListener implements ITestListener
{

    @Override
    public void onFinish(ITestContext context)
    { 
System.out.println("**********onFinish");
    }

    // Following are all the method stubs that you do not have to implement
  
    @Override
    public void onTestStart(ITestResult result)
    {
        // TODO Auto-generated method stub
System.out.println("**********onTestStart");
    }
  
    @Override
    public void onTestSuccess(ITestResult result)
    {
        // TODO Auto-generated method stub
System.out.println("**********onTestSuccess");
    }
  
    @Override
    public void onTestFailure(ITestResult result)
    {
        // TODO  Auto-generated method stub
System.out.println("**********onTestFailure");
    String desktopDirectory = System.getProperty("user.home") + "\\Desktop\\BPMSLogs\\";
String testMethodName = result.getName().toString().trim();
String screenShotName = testMethodName + "_FAIL_" + System.currentTimeMillis() + ".png";
String imagePath;
    WebDriver driver = com.bpms.utility.Utility.driver;

//Take a snapshot of the screen for the failure
imagePath = Utility.takeScreenShot(driver, desktopDirectory, screenShotName);
Log.fatal("Screenshot can be found at : " + imagePath);
Log.fatal("****Test Failed****");
Log.fatal("******Aborting*****");
Log.fatal("******Aborting*****");
Log.fatal("******Aborting*****");
Log.fatal(result.getInstanceName() + " Failed with the following: " + result.getThrowable());
Log.endTestCase(this.getClass().getName());
driver.quit();
System.exit(0);
   }

    @Override
    public void onTestSkipped(ITestResult result)
    {
        // TODO Auto-generated method stub
System.out.println("**********onTestSkipped");
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result)
    {
        // TODO Auto-generated method stub
System.out.println("**********onTestFailedButWithinSuccessPercentage");
   }

    @Override
    public void onStart(ITestContext context)
    {
        // TODO Auto-generated method stub
System.out.println("**********onStart");
    }
}  // ends the class

----------------------------------------------------------------------------------------------

Suite.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SuiteEndToEnd" parallel="false" verbose="10" thread-count="2">
<listeners>
        <listener class-name="com.bpms.utility.RetryListener"/>
    <listener class-name="com.bpms.utility.TestListener"/>
</listeners>
<test name="End to End Test Suite">
<parameter name="browser" value="firefox"/>
<parameter name="debug" value="true"/>
<parameter name="defaults" value="false"/>
<classes>
<class name="com.bpms.tests.InitiateBuyPlan"/>
</classes>
</test>
</suite>

--------------------------------------------------------------------------------------------------


Thanks!
Greg
Reply all
Reply to author
Forward
0 new messages