Как прикрепить скриншот к отчету об объеме в java селене - PullRequest
4 голосов
/ 07 апреля 2020

Я пытаюсь прикрепить скриншот для неудачных тестовых случаев из моего пути к отчету об объемах, но я не могу в него вложить.

Я попробовал мое возможное решение, но оно не удалось. Я использовал отчет об экстентах версии 3

. Вот мой полный код в отдельном классе extenreport:

    package com.qa.ExtentReportListener;


    import java.io.IOException;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    import java.util.Map;

    import org.testng.IReporter;
    import org.testng.IResultMap;
    import org.testng.ISuite;
    import org.testng.ISuiteResult;
    import org.testng.ITestContext;
    import org.testng.ITestResult;
    import org.testng.Reporter;
    import org.testng.xml.XmlSuite;

    import com.aventstack.extentreports.ExtentReports;
    import com.aventstack.extentreports.ExtentTest;
    import com.aventstack.extentreports.Status;
    import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
    import com.aventstack.extentreports.reporter.configuration.ChartLocation;
    import com.aventstack.extentreports.reporter.configuration.Theme;
    import com.crm.qa.util.TestUtil;

    public class ExtentTestNGIReporterListener implements IReporter {

        private static final String OUTPUT_FOLDER = "test-output/";
        private static final String FILE_NAME = "Extent.html";

        private ExtentReports extent;
        private ExtentTest test;

        public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
            init();

            for (ISuite suite : suites) {
                Map<String, ISuiteResult> result = suite.getResults();

                for (ISuiteResult r : result.values()) {
                    ITestContext context = r.getTestContext();

                    buildTestNodes(context.getFailedTests(), Status.FAIL);
                    buildTestNodes(context.getSkippedTests(), Status.SKIP);
                    buildTestNodes(context.getPassedTests(), Status.PASS);

                }
            }

            for (String s : Reporter.getOutput()) {
                extent.setTestRunnerOutput(s);
            }

            extent.flush();
        }

        private void init() {
            ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
            htmlReporter.config().setDocumentTitle("ExtentReports - Created by TestNG Listener");
            htmlReporter.config().setReportName("ExtentReports - Created by TestNG Listener");
            htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
            htmlReporter.config().setTheme(Theme.STANDARD);

            extent = new ExtentReports();
            extent.attachReporter(htmlReporter);
            extent.setReportUsesManualConfiguration(true);
        }

        private void buildTestNodes(IResultMap tests, Status status) {


            if (tests.size() > 0) {
                for (ITestResult result : tests.getAllResults()) {
                    test = extent.createTest(result.getMethod().getMethodName());

                    for (String group : result.getMethod().getGroups())
                        test.assignCategory(group);

                    if (result.getThrowable() != null) {
                        test.log(status, result.getThrowable());
                    }
                    else {
                        test.log(status, "Test " + status.toString().toLowerCase() + "ed");
                    }

                    test.getModel().setStartTime(getTime(result.getStartMillis()));
                    test.getModel().setEndTime(getTime(result.getEndMillis()));
                }
            }
        }

        public void down(ITestResult result) throws IOException{


            if(result.getStatus()==ITestResult.FAILURE){
                test.log(Status.FAIL, "TEST CASE FAILED IS "+result.getName()); //to add name in extent report
                test.log(Status.FAIL, "TEST CASE FAILED IS "+result.getThrowable()); //to add error/exception in extent report

                String screenshotPath = TestUtil.takeScreenshotAtEndOfTest();
                test.fail("Test Case failed check screenshot below"+test.addScreenCaptureFromPath(screenshotPath));
                //extentTest.log(Status.FAIL, MediaEntityBuilder.createScreenCaptureFromPath(screenshotPath).build()); //to add screenshot in extent report
                //extentTest.fail("details").addScreenCaptureFromPath(screenshotPath);
            }
            else if(result.getStatus()==ITestResult.SKIP){
                test.log(Status.SKIP, "Test Case SKIPPED IS " + result.getName());
            }
            else if(result.getStatus()==ITestResult.SUCCESS){
                test.log(Status.PASS, "Test Case PASSED IS " + result.getName());

            }
        extent.flush();
        }

        private Date getTime(long millis) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(millis);
            return calendar.getTime();      
        }
    }

Вот мой класс утилит, содержащий метод снимка экрана:

public static String takeScreenshotAtEndOfTest() throws IOException {
    String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
    TakesScreenshot ts = (TakesScreenshot)driver;
    File source = ts.getScreenshotAs(OutputType.FILE);
    String destination = System.getProperty("user.dir") + "/screenshots/" +  dateName
            + ".png";
    File finalDestination = new File(destination);
    FileHandler.copy(source, finalDestination);
    return destination;
}

1 Ответ

0 голосов
/ 07 апреля 2020

Используйте следующий пример в Java

   WebDriver driver = new FirefoxDriver();

   driver.get("http://www.google.com/");

        // Store the screenshot in current project dir.
        String screenShot = System.getProperty("user.dir")+"\\Artifacts\\FileName.png";

        // Call Webdriver to click the screenshot.
        File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        // Save the screenshot.
        FileUtils.copyFile(scrFile, new File(screenShot));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...