gpt4 book ai didi

java - 如何将导致异常的方法的错误消息传递给监听器中的 onTestFailure 方法

转载 作者:行者123 更新时间:2023-11-30 01:52:21 25 4
gpt4 key购买 nike

我正在尝试从导致 testng 监听器异常的方法打印异常消息,以便我可以在范围报告中打印它。我写过这样的代码

@Override
public void onTestFailure(ITestResult result) {
String screenshotPath = "";
try {
screenshotPath = ScreenshotUtil.capture();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
test2.log(Status.FAIL, MarkupHelper.createLabel("Error occured", ExtentColor.RED));

test2.addScreenCaptureFromPath(screenshotPath, "Error");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

我在下面的方法中抛出异常

public HomePage assertLoginSuccessful(boolean flag, String name) throws ExplicitAssertionError, IOException{
waitForPageToLoad();
WebDriverWait wait=new WebDriverWait(getDriver(), 5);
String xpath="//a[@id='welcome'][contains(.,'Welcome "+name+"')]";
try{
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(xpath)));
if (flag){
List<WebElement> list=getDriver().findElements(By.xpath(xpath));

if (list.size()==0){
throw new ExplicitAssertionError("User name"+name+" not found hence login not successful");//this message should be passed to listener
}
}
}catch (Exception ex){
throw new ExplicitAssertionError("User name"+name+" not found hence login not successful");
}
return this;

最佳答案

接口(interface)ITestResult包含一个方法getThrowable来访问您在测试中抛出的异常。

您可以将以下代码添加到onTestFailure:

    if (null != result.getThrowable()) {
String msg = result.getThrowable().getMessage();
}

关于java - 如何将导致异常的方法的错误消息传递给监听器中的 onTestFailure 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55649648/

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