How to capture a screenshot after each step in tests with JAVA and Cucumber?(如何在使用 JAVA 和 Cucumber 进行测试的每个步骤后截取屏幕截图?)
问题描述
在运行集成测试时,在每个步骤之后捕获屏幕截图的最佳方法是什么?
What would be the best way to capture screenshots after each step when running integration tests?
使用 Selenium(3.0.1) 和 Cucumber(1.2.4) 用 Java 编写测试.
Tests are written in Java using Selenium(3.0.1) and Cucumber(1.2.4).
测试后截屏的代码如下,但我需要在每个用@Given、@When、@Then注释的方法之后截屏.
Code for taking a screenshot after a test is below, but I need a screenshot after each method annotated with @Given, @When, @Then.
@After
public void after(Scenario scenario){
final byte[] screenshot = driver.getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
}
感谢您的任何提示.
推荐答案
使用 Aspects 解决了这个问题.非常棘手,请注意注释:
Solved this using Aspects. Was pretty tricky, note the annotation:
@After("call(public * cucumber.runtime.StepDefinitionMatch.runStep(..)) && within(cucumber.runtime.Runtime)")
下面是完整的代码,由 Viviana Cattenazzi 编写.
Below is the full code, written by Viviana Cattenazzi.
pom.xml
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.4</version>
</dependency>
</dependencies>
......
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.10</version>
<configuration>
<weaveDependencies>
<weaveDependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
</weaveDependency>
</weaveDependencies>
<showWeaveInfo>true</showWeaveInfo>
<source>1.8</source>
<target>1.8</target>
<complianceLevel>1.8</complianceLevel>
</configuration>
<executions>
<execution>
<phase>process-test-classes</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
.......
StepsInterceptor.java
@Aspect
public class StepsInterceptor {
@After("call(public * cucumber.runtime.StepDefinitionMatch.runStep(..)) && within(cucumber.runtime.Runtime)")
public void beforeRunningStep(JoinPoint thisJoinPoint) throws Exception {
try {
StepDefinitionMatch stepDefinitionMatch = (StepDefinitionMatch) thisJoinPoint.getTarget();
Step step = (Step) retrievePrivateField(stepDefinitionMatch, "step");
String stepName = step.getKeyword().trim();
if ("Given".equals(stepName) || "When".equals(stepName)) {
Object theRealStepDef = extractJavaStepDefinition(stepDefinitionMatch);
// take screen shot here
}
} catch (ClassCastException exc) { ....
}
}
}
这篇关于如何在使用 JAVA 和 Cucumber 进行测试的每个步骤后截取屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在使用 JAVA 和 Cucumber 进行测试的每个步骤后截取屏幕截图?


基础教程推荐
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01