繁体   English   中英

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

[英]How to pass error message from method causing exception to onTestFailure method in Listener

我正在尝试从导致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;

ITestResult接口包含ITestResult方法,以访问在测试中getThrowable的异常。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM