MakeScreenshotAction extends AbstractSeleniumAction { @Override protected void execute(SeleniumBrowser browser, TestContext context) { File screenshot = null; if (browser.getWebDriver() instanceof TakesScreenshot) { screenshot = ((TakesScreenshot) browser.getWebDriver()).getScreenshotAs(OutputType.FILE); } else { log.warn("Skip screenshot action because web driver is missing screenshot features"); } if (screenshot != null) { String testName = "Test"; if (context.getVariables().containsKey(CitrusSettings.TEST_NAME_VARIABLE)) { testName = context.getVariable(CitrusSettings.TEST_NAME_VARIABLE); } context.setVariable(SeleniumHeaders.SELENIUM_SCREENSHOT, testName + "_" + screenshot.getName()); if (StringUtils.hasText(outputDir)) { try { File targetDir = new File(context.replaceDynamicContentInString(outputDir)); if (!targetDir.exists()) { if (!targetDir.mkdirs()) { throw new CitrusRuntimeException("Failed to create screenshot output directory: " + outputDir); } } FileCopyUtils.copy(screenshot, new File(targetDir, testName + "_" + screenshot.getName())); } catch (IOException e) { log.error("Failed to save screenshot to target storage", e); } } else { browser.storeFile(new FileSystemResource(screenshot)); } } } MakeScreenshotAction(Builder builder); String getOutputDir();  }
@Test public void testExecute() throws Exception { when(webDriver.getScreenshotAs(OutputType.FILE)).thenReturn(new ClassPathResource("screenshot.png").getFile()); MakeScreenshotAction action = new MakeScreenshotAction.Builder() .browser(seleniumBrowser) .build(); action.execute(context); Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_SCREENSHOT), "Test_screenshot.png"); Assert.assertNotNull(seleniumBrowser.getStoredFile("screenshot.png")); }
@Test public void testExecuteOutputDir() throws Exception { when(webDriver.getScreenshotAs(OutputType.FILE)).thenReturn(new ClassPathResource("screenshot.png").getFile()); context.setVariable(CitrusSettings.TEST_NAME_VARIABLE, "MyTest"); MakeScreenshotAction action = new MakeScreenshotAction.Builder() .browser(seleniumBrowser) .outputDir("target") .build(); action.execute(context); File stored = new File("target/MyTest_screenshot.png"); Assert.assertTrue(stored.exists()); }