我正在尝试连接使用 AspectJ 运行的 Cucumber-Serenity 库,但我的切入点似乎没有被触发。
SerenityAOP.java:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import gherkin.ast.Feature;
import gherkin.ast.ScenarioDefinition;
import net.thucydides.core.model.TestStep;
import net.thucydides.core.steps.StepEventBus;
@Aspect
public class SerenityAOP {
@Pointcut("execution(void cucumber.runtime.formatter.SerenityReporter.startOfScenarioLifeCycle(..))")
public void beforeStartScenario() {}
@Pointcut("execution(void cucumber.runtime.SerenityObjectFactory.stop())")
public static void beforeStop(){}
@Before("beforeStartScenario()")
public void beforeStartScenarioAdvice(JoinPoint joinPoint) {
Object[] signatureArgs = joinPoint.getArgs();
Feature currentFeature = (Feature)signatureArgs[0];
String scenarioName = signatureArgs[1].toString();
ScenarioDefinition scenarioDefinition = (ScenarioDefinition)signatureArgs[2];
System.out.println("ASPECT: Starting test case for feature "+currentFeature+" with scneario: "+scenarioName);
}
@Before("beforeStop()")
public void beforeStopAdvice() {
boolean status = StepEventBus.getEventBus().aStepInTheCurrentTestHasFailed();
String stepDescription = StepEventBus.getEventBus().getCurrentStep()
.transform(TestStep::getDescription)
.get();
System.out.println("ASPECT: Testcase: "+stepDescription+" status = "+status);
}
}
相关依赖:
<dependency>
<groupId>net.thucydides</groupId>
<artifactId>thucydides-junit</artifactId>
<version>0.9.275</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-core</artifactId>
<version>${serenity.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit</artifactId>
<version>${serenity.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-cucumber</artifactId>
<version>${serenity.cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<scope>test</scope>
<version>3.8.1</version>
</dependency>
但是在执行期间我没有在任何地方看到我的打印线。
有什么方法我需要以不同的方式挂钩这些内部库调用吗?还是我的切入点选择器错误?
编辑: 添加评论以防当前答案被删除
使用黄瓜钩我无法检测到何时feature
开始新的。我需要知道它们的名称feature
以及scenario
它们何时开始。我还没有找到用事件总线做到这一点的方法。