【问题标题】:Allure report screenshots is not taken when test got failed测试失败时不拍摄倾城报告截图
【发布时间】:2020-01-26 20:36:08
【问题描述】:

我已经制作了一个 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());
    }
}

【问题讨论】:

  • 不确定为什么这里存在“setThisDriver”功能...我会删除它...但不太可能是原因...还有为什么“takeScreenshot”功能?您是否可能会将其与 Selenium 自己的“TakesScreenshot”混淆?为什么不这样做: File source = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); (还有什么是“结果”变量?)
  • 我使用这个方法是因为 allure 库需要它:swtestacademy.com/allure-testng
  • 不熟悉 allure 或 testNG,但似乎您需要在某处断言才能触发 onTestFailure。
  • 我在 testng.xml 中添加了一个测试监听器

标签: java selenium automated-tests allure


【解决方案1】:

好的,如果有人有同样的问题,这里是一个答案。配置没问题,但是如果我们想生成带有屏幕截图的报告,我们应该使用以下命令从命令行运行测试:./gradlew clean test 和 ./gradlew allureReport 以生成报告。

【讨论】:

    猜你喜欢
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 2020-08-04
    • 2015-07-19
    • 1970-01-01
    相关资源
    最近更新 更多