魅力未正确报告故障

时间:2019-03-26 01:48:58

标签: java selenium testng allure

    即使krGlobalPage.softAssertionTestCall2(softAssertion);由于而失败,
  1. 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");上没有nosuchelementexception时,将不执行krGlobalPage.softAssertionTestCall2(softAssertion)。请查看屏幕截图。

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

为什么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

2 个答案:

答案 0 :(得分: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,以及如何检查元素是否存在。

答案 1 :(得分:0)

我发现最好的方法-并在“魅力”报告中显示为失败-尝试捕获findelement并在catch块中将assertTrue设置为false,如下所示:

try {
    driver.findElement(By.xpath("(//nav[@class='topline']//nav[@class='without-seperator1']/a)[1]");
}catch(Exception e) {
    assertTrue(false, "Test Search link was not displayed");
}