首页 > 解决方案 > 如何在 catch 块中的 Assert.fail 后继续执行

问题描述

我的代码块在 Assert.fail 之后被终止。但我需要继续执行以使登录页面可用于下一个测试用例

我尝试在正常执行后使用 Assert.fail 进行处理,但在未发生实际失败的情况下捕获了登录页面的屏幕截图

public void getActivityApprovalPending() {
    try {
        wait.until(ExpectedConditions.visibilityOf(activity));
        assertEquals(activity.getText(),"Approval Pending");
        viewTransaction.click();
    } catch (AssertionError e) {
        executor.executeScript("arguments[0].scrollIntoView();", amp.user);
        amp.getUser();
        amp.getLogout();
        Assert.fail("Transaction not in approval pending activity");
    }
}

标签: javaselenium-webdriverassertion

解决方案


如果您想在断言失败后继续执行,请使用软断言而不是断言(它称为硬断言)。

// Create an Soft assert type object
SoftAssert softassert = new SoftAssert();

可以使用任何断言方法:例如

softassert.fail("I am fail")
// It will cause failure but cont. executing next lines of code.

推荐阅读