【问题标题】:Adding screenshot to the TestNG Report将屏幕截图添加到 TestNG 报告
【发布时间】:2018-04-24 06:25:07
【问题描述】:

我正在尝试将失败的测试用例的屏幕截图添加到 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();
  }

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

【问题讨论】:

    标签: selenium testng


    【解决方案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>
    

    【讨论】:

      【解决方案2】:
      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;
                  }
      

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

      希望对你有帮助!!!

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

      【讨论】:

        【解决方案3】:

        我建议使用 ReportNG 而不是 TestNG premitive 报告。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-16
          • 2018-11-28
          • 1970-01-01
          • 2022-01-02
          • 2012-02-14
          • 1970-01-01
          相关资源
          最近更新 更多