Skip to content

Commit

Permalink
Use FileOutputStreamWriter to store screenshot files
Browse files Browse the repository at this point in the history
  • Loading branch information
martingrossmann committed Sep 19, 2024
1 parent c034bd1 commit 7fb3570
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
Expand Down Expand Up @@ -227,9 +227,12 @@ public static void takeWebDriverScreenshotToFile(WebDriver decoratedWebDriver, F
}

private static void makeSimpleScreenshot(WebDriver driver, File screenShotTargetFile) {
try {
File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.moveFile(file, screenShotTargetFile);
// Using FileOutputStream prevents from file permission issues on -ix systems:
// '((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE)' creates files with user-only permissions
// Moving the file with latest org.apache.commons.io.FileUtils does not change permissions any more
try (OutputStream os = new FileOutputStream(screenShotTargetFile)) {
byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
os.write(screenshot);
} catch (Exception e) {
LOGGER.error("Unable to take screenshot to file", e);
}
Expand All @@ -238,7 +241,7 @@ private static void makeSimpleScreenshot(WebDriver driver, File screenShotTarget
/**
* Save page source to file.
*
* @param pageSource page source.
* @param pageSource page source.
* @param sourceTargetFile target file.
*/
private static void savePageSource(final String pageSource, final File sourceTargetFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected TestPage getTestPage() {
@BeforeTest(alwaysRun = true)
public void setUp() throws Exception {
try {
staticServer.start(80);
staticServer.start(8080);
} catch (BindException e) {
log().warn("Use already running WebServer: " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public class DriverStandaloneTest extends AbstractWebDriverTest {
public void testFailing() throws Exception {
DesktopWebDriverRequest request = new DesktopWebDriverRequest();
request.setBaseUrl("http://google.de");
request.setBrowser(Browsers.chrome);
// request.setBrowser(Browsers.chrome);

WebDriver webDriver = WEB_DRIVER_MANAGER.getWebDriver(request);
UITestUtils.takeScreenshot(webDriver, false);
UITestUtils.takeScreenshot(webDriver, true);
// Assert.assertTrue(false);
File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class);
log().info("take screenshot src {}", file.toPath());
// File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
// PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class);
// log().info("take screenshot src {}", file.toPath());
// log().info("Posix {}", fileAttributeView.readAttributes().permissions().toString());
}

Expand Down

0 comments on commit 7fb3570

Please sign in to comment.