测试失败后,不会拍摄诱惑报告截图

问题描述 投票:0回答:1

我制作了一个TestListener并实现了一种方法,该方法应该在测试失败时进行屏幕截图。但是,如果测试失败,则不会截屏,但是我不知道为什么。如果调用方法takeScreenshot(),则屏幕截图不会保存在诱人的结果中。下面我发布TestListener和TestBase的代码:

public class TestListener extends TestBase implements ITestListener {


@Override
public void onTestFailure(ITestResult result) {
    Object testClass = result.getInstance();
    driver = ((TestBase) testClass).getTestDriver();
    if(driver != null) {
        takeScreenshot();
    }
}

@Override
public void onTestSkipped(ITestResult result) {

}

@Attachment(value = "Page screenshot", type = "image/png")
public byte[] takeScreenshot() {
    setThisDriver(driver);
    return ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
}




public class TestBase {

public WebDriver driver;

public static Properties prop;

public static String env;

public static String url;

public TestBase() {
    try {
        this.prop = new Properties();
        FileInputStream ip = new FileInputStream(
                "src/main/java/config/env.properties");
        this.prop.load(ip);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void initialization() {
    System.setProperty("webdriver.gecko.driver", "src/main/resources/geckodriver");
    System.setProperty("http.agent", "Mozilla/5.0");
    FirefoxBinary firefoxBinary = new FirefoxBinary();
    File downloadsDir = new File("src/main/documents/");
    //firefoxBinary.addCommandLineOptions("--headless");
    FirefoxOptions options = new FirefoxOptions();
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.download.folderList", 2);
    profile.setPreference("browser.download.dir", downloadsDir.getAbsolutePath());
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
            "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("browser.download.manager.focusWhenStarting", false);
    profile.setPreference("browser.download.useDownloadDir", true);
    profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
    profile.setPreference("browser.download.manager.closeWhenDone", true);
    profile.setPreference("browser.download.manager.showAlertOnComplete", false);
    profile.setPreference("browser.download.manager.useWindow", false);
    profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
    profile.setPreference("pdfjs.disabled", true);
    //options.setHeadless(true);
    options.setBinary(firefoxBinary);
    //options.setProfile(profile);
    this.driver = new FirefoxDriver(options);
    try {
        this.driver.manage().window().maximize();
        this.driver.manage().deleteAllCookies();
    }
    catch (WebDriverException e) {
        System.out.println(e.toString());
        this.driver.quit();
    }

}

public WebDriver getTestDriver() {
    return this.driver;
}

public void setThisDriver(WebDriver webDriver) {
    this.driver = webDriver;
}


public String getCurrentDate() {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(
            Calendar.getInstance().getTime());
    return timeStamp;
}

public void captureScreenshots(String result) {
    try {
        TakesScreenshot ts = (TakesScreenshot) driver;
        File source = ts.getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(source, new File("src/screenshots/" + getCurrentDate() + ".png"));
        System.out.println("Takes screenshot");

    } catch (Exception e) {
        System.out.println("Exception" + e.getMessage());
    }
}
java selenium automated-tests allure
1个回答
0
投票

[好,如果有人遇到相同的问题,这是一个答案。配置还可以,但是如果我们要生成带有屏幕截图的报告,则应该使用./gradlew clean test和./gradlew allureReport从命令行运行测试以生成报告。

© www.soinside.com 2019 - 2024. All rights reserved.