Skip to content

Commit

Permalink
[bidi] [java] Add script pinning methods (#14305)
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani authored Jul 25, 2024
1 parent 139af72 commit 5dd385c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
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);
}
}

0 comments on commit 5dd385c

Please sign in to comment.