gpt4 book ai didi

testng - 在 TestNG 中处理断言

转载 作者:行者123 更新时间:2023-12-02 17:45:28 26 4
gpt4 key购买 nike

如果断言失败,如何在 TestNG 中继续执行测试?如何在 TestNG 的 HTML 报告中报告失败?

当我运行以下代码时,执行了断言之后的行,但在报告中未列出断言失败:

@Test
public void googleSearch(){
driver.get("http://www.google.co.in/");
System.out.println(" ---------- Start -------------");

try {
Assert.assertTrue(driver.findElement(By.xpath("xyz")).isDisplayed(), "unable to find the link");
} catch (Throwable t) {
System.out.println("Error");
}
System.out.println(" ---------- End -------------");
}

最佳答案

您可以在 catch block 中使用此代码:-

org.testng.Assert.fail("expected and actual result do not match");

让我们以下面的代码为例:-

String expectedtitle="xyz";
String actualtitle="xywz";
try {
Assert.assertEquals(expectedtitle, actualtitle);
} catch(Throwable t) {
org.testng.Assert.fail("expected and actual result do not match");
}

关于testng - 在 TestNG 中处理断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15331578/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com