【发布时间】:2018-05-15 10:27:53
【问题描述】:
我正在为我的 testng-selenium-java 项目使用 ExtentReports 和 ItestListener, 我的听众为 ExtentReports 的失败测试用例截屏,但问题是我的 testng.XML 中有多个类,我一口气运行它们,一个接一个地做不同的事情并拥有自己的驱动程序。
在失败的情况下,Ilistener 的代码是 -
public void onTestFailure(ITestResult iTestResult)
{
System.out.println("I am in onTestFailure method " +
getTestMethodName(iTestResult) + " failed");
//Get driver from BaseTest and assign to local webdriver variable.
Object testClass = iTestResult.getInstance();
WebDriver webDriver = ((BaseTest) testClass).getDriver();
//Take base64Screenshot screenshot.
String base64Screenshot = "data:image/png;base64,"+((TakesScreenshot)webDriver).
getScreenshotAs(OutputType.BASE64);
//Extentreports log and screenshot operations for failed tests.
ExtentTestManager.getTest().log(LogStatus.FAIL,"Test Failed",
ExtentTestManager.getTest().addBase64ScreenShot(base64Screenshot));
}
如何确保在测试用例失败时采用失败测试用例类的驱动程序,因为在上面的代码中始终只给出一个类的驱动程序,而不是当前类的驱动程序。
【问题讨论】:
-
乍一看,这里没有明显的问题。也许您还可以包含一些 BaseTest 类的片段,其中包含
driver字段定义和初始化,因为这将有助于识别任何问题。
标签: java selenium-webdriver testng extentreports