Q1),我在Selenium-TestNG中有一个现有的框架以及Listener类。我使用TestNG的@AfterTest注释关闭浏览器:
@AfterTest
public void teardown()
{
driver.quit();
}
在这里,当我以普通的Selenium TestNG的形式运行时,监听器类的onTestSuccess方法工作得非常好,而没有黄瓜特性文件:
public void onTestSuccess(ITestResult result)
{
test.log(Status.PASS, "Test case Passed");
try
{
String path = takeScreenshot(driver, "Pass/" + result.getInstanceName() + "/" + result.getMethod().getMethodName());
test.addScreenCaptureFromPath(path, result.getMethod().getMethodName());
}
catch (Exception e)
{
log.fatal("Exception in onTestSuccess Method of Listeners class");
log.fatal(e.getMessage() +" : " + e.getStackTrace());
System.out.println("Exception " + e.getMessage());
System.out.println("Exception " + e.getStackTrace());
}
}
测试通过时,首先调用侦听器类的onTestSuccess()方法,然后调用teardown()方法。所以每件事都很顺利。
现在我在黄瓜中使用这个特性文件:
Feature: Logout feature
Background:
Given User is on Salesforce login page
@SmokeTest @Low @PositiveTest @Logout
Scenario: Logout of application
Given "RMUser" has logged into Salesforce application
When User clicks on logout button
Then Users is logged-out from the application
并使用@After钩子关闭浏览器:
@After
public void teardown() {
driver.quit();
}
现在,当我以TestNG的形式运行这个特性文件时,首先调用钩子文件中的teardown方法,然后调用监听器类的onTestSuccess()方法,因为这会导致错误“异常会话ID为空。在尝试使用驱动程序变量的行调用()?之后使用WebDriver:
String path = takeScreenshot(driver, "Pass/" + result.getInstanceName() + "/" + result.getMethod().getMethodName());
如果在调用@After钩子之前先调用监听器类的onTestSuccess()方法,就可以解决这个问题。
在黄瓜中寻找解决这个问题的解决办法或想法。
( Q2)在以普通Selenimm的形式运行时,区段报告在报表中给出了正确的TestName,但当以TestNG Cuccumber框架运行时,区段报告只显示所有测试名称为"runScenario“。
如何使ExtentReport给出正确的功能文件场景名称而不仅仅是"runScenario"
发布于 2021-06-20 20:17:48
通过从钩子类文件中移除driver.quit();并将其放入监听器类的onTestStart和onTestFailure方法中,解析了driver.quit侦听器类异常:
public void onTestSuccess(ITestResult result)
{
test.log(Status.PASS, "Test case Passed");
try
{
String path = takeScreenshot(driver, "Pass/" + result.getInstanceName() + "/" + result.getMethod().getMethodName());
test.addScreenCaptureFromPath(path, result.getMethod().getMethodName());
}
catch (Exception e)
{
log.fatal("Exception in onTestSuccess Method of Listeners class");
log.fatal(e.getMessage() +" : " + e.getStackTrace());
System.out.println("Exception " + e.getMessage());
System.out.println("Exception " + e.getStackTrace());
}
driver.quit();
}
public void onTestFailure(ITestResult result) {
test.log(Status.FAIL, "Test case Failed");
test.fail(result.getThrowable());
try
{
String path = takeScreenshot(driver, "Fail/" + result.getInstanceName() + "/" + result.getMethod().getMethodName());
test.addScreenCaptureFromPath(path, result.getMethod().getMethodName());
}
catch(Exception e)
{
log.fatal("Exception in onTestFailure Method of Listeners class");
log.fatal(e.getMessage() +" : " + e.getStackTrace());
System.out.println("Exception " + e.getMessage());
System.out.println("Exception " + e.getStackTrace());
}
driver.quit();
}
由于测试用例将通过或失败,因此控制将始终在onTestSuccess或onTestFailure方法中进行,这将在testcase完成执行后退出驱动程序。
https://stackoverflow.com/questions/68049052
复制相似问题