这真让我抓狂!任何人都可以帮我这个吗?我正在与监听器一起运行重试例程。当我从Eclipse中运行xml时,它重试就好了。但是,当我从命令行使用批处理文件运行相同的xml时,它甚至从未启动retrylistener ...
这是从Eclipse中运行时的输出:
**********Running RetryListener
.... (Other extraneous output)
**********onStart
[Invoker 1636165903] Keeping method InitiateBuyPlan.setup(java.lang.String, java.lang.String, java.lang.String)[pri:0, instance:com.bpms.tests.InitiateBuyPlan@24150b2d] for class [TestClass name=class com.bpms.tests.InitiateBuyPlan]
[Invoker 1636165903] Keeping method InitiateBuyPlan.tearDown(org.testng.ITestResult)[pri:0, instance:com.bpms.tests.InitiateBuyPlan@24150b2d] for class [TestClass name=class com.bpms.tests.InitiateBuyPlan]
[Invoker 1636165903] Keeping method InitiateBuyPlan.setup(java.lang.String, java.lang.String, java.lang.String)[pri:0, instance:com.bpms.tests.InitiateBuyPlan@24150b2d] for class [TestClass name=class com.bpms.tests.InitiateBuyPlan]
[Invoker 1636165903] Invoking @BeforeMethod InitiateBuyPlan.setup(java.lang.String, java.lang.String, java.lang.String)[pri:0, instance:com.bpms.tests.InitiateBuyPlan@24150b2d]
In properties file: 'C:\Users\gmartz\Desktop\BPMSLogs\output.properties', Property: 'log.filename' was set to: 'C:\Users\gmartz\Desktop\BPMSLogs\07232015_1242'
In properties file: 'C:\Users\gmartz\Desktop\BPMSLogs\output.properties', Property: 'dateTime' was set to: '07232015 1242'
2015-07-23 10:42:51,730 INFO [Log] ****************************************************************************************
2015-07-23 10:42:51,740 INFO [Log] ****************************************************************************************
2015-07-23 10:42:51,740 INFO [Log] $$$$$$$$$$$$$$$$$$$$$ com.bpms.tests.InitiateBuyPlan $$$$$$$$$$$$$$$$$$$$$$$$$
2015-07-23 10:42:51,740 INFO [Log] ****************************************************************************************
2015-07-23 10:42:51,740 INFO [Log] ****************************************************************************************
**********onTestStart
[Invoker 1636165903] Invoking com.bpms.tests.InitiateBuyPlan.testInitiateBuyPlan
2015-07-23 10:43:08,059 INFO [Log] Entering Retry
2015-07-23 10:44:14,288 INFO [Log] Retry
以下是从命令行运行时的输出:
.... (Other extraneous output)
**********onStart
[Invoker 562850178] Keeping method InitiateBuyPlan.setup(java.lang.String, java.lang.String, java.lang.String)[pri:0, instance:com.bpms.tests.InitiateBuyPlan@62d438fd] for class [TestClass name=class com.bpms.tests.InitiateBuyPlan]
[Invoker 562850178] Keeping method InitiateBuyPlan.tearDown(org.testng.ITestResult)[pri:0, instance:com.bpms.tests.InitiateBuyPlan@62d438fd] for class [TestClass name=class com.bpms.tests.InitiateBuyPlan]
[Invoker 562850178] Keeping method InitiateBuyPlan.setup(java.lang.String, java.lang.String, java.lang.String)[pri:0, instance:com.bpms.tests.InitiateBuyPlan@62d438fd] for class [TestClass name=class com.bpms.tests.InitiateBuyPlan]
[Invoker 562850178] Invoking @BeforeMethod InitiateBuyPlan.setup(java.lang.String, java.lang.String, java.lang.String)[pri:0, instance:com.bpms.tests.InitiateBuyPlan@62d438fd]
In properties file: 'C:\Users\gmartz\Desktop\BPMSLogs\output.properties', Property: 'log.filename' was set to: 'C:\Users\gmartz\Desktop\BPMSLogs\07212015_1135'
In properties file: 'C:\Users\gmartz\Desktop\BPMSLogs\output.properties', Property: 'dateTime' was set to: '07212015 1135'
2015-07-21 09:35:34,490 INFO [Log] ********************************************************************************
2015-07-21 09:35:34,506 INFO [Log] ********************************************************************************
2015-07-21 09:35:34,506 INFO [Log] $$$$$$$$$$$$$$ com.bpms.tests.InitiateBuyPlan $$$$$$$$$$$$$$$$$$
2015-07-21 09:35:34,506 INFO [Log] ********************************************************************************
2015-07-21 09:35:34,506 INFO [Log] ********************************************************************************
**********onTestStart
[Invoker 562850178] Invoking com.bpms.tests.InitiateBuyPlan.testInitiateBuyPlan
**********onTestFailure
2015-07-21 09:36:31,282 FATAL [Log] Screenshot can be found at : C:\Users\gmartz\Desktop\BPMSLogs\testInitiateBuyPlan_FAIL_1437496591116.png
2015-07-21 09:36:31,282 FATAL [Log] ****Test Failed****
2015-07-21 09:36:31,282 FATAL [Log] ******Aborting*****
2015-07-21 09:36:31,282 FATAL [Log] ******Aborting*****
2015-07-21 09:36:31,282 FATAL [Log] ******Aborting*****
2015-07-21 09:36:31,282 FATAL [Log] com.bpms.tests.InitiateBuyPlan Failed with the following: java.lang.AssertionError: null
2015-07-21 09:36:31,282 INFO [Log] XXXXXXXXXXXXXX -E---N---D- XXXXXXXXXXXXXXXXXX
2015-07-21 09:36:31,282 INFO [Log] X
2015-07-21 09:36:31,282 INFO [Log] X
2015-07-21 09:36:31,282 INFO [Log] X
2015-07-21 09:36:31,282 INFO [Log] X
请注意,从命令行运行时,如何不运行Running RetryListener,并调用onTestFailure而不是重试代码。
这是代码:
的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
retry listener.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)
{
System.out.println("**********Running RetryListener");
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;
}
}
test listener.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.hml
<?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>
我找到了解决方案。 eclipse运行的testng jar文件比命令行环境使用的testng jar文件要新得多。它在selenium路径中使用了testng jar文件。我更新了那个罐子,它完美地工作了......