将屏幕截图添加到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;
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();
  }

感谢您的帮助,请提及在报告中添加屏幕截图的我应该在哪里进行更改

您需要使用 iTestListeners testng 的接口,它基本上提供 7方法,这些是:

onTestStart(ITestResult result)  
onTestFailure(ITestResult result)    
onTestSuccess(ITestResult result)  
onTestSkipped(ITestResult result)  
onTestFailedButWithinSuccessPercentage(ITestResult result)  
onStart(ITestContext context)  
onFinish(ITestContext context)  

您的要求是"对失败的测试用例/方法进行屏幕截图"

演示:

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>
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;
            }

ref-> https://www.softwaretestingmaterial.com/screenshots-extent-reports/http://automationtesting.in/capture-screenshot-in-extent-reports-java/

希望这会有所帮助!!!

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

我建议使用reportng而不是Testng预先报告。

相关内容