Skip to content

Commit 9b3eb79

Browse files
committed
Refactor PlaywrightIT to use environment variable for headless mode and add null checks for resource cleanup
1 parent 620119c commit 9b3eb79

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/test/java/org/javadominicano/PlaywrightIT.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,24 @@ public static void beforeAll() {
2222
playwright = Playwright.create();
2323
BrowserType browserType = playwright.chromium();
2424

25-
BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions().setHeadless(true);
26-
launchOptions.headless = false;
25+
boolean headless = Boolean.parseBoolean(
26+
System.getenv().getOrDefault("HEADLESS", "true")
27+
);
28+
29+
BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions()
30+
.setHeadless(headless);
2731

2832
browser = browserType.launch(launchOptions);
2933
}
3034

3135
@AfterAll
3236
public static void afterAll() {
33-
browser.close();
34-
playwright.close();
37+
if (browser != null) {
38+
browser.close();
39+
}
40+
if (playwright != null) {
41+
playwright.close();
42+
}
3543
}
3644

3745
@BeforeEach
@@ -50,8 +58,8 @@ void beforeEach() {
5058

5159
@AfterEach
5260
void afterEach() {
53-
page.close();
54-
browserContext.close();
61+
if (page != null) page.close();
62+
if (browserContext != null) browserContext.close();
5563
}
5664

5765
}

0 commit comments

Comments
 (0)