Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将导致异常的方法的错误消息传递给侦听器中的onTestFailure方法_Java_Selenium_Testng_Extentreports - Fatal编程技术网

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

Java 如何将导致异常的方法的错误消息传递给侦听器中的onTestFailure方法,java,selenium,testng,extentreports,Java,Selenium,Testng,Extentreports,我正试图从方法中打印异常消息,该方法导致testng侦听器出现异常,以便我可以在数据块报告中打印它。我写过这样的代码 @Override public void onTestFailure(ITestResult result) { String screenshotPath = ""; try { screenshotPath = ScreenshotUtil.capture(); } catch (IOException e) { //

我正试图从方法中打印异常消息,该方法导致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;
public HomePage assertloginsucessful(布尔标志,字符串名称)抛出ExplicitAssertionError,IOException{
waitForPageToLoad();
WebDriverWait wait=newwebdriverwait(getDriver(),5);
字符串xpath=“//a[@id='welcome'][包含(,'welcome'+name+”)];
试一试{
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(xpath));
国际单项体育联合会(旗){
List=getDriver().findElements(By.xpath(xpath));
if(list.size()==0){
throw new ExplicitAssertionError(“未找到用户名”+name+“因此登录不成功”);//应将此消息传递给侦听器
}
}
}捕获(例外情况除外){
抛出新的EXPLICITASERTIONERROR(“未找到用户名”+name+“因此登录不成功”);
}
归还这个;

接口
ITestResult
包含一个方法
getThrowable
,用于访问测试中抛出的异常

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

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

您面临的问题是什么?我想将错误消息从java方法传递给testng listener,以便将错误消息打印在报告中。listener方法onTestFailure()中是否有一个参数可以从失败的方法获取失败消息。目前,我只能够附加带有硬编码错误消息E.getMessage()的屏幕截图可以给你异常消息,我想将消息从ExplicitAssertionError构造函数传递到listener类的onTestFailure方法这正是我想要的。我知道我必须使用throwable类,但由于不知道方法,所以无法这样做。谢谢你让我知道。