Allure没有正确报告失败,但有例外(nosuchelementexception)

问题描述 投票:0回答:1
  1. krGlobalPage.softAssertionTestCall2(softAssertion);即使由于krGlobalPage.softAssertionTestCall1(softAssertion);而失败,也必须运行

nosuchelementexception

  1. 测试应该失败,并且在“魅力”报告上显示红色标记。
@Test(description = "softAssert")
public void softAssert(Method method) {
SoftAssert softAssertion = new SoftAssert();
KrGlobalPage krGlobalPage = new KrGlobalPage(prop, driver, wait);
krGlobalPage.softAssertionTestCall1(softAssertion);
krGlobalPage.softAssertionTestCall2(softAssertion);
softAssertion.assertAll();
}

[assertions.assertEquals("msg1", "msg2", "msg1&msg2 not equal");-正常工作且报告失败。

    @Test(description = "softAssert")
    public void softAssert(Method method) {
        SoftAssert softAssertion = new SoftAssert();
        KrGlobalPage krGlobalPage = new KrGlobalPage(prop, driver, wait);
        krGlobalPage.softAssertionTestCall1(softAssertion);
        krGlobalPage.softAssertionTestCall2(softAssertion);
        softAssertion.assertAll();
    }

    @Step("SoftAssertion test1")
    public void softAssertionTestCall1(SoftAssert assertions) {
        Allure.step("softAssert Method Was Started", Status.SKIPPED);
        assertions.assertTrue(false);
    }

    @Step("SoftAssertion test2")
    public void softAssertionTestCall2(SoftAssert assertions) {
        Allure.step("softAssert Method Was Started", Status.SKIPPED);
    }

请参见下面的屏幕截图。enter image description here

但是当assertions.assertEquals((driver.findElement(By.xpath("(//nav[@class='topline']//nav[@class='without-seperator1']/a)[1]").getAttribute("href")), "www.cort.com");上没有此类元素例外时,将不执行krGlobalPage.softAssertionTestCall2(softAssertion)。请查看屏幕截图。

[由于krGlobalPage.softAssertionTestCall2(softAssertion);而导致krGlobalPage.softAssertionTestCall1(softAssertion);失败后如何运行nosuchelementexception,并且使用Red颜色指示符失败了测试用例,如第一个屏幕截图所示?

为什么assertions.assertTrue(false);正常工作?

为什么assertions.assertEquals((driver.findElement(By.xpath("(//nav[@class='topline']//nav[@class='without-seperator1']/a)[1]").getAttribute("href")), "www.cort.com")不能按预期工作?

    @Test(description = "softAssert")
    public void softAssert(Method method) {
        SoftAssert softAssertion = new SoftAssert();
        KrGlobalPage krGlobalPage = new KrGlobalPage(prop, driver, wait);
        krGlobalPage.softAssertionTestCall1(softAssertion);
        krGlobalPage.softAssertionTestCall2(softAssertion);
        softAssertion.assertAll();
    }

    @Step("SoftAssertion test1")
    public void softAssertionTestCall1(SoftAssert assertions) {
        Allure.step("softAssert Method Was Started", Status.SKIPPED);
        assertions.assertEquals((driver.findElement(By.xpath("(//nav[@class='topline']//nav[@class='without-seperator1']/a)[1]").getAttribute("href")), "www.cort.com");
    }

    @Step("SoftAssertion test2")
    public void softAssertionTestCall2(SoftAssert assertions) {
        Allure.step("softAssert Method Was Started", Status.SKIPPED);
    }

enter image description here

java selenium testng allure
1个回答
0
投票

TestNG软断言以及代码中的任何错误/异常,包括所有Se​​lenium的错误(NoSuchElementExceptionElementNotVisibleException,...)彼此无关。

如果您不想因Selenium的错误而失败,请跳过它们。

@Step("SoftAssertion test1")
public void softAssertionTestCall1(SoftAssert assertions) {
    Allure.step("softAssert Method Was Started", Status.SKIPPED);

    // Use find elements to bypass NoSuchElementException error, if there is no such element. 
    // findElements list with size=0 if there is no element without any exception, and > 0 if element exist
    List<WebElement> mylinks = driver.findElements(By.xpath("(//nav[@class='topline']//nav[@class='without-seperator1']/a)[1]");

    // Assert if mylinks size > 0, using TestNG soft assertions
    assertions.assertTrue(mylinks.size() > 0, "Link with without-seperator1 class exist");

    // Assert if link has "www.cort.com" href
    if (mylinks.size() > 0)
        assertions.assertEquals(mylinks.get(0).getAttribute("href"), "www.cort.com");
}

您可以找到here,如何检查元素是否存在。

© www.soinside.com 2019 - 2024. All rights reserved.