故事中不可能同时有成功和失败的结果吗?
Lifecycle:
After:
Outcome: FAILURE
Given I capture page screenshot
Given I close browser
Outcome: SUCCESS
Given I close browser
Scenario: Sample one
Given I open browser
When I do something
Scenario: Sample two
Given I open browser
When I do another thing
例如,对于失败,我想在关闭浏览器之前截取屏幕截图。如果成功我只想关闭浏览器。
我知道我可以在所有场景结束时关闭浏览器,只保留失败结果。我想知道是否有办法在生命周期之后执行此操作。 谢谢。
最佳答案
引用OP:
For example, for failures I want to take a screenshot before closing the browser. If successful I just want to close the browser.
现在,有趣的问题是您使用哪个框架来进行断言? 我假设您使用与 JBehave 捆绑在一起的 Junit,因为 JBehave 依赖于知道 JUnit 抛出的异常是否存在错误。
这个想法是: a) 发生错误时抛出异常(因此需要检查每一步) b) 截屏 c) 继续测试(即关闭浏览器)
因此,为了引发异常,您实际上不需要做太多事情,因为在使用 JUnit 的 Assert 语句时这是自动完成的。
例如
Assert(username.equals("expected_user").isTrue();
如果上述失败,将会抛出异常。 您可以这样捕获它:
public class RunnerExtension implements AfterTestExecutionCallback {
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
Boolean testResult = context.getExecutionException().isPresent();
System.out.println(testResult); //false - SUCCESS, true - FAILED
}
}
@ExtendWith(RunnerExtension.class)
public abstract class Tests {
}
摘自这个答案: JUnit5 - How to get test result in AfterTestExecutionCallback
所以基本上,在执行每个断言之后,您会覆盖标准行为。在上面的例子中,你可以添加(当抛出异常时-->截屏)。
下面是 Selenium-Java 的截图代码:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
希望以上内容对您有所帮助!
关于java - JBehave - 一个故事中不可能有多个结果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52861022/