1

In soft assertion screenshot get captured when softAssertions.assertAll() is called. So to capture screenshots for each soft Assertion failure, created simple CustomAssertion which extends to SoftAssertions and in that override a method name onAssertionErrorCollected().

Below is the sample code.

public class CustomSoftAssertion extends SoftAssertions {

    public CustomSoftAssertion() {
    }

    @Override
    public void onAssertionErrorCollected(AssertionError assertionError) {
        File file = TestRunner.appiumDriver.getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(file, new File(System.getProperty("user.dir") + File.separator + "ScreenShots" + File.separator + LocalDate.now().format(DateTimeFormatter.ofPattern("MMMM_dd_yyyy")) + File.separator + "demo.png"), true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the step definition file:

CustomSoftAssertion softAssertion = new CustomSoftAssertion();
softAssertion.assertThat(isLogin).isTrue();

Above code is working properly. But, how to this captured attach/embed this screenshots into the cucumber report? Note: For Assertion I am using Assertj library.

DeepakVerma
  • 179
  • 1
  • 3
  • 14
  • Maybe a duplicate of https://stackoverflow.com/q/68510688/9714611? Or are you having issues adding the screenshot to the report? – Stefano Cordio Nov 02 '21 at 16:54
  • @Stefano Cordio, Thanks for sharing the link. To capture screen shot, I used hook `if ((scenario.isFailed()) || (scenario.getStatus().toString().equals("SKIPPED"))) { byte[] screenshot = testContext.getAppiumDriver().getScreenshotAs(OutputType.BYTES); scenario.attach(resizeBytesImage(screenshot), "image/png", scenario.getName()); }` But, screen shot taken by customAssertion() is not attached into the report. – DeepakVerma Nov 02 '21 at 20:08

1 Answers1

2

You attach scenarios to the report by using scenario.attach. This means you'll have to setup some plumbing to get the scenario into the assertion.

public class CustomSoftAssertion extends SoftAssertions {

    private final Scenario scenario;

    public CustomSoftAssertion(Scenario scenario) {
        this.scenario = scenario;
    }

    @Override
    public void onAssertionErrorCollected(AssertionError assertionError) {
        // take screenshot and use the scenario object to attach it to the report
        scenario.attach(....)
    }
}

private CustomSoftAssertion softAssertion;

@Before
public void setup(Scenario scenario){
    softAssertion = new CustomSoftAssertion(scenario);
}

@After // Or @AfterStep
public void assertAll(){
    softAssertion.assertAll();
}

@Given("example")
public void example(){
    softAssertion.assertThat(isLogin).isTrue();
}
M.P. Korstanje
  • 10,426
  • 3
  • 36
  • 58
  • screenshots is attached now., but in report it is highlighted in green. https://imgur.com/ehBHwLg How this can be change?. I am using maven-cucumber-reporting – DeepakVerma Nov 03 '21 at 12:12
  • 1
    Your afterhook failed because that is where `assertAll` is invoked. If you want the soft assertion to fail in your step, you have to invoke `assertAll` in your step or in an after step hook. – M.P. Korstanje Nov 03 '21 at 12:21