Search code examples
javagradlecucumberallureselenide

How I can attach screenshot of failed Cucumber step to Allure report exactly in step, not in Tear Down section?


I can't add screenshot of failed cucumber step under the step in Allure report.

I use allure-cucumber4-jvm:2.12.1, cucumber-java:4.3.1 and try to add screenshot of tested web app to allure report EXACTLY under failed cucumber step. So I tried to use cucumber's @AfterStep and @After annotations and add screenshot in annotated methods under scenario.isFailed() conditions. But the problem is that screenshots are added after all steps in Tear Down section. The only difference is that @After method is called once and @AfterStep methods is called by every step.

@AfterStep
public void addScreenshotOnFailed(Scenario scenario) throws IOException {
        if (scenario.isFailed()) {
            try {
                Allure.addAttachment("Failed step", new FileInputStream(Screenshots.takeScreenShotAsFile()));
            } catch (FileNotFoundException ignored) {}
        }
    }

I expect screenshot of tested web app exactly under failed cucumber step? but actually I have it in Tear Down section after all scenario


Solution

  • Idea is using the events to capture the step status and embed the screenshot. If I am correct, As per current design AfterStep does not execute when the step is failed. So we have to use AfterConfiguration in hooks.

    Ruby: Here is the ruby implementation.

    AfterConfiguration do |scenario, config|
        config.on_event :test_step_finished do |event|
            if event.result.failed?
              # get screenshot
            end
        end
      end
    

    Check this thread regarding the allure implementation.