DEV Community

Terence Pan
Terence Pan

Posted on

Set up Cucumber reports with screenshots

Maven setup Surefire plugin

Below pom.xml configuration will generate a html and json report. The reports will have a screenshot when tests fail.

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.version}</version>
                <configuration>
                    <properties>
                        <configurationParameters>
                            cucumber.plugin=pretty,html:target/site/TestRun.html,json:target/site/TestRun.json
                            cucumber.publish.quiet=true
                            cucumber.publish.enabled=false
                        </configurationParameters>
                    </properties>
                </configuration>
            </plugin>
        </plugins>
    </build>
Enter fullscreen mode Exit fullscreen mode

Cucumber After method

    @After
    public void afterEach(Scenario scenario){
        if(scenario.isFailed()){
            final byte[] screenshot = page.screenshot(new Page.ScreenshotOptions()
                    .setPath(Paths.get("screenshot.png")));
            scenario.attach(screenshot, "image/png", scenario.getName());
        }
        browserContext.close();
    }
Enter fullscreen mode Exit fullscreen mode

Here, we are checking if the scenario is failed and if so, it will attach a screenshot.

Intentionally create test failure

  Scenario Outline: User is able to request a Demo
    Given User navigates to PHPTRAVELS Demo Page
    When User fills in Demo Request Form <firstName>, <lastName>, <businessName>, <email>
    When User calculates and fills wrong result
    When User clicks submit
    Then Verify thank you text
    Examples:
      | firstName | lastName | businessName | email               |
      | "ABC"     | "DEF"    | "Main Inc."  | "whatever@main.com" |
Enter fullscreen mode Exit fullscreen mode

Here on the register.feature file, we intentionally filled out the wrong result causing the test to fail.

run: mvn clean install

check report path as noted in pom.xml: target/site/TestRun.html

Image description

As always code is on available on Github

Top comments (0)