Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script pinning methods #14305

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions java/src/org/openqa/selenium/remote/RemoteScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,14 @@ public long addDomMutationHandler(Consumer<DomMutation> consumer) {
public void removeDomMutationHandler(long id) {
this.biDi.removeListener(id);
}

@Override
public String pin(String script) {
return this.script.addPreloadScript(script);
}

@Override
public void unpin(String id) {
this.script.removePreloadScript(id);
}
}
4 changes: 4 additions & 0 deletions java/src/org/openqa/selenium/remote/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public interface Script {
long addDomMutationHandler(Consumer<DomMutation> event);

void removeDomMutationHandler(long id);

String pin(String script);

void unpin(String id);
}
43 changes: 43 additions & 0 deletions java/test/org/openqa/selenium/WebScriptTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,47 @@ void canRemoveDomMutationHandler() throws InterruptedException {

Assertions.assertThat(latch.await(10, SECONDS)).isFalse();
}

@Test
void canPinScript() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<ConsoleLogEntry> future = new CompletableFuture<>();

((RemoteWebDriver) driver).script().pin("() => { console.log('Hello!'); }");

long id = ((RemoteWebDriver) driver).script().addConsoleMessageHandler(future::complete);

page = server.whereIs("/bidi/logEntryAdded.html");
driver.get(page);

ConsoleLogEntry logEntry = future.get(5, TimeUnit.SECONDS);

assertThat(logEntry.getText()).isEqualTo("Hello!");

((RemoteWebDriver) driver).script().removeConsoleMessageHandler(id);
}

@Test
void canUnpinScript() throws ExecutionException, InterruptedException, TimeoutException {
CountDownLatch latch = new CountDownLatch(2);

String pinnedScript =
((RemoteWebDriver) driver).script().pin("() => { console.log('Hello!'); }");

long id =
((RemoteWebDriver) driver)
.script()
.addConsoleMessageHandler(consoleLogEntry -> latch.countDown());

page = server.whereIs("/bidi/logEntryAdded.html");

driver.get(page);

((RemoteWebDriver) driver).script().unpin(pinnedScript);

driver.get(page);

assertThat(latch.getCount()).isEqualTo(1L);

((RemoteWebDriver) driver).script().removeConsoleMessageHandler(id);
}
}
Loading