我正在开发一个基于 Selenium/testng/java/gradle 的项目,使用 ThreadLocal 方法来处理 webdriver 和程度测试对象。每当我的测试用例失败时,我都会使用 RetryListener 重新运行失败的测试用例一次。如果第二次通过,我的结果仍然在范围报告中显示为“失败”(注意-所有迭代都记录在 html 报告中的单个测试节点中)。 stackoverflow 上有很多关于此的讨论(所有这些都有点旧)。不过,我从那里尝试了一些逻辑。但这并没有帮助。
我创建了一个扩展 TestListenerAdapter 的监听器,并在 onFinish 方法中编写了逻辑以删除重复的失败。
我的听众类:
import java.util.Iterator;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
public class TestListener extends TestListenerAdapter {
private static ExtentReports extent;
public ExtentTest test;
public TestListener() {
this.test = CustomExtentTest.getInstance().getExtentTest();
}
@Override
public void onFinish(ITestContext context) {
Iterator<ITestResult> skippedTestCases = context.getSkippedTests().getAllResults().iterator();
while (skippedTestCases.hasNext()) {
ITestResult skippedTestCase = skippedTestCases.next();
ITestNGMethod method = skippedTestCase.getMethod();
if (context.getSkippedTests().getResults(method).size() > 0) {
System.out.println("Removing:" + skippedTestCase.getTestClass().toString());
skippedTestCases.remove();
extent.removeTest(test);
}
}
}
public void onTestStart(ITestResult result) {
}
public void onTestSuccess(ITestResult result) {
}
public void onTestFailure(ITestResult result) {
}
public void onTestSkipped(ITestResult result) {
}
public void onTestFailedButWithinSuccessPercentage(ITestResult result) { }
public void onStart(ITestContext context) {
}
}
我的记者类(class):
public abstract class Reporter{
public RemoteWebDriver driver;
public static ExtentReports extent;
public ExtentTest test;
public String testcaseName, testcaseDec, author ;
public String category;
private Logger log=Logger.getLogger(Reporter.class);
@BeforeSuite (alwaysRun = true)
public void startReport(ITestContext c) throws IOException
{
String timeStamp = new SimpleDateFormat("MM.dd.yyyy.HH.mm.ss").format(new Date());
System.setProperty("org.freemarker.loggerLibrary", "none");
try {
Properties prop=new Properties();
prop.load(new FileInputStream("./Resources/log4j.properties"));
PropertyConfigurator.configure(prop);
} catch (Exception e) {
log.error(e);
}
log.debug("Configuring Extent Report...");
ExtentSparkReporter reporter;
String extentreportpath;
String reportName=this.getClass().getName().substring(29, 33).toUpperCase() +" - Test Report";
String suiteName = c.getCurrentXmlTest().getSuite().getName()+"-Test Report";
if (suiteName.contains("Default suite")||suiteName.contains("Failed suite"))
{
suiteName =reportName;
}
extentreportpath="./reports/"+suiteName+"_"+timeStamp+".html";
reporter = new ExtentSparkReporter(extentreportpath);
extent = new ExtentReports();
extent.attachReporter(reporter);
extent.setSystemInfo("URL", ReadPropertyFile.getInstance().getPropertyValue("URL"));
reporter.loadXMLConfig("./Resources/extent-config.xml");
reporter.config().setTheme(Theme.DARK);
reporter.config().setReportName(suiteName);
log.info("Extent Report Configured Successfully");
}
@Parameters({"browser"})
@BeforeClass(alwaysRun = true)
public ExtentTest report(@Optional("browser") String browser)
{
if(ReadPropertyFile.getInstance().getPropertyValue("RunMode").equalsIgnoreCase("STANDALONE"))
{
if(browser.equals("browser")) {
browser = ReadPropertyFile.getInstance().getPropertyValue("Browser");
}
}
test = extent.createTest(testcaseName, testcaseDec +" <br /><br />Browser Name: "+browser+" <br /><br />Category: "+category);
test.assignAuthor(author);
CustomExtentTest extenttst = CustomExtentTest.getInstance();
extenttst.setExtentTest(test);
return test;
}
public abstract long takeSnap();
public void reportStep(String desc,String status,boolean bSnap)
{
MediaEntityModelProvider img=null;
if(bSnap && !status.equalsIgnoreCase("INFO"))
{
long snapNumber=100000L;
snapNumber=takeSnap();
try
{
img=MediaEntityBuilder.createScreenCaptureFromPath("images/"+snapNumber+".jpg").build();
}
catch(IOException e)
{
log.error(e);
}
}
if(status.equalsIgnoreCase("pass"))
{
test.log(Status.PASS, desc, img);
}
else if(status.equalsIgnoreCase("fail"))
{
test.log(Status.FAIL, desc, img);
}
else if(status.equalsIgnoreCase("INFO"))
{
test.log(Status.INFO, desc,img);
}
}
public void reportStep(String desc,String status)
{
reportStep(desc,status,true);
}
@AfterSuite (alwaysRun=true )
public void stopReport()
{
log.debug("Stopping and preparing the report...");
extent.flush();
log.info("Report prepared successfully");
}
}
用于范围测试的 ThreadLocal 类:
public class CustomExtentTest {
private CustomExtentTest() {
}
private static final ThreadLocal<CustomExtentTest> _localStorage = new ThreadLocal<CustomExtentTest>(){
protected CustomExtentTest initialValue() {
return new CustomExtentTest();
}
};
public static CustomExtentTest getInstance() {
return _localStorage.get();
}
ExtentTest testextent;
public ExtentTest getExtentTest() {
return this.testextent;
}
public void setExtentTest(ExtentTest testextent) {
this.testextent = testextent;
}
}
我刚刚发现我总是从测试监听器中获取失败测试的大小为 1。因此,无法继续进行重复测试删除。
- 范围报告 - 4.1.6
- Selenium - 3.141.59
- 测试 - 7.1.0
请就上述内容提供想法。
最佳答案
当您重新运行 selenium 脚本来获取报告时,您应该删除以前的报告
关于java - 程度报告 4.1.6 和 Selenium 未显示最新测试结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62306537/