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");
上的nosuchelementexception时,krGlobalPage.softAssertionTestCall2(softAssertion)
没有被执行。请看截图。
即使在krGlobalPage.softAssertionTestCall2(softAssertion);
由于krGlobalPage.softAssertionTestCall1(softAssertion);
而失败并且在第一个屏幕截图中显示的红色指示器测试用例失败后,如何运行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);
}
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,如何检查元素是否存在。