测试失败时无法从 testnglistner ontestfailure 方法截取屏幕截图

请在下方找到 的完整代码testNGlistner。好心检查。包pom;


import java.io.File;

import java.io.IOException;


import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.testng.ITestContext;

import org.testng.ITestListener;

import org.testng.ITestResult;

import com.aventstack.extentreports.ExtentReports;

import com.aventstack.extentreports.ExtentTest;

import com.aventstack.extentreports.MediaEntityBuilder;

import com.aventstack.extentreports.Status;

import com.aventstack.extentreports.markuputils.ExtentColor;

import com.aventstack.extentreports.markuputils.MarkupHelper;

import com.aventstack.extentreports.reporter.ExtentHtmlReporter;

import com.aventstack.extentreports.reporter.configuration.ChartLocation;

import com.aventstack.extentreports.reporter.configuration.Theme;


import generic.BaseTest;


public class testNgListners extends BaseTest implements ITestListener {

    ExtentHtmlReporter htmlReporter;

    ExtentReports extent;

    ExtentTest logger;


    @Override

    public void onTestStart(ITestResult result) {



    }


    @Override

    public void onTestSuccess(ITestResult result) {

        logger = extent.createTest(result.getName());

        logger.log(Status.PASS, MarkupHelper.createLabel(result.getName(), ExtentColor.GREEN));


    }


    @Override

    public void onTestFailure(ITestResult result) {

        System.out.println("hii");

        logger = extent.createTest(result.getName());

        logger.log(Status.FAIL, MarkupHelper.createLabel(result.getName(), ExtentColor.RED));

        if (result.getStatus() == ITestResult.FAILURE) {

            TakesScreenshot take = (TakesScreenshot) driver;

            File srcFile = take.getScreenshotAs(OutputType.FILE);


            }



        }

    }

  

慕沐林林
浏览 103回答 1
1回答

万千封印

您将 TestNG 侦听器和 Testclass 相关注释混合在一起,这就是导致NullPointerException我猜你有一个也在扩展BaseTest.&nbsp;为了示例,我们将该类称为RegressionTest.TestNG 现在创建两个实例:RegressionTest1个实例testNgListners1 个班级实例。但要注意的是,testNgListners创建实例时,根本不会调用@BeforeMethod和@AfterMethod注释,因为这些注释在侦听器的上下文中没有任何相关性。要解决此问题,您需要执行以下操作:重构testNgListners使其不扩展BaseTest现在BaseTest从中移动浏览器实例化和清理逻辑。您应该通过 TestNG 侦听器管理浏览器实例化,因为只要出现故障,您的 testng 也将使用驱动程序对象来截取屏幕截图。下面是修改后的监听器的样子:import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Properties;import java.util.concurrent.TimeUnit;import org.apache.commons.io.FileUtils;import org.openqa.selenium.OutputType;import org.openqa.selenium.PageLoadStrategy;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.chrome.ChromeOptions;import org.openqa.selenium.remote.RemoteWebDriver;import org.testng.ITestListener;import org.testng.ITestResult;import org.testng.Reporter;public class BrowserManagementListener implements ITestListener {&nbsp; private static final String BROWSER = "browser";&nbsp; private File file =&nbsp; &nbsp; &nbsp; new File("\\\\192.168.70.39\\IT Share\\Automation\\ERP Automation\\credential.properties");&nbsp; private Properties prop = new Properties();&nbsp; public static RemoteWebDriver getDriver() {&nbsp; &nbsp; ITestResult result = Reporter.getCurrentTestResult();&nbsp; &nbsp; if (result == null) {&nbsp; &nbsp; &nbsp; throw new IllegalStateException("could not detect a valid test result");&nbsp; &nbsp; }&nbsp; &nbsp; Object object = result.getAttribute(BROWSER);&nbsp; &nbsp; if (object == null) {&nbsp; &nbsp; &nbsp; throw new IllegalStateException("could not find a browser");&nbsp; &nbsp; }&nbsp; &nbsp; return (RemoteWebDriver)object;&nbsp; }&nbsp; @Override&nbsp; public void onTestStart(ITestResult result) {&nbsp; &nbsp; // This line retrieves the value of&nbsp; &nbsp; // <parameter name="browser" value="chrome"/> from your testng suite xml&nbsp; &nbsp; String browser = result.getTestContext().getCurrentXmlTest().getParameter("browser");&nbsp; &nbsp; if ("chrome".equalsIgnoreCase(browser)) {&nbsp; &nbsp; &nbsp; FileInputStream fileInput = null;&nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; fileInput = new FileInputStream(file);&nbsp; &nbsp; &nbsp; } catch (FileNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; prop.load(fileInput);&nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ChromeOptions options = new ChromeOptions();&nbsp; &nbsp; options.setPageLoadStrategy(PageLoadStrategy.NONE);&nbsp; &nbsp; RemoteWebDriver driver = new ChromeDriver();&nbsp; &nbsp; driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);&nbsp; &nbsp; driver.manage().window().maximize();&nbsp; &nbsp; result.setAttribute(BROWSER, driver);&nbsp; }&nbsp; @Override&nbsp; public void onTestSuccess(ITestResult result) {&nbsp; &nbsp; cleanUpBrowser(result);&nbsp; }&nbsp; @Override&nbsp; public void onTestFailure(ITestResult result) {&nbsp; &nbsp; Object object = result.getAttribute(BROWSER);&nbsp; &nbsp; if (object == null) {&nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; RemoteWebDriver driver = (RemoteWebDriver) object;&nbsp; &nbsp; File srcFile = driver.getScreenshotAs(OutputType.FILE);&nbsp; &nbsp; File destFile = new File("test-output/" + result.getName() + ".png");&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; FileUtils.copyFile(srcFile, destFile);&nbsp; &nbsp; &nbsp; System.out.println("Screenshot is been taken for failed test case: " + result.getName());&nbsp; &nbsp; &nbsp; System.err.println("Screenshot below" + destFile.getAbsolutePath());&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; cleanUpBrowser(result);&nbsp; &nbsp; }&nbsp; }&nbsp; private void cleanUpBrowser(ITestResult result) {&nbsp; &nbsp; Object driver = result.getAttribute(BROWSER);&nbsp; &nbsp; if (driver != null) {&nbsp; &nbsp; &nbsp; ((RemoteWebDriver) driver).quit();&nbsp; &nbsp; &nbsp; result.setAttribute(BROWSER, null);&nbsp; &nbsp; }&nbsp; }}这是测试类的样子import org.testng.annotations.Listeners;import org.testng.annotations.Test;@Listeners(BrowserManagementListener.class)public class SampleTestClass {&nbsp; @Test&nbsp; public void testMethod() {&nbsp; &nbsp; BrowserManagementListener.getDriver().get("http://www.google.com");&nbsp; &nbsp; throw new RuntimeException("Simulating an error");&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java