如何attach/embed 在 Cucumber Report 中的自定义软断言期间捕获屏幕截图?

How to attach/embed captured screenshots during custom softAssertion into Cucumber Report?

在调用 softAssertions.assertAll() 时捕获软断言屏幕截图。因此,为了捕获每个软断言失败的屏幕截图,创建了 简单的 CustomAssertion,它扩展到 SoftAssertions 并在其中重写方法名称 onAssertionErrorCollected().

下面是示例代码。

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();
        }
    }
}

在步骤定义文件中:

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

以上代码运行正常。但是,如何将这个截取的attach/embed这个截图放到黄瓜报告中呢? 注意:对于断言,我使用的是 Assertj 库。

您使用 scenario.attach 将场景附加到报告中。这意味着您必须设置一些管道才能将场景放入断言中。

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();
}