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 进行测试的每个步骤后截取屏幕截图?
				
        
 
            
        基础教程推荐
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
 - Java 实例变量在两个语句中声明和初始化 2022-01-01
 - 验证是否调用了所有 getter 方法 2022-01-01
 - 从 python 访问 JVM 2022-01-01
 - 不推荐使用 Api 注释的描述 2022-01-01
 - 大摇大摆的枚举 2022-01-01
 - 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
 - 多个组件的复杂布局 2022-01-01
 - 在 Java 中创建日期的正确方法是什么? 2022-01-01
 - Java Swing计时器未清除 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				