krGlobalPage.softAssertionTestCall2(softAssertion);
即使由于krGlobalPage.softAssertionTestCall1(softAssertion);
而失败,也必须运行nosuchelementexception
@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);
}
但是当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);
}
TestNG软断言以及代码中的任何错误/异常,包括所有Selenium的错误(NoSuchElementException
,ElementNotVisibleException
,...)彼此无关。
如果您不想因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,如何检查元素是否存在。