Skip to content

Commit

Permalink
2.4.3
Browse files Browse the repository at this point in the history
load() now waits until HTML is fully loaded and close() in PlaywrightWindow also executes browserCtx.close() to prevent zombie processes
  • Loading branch information
Osiris-Team committed May 1, 2023
1 parent 27f1bb9 commit 9615a32
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.osiris.headlessbrowser</groupId>
<artifactId>Headless-Browser</artifactId>
<version>2.4.1</version>
<version>2.4.3</version>
<repositories>
<repository>
<id>jitpack</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,54 @@ public PlaywrightWindow(HBrowser parentBrowser, boolean enableJavaScript, Output
}

/**
* Loads the contents from the provided url into the current page/tab.
* Loads the contents from the provided url into the current page/tab. <br>
* Method only returns when 'documentloaded' event was triggered.
*/
public PlaywrightWindow load(String url) throws NodeJsCodeException {
if (!url.startsWith("http") && !url.equals("about:blank"))
url = "https://" + url;

jsContext.executeJavaScript("" +
"response = await page.goto('" + url + "');\n");
"response = await page.goto('" + url + "', wait_until=\"domcontentloaded\");\n");
this.url = url;
return this;
}

/**
* Loads the contents from the provided file into the current page/tab.
* Loads the contents from the provided file into the current page/tab. <br>
* Method only returns when 'documentloaded' event was triggered.
*/
public PlaywrightWindow load(File file) throws NodeJsCodeException {
String url = "file:///" + file.getAbsolutePath().replace("\\", "/");
jsContext.executeJavaScript("" +
"response = await page.goto('" + url + "', wait_until=\"domcontentloaded\");\n");
this.url = url;
return this;
}

/**
* Loads the contents from the provided url into the current page/tab. <br>
* Directly returns and does NOT wait for 'documentloaded' event to be triggered. <br>
* If you try to access HTML elements or run JavaScript directly after this method it probably won't work,
* so only use this method if you know what you are doing.
*/
public PlaywrightWindow unsafeLoad(String url) throws NodeJsCodeException {
if (!url.startsWith("http") && !url.equals("about:blank"))
url = "https://" + url;

jsContext.executeJavaScript("" +
"response = await page.goto('" + url + "');\n");
this.url = url;
return this;
}

/**
* Loads the contents from the provided file into the current page/tab. <br>
* Directly returns and does NOT wait for 'documentloaded' event to be triggered. <br>
* If you try to access HTML elements or run JavaScript directly after this method it probably won't work,
* so only use this method if you know what you are doing.
*/
public PlaywrightWindow unsafeLoad(File file) throws NodeJsCodeException {
String url = "file:///" + file.getAbsolutePath().replace("\\", "/");
jsContext.executeJavaScript("" +
"response = await page.goto('" + url + "');\n");
Expand Down Expand Up @@ -752,7 +784,10 @@ public boolean isTemporaryUserDataDir() {
public void close() throws RuntimeException {
// Running js: browser.close() here causes a weird exception: https://github.com/isaacs/rimraf/issues/221
// Since it's not mandatory we just don't do it.
out.println("---------------");
out.println("Closing "+this);
try {
jsContext.executeJavaScript("await browserCtx.close();");
jsContext.close();
if (temporaryUserDataDir) {
out.println("Deleting: " + userDataDir);
Expand All @@ -761,6 +796,8 @@ public void close() throws RuntimeException {
} catch (Exception e) {
throw new RuntimeException(e);
}
out.println("Successfully closed "+this);
out.println("---------------");
}

private void forceDeleteDirectory(File file) throws IOException {
Expand Down

0 comments on commit 9615a32

Please sign in to comment.