我想设置一个仅在断言步骤(我用 @Then 标记标记的步骤)之后运行的后步骤挂钩。 这可能吗? 我正在努力
@AfterStep(value = "@Then")
public void afterStep(Scenario scenario) {
try {
takeScreenshot(scenario);
}
catch (IllegalStateException ignore){
}
}
但是它仅在场景标记有 @Then 时运行,它不会查看步骤标签。 欢迎任何帮助! 谢谢!
我担心这不可能开箱即用。
Cucumber 没有提供明确的 API 来访问 @BeforeStep/@AfterStep 挂钩中当前运行的步骤。
总的来说,我看到了两种可能的选择来实现所需的目标(不使用黄瓜对象的反射,其状态无法保证):
public void stepImpl() {
try {
// some step logic
driver.....
} finally {
takeScreenshot(driver, context.getScenario());
}
}
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class ScreenshottingAspect {
@Pointcut("execution(* your.package..*(..)) && @annotation(io.cucumber.java.en.Then)")
public void methodExecution() {
}
@Around("methodExecution()")
public Object captureScreenshot(ProceedingJoinPoint joinPoint) throws Throwable {
try {
return joinPoint.proceed();
} finally {
takeScreenshot(context.getDriver(), context.getScenario());
}
}
}