Selenium 向TestNG报告添加屏幕截图

Selenium 向TestNG报告添加屏幕截图,selenium,testng,Selenium,Testng,我正在尝试将失败测试用例的截图添加到testNG报告中,对此我没有任何想法 这是我用来截图的pom课程 package library; import java.awt.AWTException; import java.io.File; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; impor

我正在尝试将失败测试用例的截图添加到testNG报告中,对此我没有任何想法

这是我用来截图的pom课程

package library;

import java.awt.AWTException;
import java.io.File;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public class TakeScreenShot {

    WebDriver driver;

    public TakeScreenShot(WebDriver driver) {

        this.driver=driver;

    }

     public void CaptureScreenshot(String screenshotName) throws AWTException
        {


                    try {
                                TakesScreenshot ts=(TakesScreenshot)driver;
                                File source=ts.getScreenshotAs(OutputType.FILE);
                                FileUtils.copyFile(source, new File("C:\\Users\\Documents\\Eclipse\\WorkSpace\\ScreenShot\\"+screenshotName));

                                System.out.println("Screenshot taken");


                    } catch (Exception e) {

                                System.out.println("Exception "+e.getMessage());

                    }            
        }
}
这是我的TestNG类

@AfterMethod
  public void CloseBrowser(ITestResult result) throws AWTException {

     String name=result.getName()+"."+result.getMethod().getCurrentInvocationCount()+".png";

     if(ITestResult.FAILURE==result.getStatus())
         {
            ScreenshotPageObjectModel screenshotPom= new ScreenshotPageObjectModel(driver);

             screenshotPom.CaptureScreenshot(name);

         }

        driver.close();
  }
感谢您的帮助,请说明在报告中添加屏幕截图时我应该在哪里进行更改

To get screen shot use this method:


 public static void takeSnapShot(WebDriver driver, String fileWithPath) throws Exception {
            TakesScreenshot scrShot = (TakesScreenshot)driver;
            File SrcFile = (File)scrShot.getScreenshotAs(OutputType.FILE);
            File DestFile = new File(fileWithPath);
            FileUtils.copyFile(SrcFile, DestFile);
        }

    You can call the screen shot under TestNG using below code:
     @AfterMethod(
                alwaysRun = true
        )
        protected void afterMethod(ITestResult result, Method method) throws Exception {
            // boolean testStatus = false;
            String fileName = "FAIL  - Error Message Generated on Details Reports";
            byte testStatus;
            if (result.getStatus() == 2) {
                fileName = System.getProperty("user.dir") + "\\Reports\\failure_screenshots\\" + ".png";
                takeSnapShot(this.driver, fileName);
                ExtentTestManager.getTest().log(LogStatus.FAIL, "Error Screenshot" + ExtentTestManager.getTest().addScreenCapture("failure_screenshots\\" + ".png"));
                ExtentTestManager.getTest().log(LogStatus.FAIL, "Test Failed");
                testStatus = 2;
            } else {
                ExtentTestManager.getTest().log(LogStatus.PASS, "Test passed");
                testStatus = 1;
            }
参考->

希望这有帮助


PS-使用最稳定的数据块报告版本-2.41.1

您需要使用TestNgITestListeners接口,它基本上提供了7种方法,它们是:

onTestStart(ITestResult result)  
onTestFailure(ITestResult result)    
onTestSuccess(ITestResult result)  
onTestSkipped(ITestResult result)  
onTestFailedButWithinSuccessPercentage(ITestResult result)  
onStart(ITestContext context)  
onFinish(ITestContext context)  
您在这里的要求是“拍摄失败测试用例/方法的屏幕截图”
。您应该使用onTestFailure(ITestResult result)方法

演示:

public class TestListener implements ITestListener {
        WebDriver driver=null;  
        String filePath = "D:\\SCREENSHOTS";  

        @Override
        public void onTestFailure(ITestResult result) {
            System.out.println("***** Error "+result.getName()+" test has failed *****");
            String methodName=result.getName().toString().trim();
            takeScreenShot(methodName);
        }

        public void takeScreenShot(String methodName) {
            //get the driver
            driver=TestBase.getDriver();
             File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
             //The below method will save the screen shot in d drive with test method name 
                try {
                    FileUtils.copyFile(scrFile, new File(filePath+methodName+".png"));
                    System.out.println("***Placed screen shot in "+filePath+" ***");
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        public void onFinish(ITestContext context) {}

        public void onTestStart(ITestResult result) {   }

        public void onTestSuccess(ITestResult result) {   }

        public void onTestSkipped(ITestResult result) {   }

        public void onTestFailedButWithinSuccessPercentage(ITestResult result) {   }

        public void onStart(ITestContext context) {   }
    }  
您需要将此标记添加到XML文件中:

<listeners>
       <listener class-name="com.pack.listeners.TestListener"/>
</listeners>

我建议使用ReportNG而不是TestNG前置报告