如何在测试结果列“范围 HTML 报告”中将 TestNg“dependsOnMethods”显示为单独的节点
我正在 Maven 项目中运行 TestNg、java、extent reports 3.1.2、selenium 测试。我有一个测试 - TEST1,它在内部调用 TEST2(使用 TestNg- dependentendsOnMethods)。运行后,当我检查范围报告时,它分别将 TEST1 和 TEST2 的结果显示为 2 个测试,而不是 1 个测试,即 TEST1 带有 2 个子测试,即测试 1 和测试 2。这是当前情况的链接:/image/Yw5k4.jpg
我希望 Extent HTML 报告仅显示 TEST1(在 TESTS 列中),当我单击 TEST1 时,报告应在测试步骤结果列中显示 TEST1 和 TEST2 的状态和屏幕截图(位于单击相应测试时的右侧)。这是 ToBe 情况的链接:/image/E29Mr.jpg
//测试1
@Test(dependsOnMethods = { "TEST2" })
public void TEST1 () throws InterruptedException {
// selenium test java code
}
//测试2
@Test()
public void TEST2 () throws InterruptedException {
// selenium test java code
}
//范围报告
public void onTestSuccess(ITestResult tr) {
logger = extent.createTest(tr.getName());
logger.log(Status.PASS, MarkupHelper.createLabel(tr.getName(),
ExtentColor.GREEN));
System.out.println("TEST PASSED- Screenshot taken");
try {
captureScreen(tr.getName());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String screenshotPath = ".//"+tr.getName()+".png";
try {
logger.pass("Screenshot is below:" +
logger.addScreenCaptureFromPath(screenshotPath));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
最佳答案
最后,通过实现 ITestListener 解决了我的问题。
public static ArrayList<String> methodList = new ArrayList<String>();
@Override
public void onTestStart(ITestResult result) {
if ((chkAndAddArray(finalInstance))) {
return;
}
if (finalmethodname.equalsIgnoreCase(finalInstance.toString())) {
parentTest= extent.createTest(finalmethodname);
} else {
parentTest= extent.createTest(finalInstance);
}
}
boolean chkAndAddArray(String instance) {
if (methodList.contains(instance)) {
System.out.println(instance + " value is already present");
return true;
} else
methodList.add(instance);
System.out.println(instance + " value is not present & now, added");
return false;
}
public void onTestSuccess(ITestResult tr) {
System.out.println("onTestSuccess");
childTest = parentTest.createNode(tr.getName());
childTest.log(Status.PASS, MarkupHelper.createLabel(tr.getName(),
xtentColor.GREEN));
e1.printStackTrace();
}
childTest.pass("Screenshot is below:",
MediaEntityBuilder.createScreenCaptureFromPath(screenshotPath).build());
} catch (IOException e1) {
e1.printStackTrace();
}
}
**对 onTestFailure、onTestSkipped 做了同样的事情。
关于java - 如何在测试结果列范围 HTML 报告中将 TestNg "dependsOnMethods"显示为单独的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57510722/