Skip to content

Commit

Permalink
Move profile directory deletion to correct place outside the lifetime…
Browse files Browse the repository at this point in the history
… of the webdriver, because that still writes stuff into the profile dir until it is quit.
  • Loading branch information
MediaMarco committed Sep 1, 2023
1 parent 785cfaa commit 0848f49
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
11 changes: 7 additions & 4 deletions core/src/main/java/de/otto/jlineup/browser/Browser.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ public void close() {
//grepChromedrivers();
}
LOG.debug("Closing webdrivers done.");

if (runStepConfig.isCleanupProfile()) {
LOG.info("Cleaning up profile directory.");
cleanupProfileDirectory();
LOG.info("Profile cleanup done.");
}
}

public void runSetupAndTakeScreenshots() throws Exception {
Expand All @@ -186,10 +192,6 @@ public void runSetupAndTakeScreenshots() throws Exception {
cloudBrowser.takeScreenshots(screenshotContextList);
}
} finally {
if (runStepConfig.isCleanupProfile()) {
LOG.info("Cleaning up profile directory.");
cleanupProfileDirectory();
}
if (!testCleanupContexts.isEmpty()) {
LOG.debug("Running test cleanup.");
runTestSetupOrCleanup(testCleanupContexts);
Expand All @@ -210,6 +212,7 @@ private void cleanupProfileDirectory() {
}
LOG.debug("Deleting chrome profile dir {}", path);
FileUtils.deleteDirectory(path);

} catch (Exception e) {
LOG.error("Exception while deleting user data dir: " + e.getMessage(), e);
}
Expand Down
25 changes: 13 additions & 12 deletions core/src/test/java/de/otto/jlineup/browser/BrowserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ public void shouldDeleteTheBrowserProfileDirectoryForChrome() throws Exception {
when(webDriverMock.getScreenshotAs(OutputType.FILE)).thenReturn(new File(getFilePath("screenshots/test_image_1125x750.png")));
when(webDriverMock.executeScript(JS_GET_DEVICE_PIXEL_RATIO_CALL)).thenReturn(1.5d);
Files.createDirectories(new File("/tmp/jlineup-test/jlineup/chrome/12345").toPath());
testee.close();
testee = new Browser(runStepConfig, jobConfig, fileService, browserUtilsMock);
testee.runSetupAndTakeScreenshots();

//when
testee.runSetupAndTakeScreenshots();

testee.close();

//then
assertThat(Files.exists(new File("/tmp/jlineup-test/jlineup/chrome/12345").toPath()), is(false));
Expand All @@ -188,19 +189,19 @@ public void shouldDeleteTheBrowserProfileDirectoryForFirefoxWithProfileParam() t
.withDevices(List.of(DeviceConfig.deviceConfigBuilder().build()))
.withPath("/")
.build()))
.withBrowser(CHROME).build().insertDefaults();
.withBrowser(FIREFOX).build().insertDefaults();
when(runStepConfig.isCleanupProfile()).thenReturn(true);
when(runStepConfig.getChromeParameters()).thenReturn(List.of("-profile /tmp/jlineup-test/jlineup/firefox/12345"));
when(runStepConfig.getFirefoxParameters()).thenReturn(List.of("-profile /tmp/jlineup-test/jlineup/firefox/12345"));
when(webDriverMock.executeScript(JS_DOCUMENT_HEIGHT_CALL)).thenReturn(1000L);
when(webDriverMock.executeScript(JS_CLIENT_VIEWPORT_HEIGHT_CALL)).thenReturn(1000L);
when(webDriverMock.getScreenshotAs(OutputType.FILE)).thenReturn(new File(getFilePath("screenshots/test_image_1125x750.png")));
when(webDriverMock.executeScript(JS_GET_DEVICE_PIXEL_RATIO_CALL)).thenReturn(1.5d);
Files.createDirectories(new File("/tmp/jlineup-test/jlineup/chrome/12345").toPath());
testee.close();
Files.createDirectories(new File("/tmp/jlineup-test/jlineup/firefox/12345").toPath());
testee = new Browser(runStepConfig, jobConfig, fileService, browserUtilsMock);
testee.runSetupAndTakeScreenshots();

//when
testee.runSetupAndTakeScreenshots();
testee.close();

//then
assertThat(Files.exists(new File("/tmp/jlineup-test/jlineup/firefox/12345").toPath()), is(false));
Expand All @@ -215,19 +216,19 @@ public void shouldDeleteTheBrowserProfileDirectoryForFirefoxWithPParam() throws
.withDevices(List.of(DeviceConfig.deviceConfigBuilder().build()))
.withPath("/")
.build()))
.withBrowser(CHROME).build().insertDefaults();
.withBrowser(FIREFOX).build().insertDefaults();
when(runStepConfig.isCleanupProfile()).thenReturn(true);
when(runStepConfig.getChromeParameters()).thenReturn(List.of("-P /tmp/jlineup-test/jlineup/firefox/12345"));
when(runStepConfig.getFirefoxParameters()).thenReturn(List.of("-P /tmp/jlineup-test/jlineup/firefox/12345"));
when(webDriverMock.executeScript(JS_DOCUMENT_HEIGHT_CALL)).thenReturn(1000L);
when(webDriverMock.executeScript(JS_CLIENT_VIEWPORT_HEIGHT_CALL)).thenReturn(1000L);
when(webDriverMock.getScreenshotAs(OutputType.FILE)).thenReturn(new File(getFilePath("screenshots/test_image_1125x750.png")));
when(webDriverMock.executeScript(JS_GET_DEVICE_PIXEL_RATIO_CALL)).thenReturn(1.5d);
Files.createDirectories(new File("/tmp/jlineup-test/jlineup/chrome/12345").toPath());
testee.close();
Files.createDirectories(new File("/tmp/jlineup-test/jlineup/firefox/12345").toPath());
testee = new Browser(runStepConfig, jobConfig, fileService, browserUtilsMock);
testee.runSetupAndTakeScreenshots();

//when
testee.runSetupAndTakeScreenshots();
testee.close();

//then
assertThat(Files.exists(new File("/tmp/jlineup-test/jlineup/firefox/12345").toPath()), is(false));
Expand Down

0 comments on commit 0848f49

Please sign in to comment.