Skip to content

Commit

Permalink
[bidi][java] Add code examples for Script and Network module events
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Feb 16, 2024
1 parent 1e4861a commit e046b56
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package dev.selenium.bidirectional.webdriver_bidi;

import dev.selenium.BaseTest;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.bidi.Network;
import org.openqa.selenium.bidi.network.BeforeRequestSent;
import org.openqa.selenium.bidi.network.ResponseDetails;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

class NetworkEventsTest extends BaseTest {

@BeforeEach
public void setup() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
driver = new FirefoxDriver(options);
}

@Test
void canListenToBeforeRequestSentEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();
network.onBeforeRequestSent(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");

BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, requestSent.getBrowsingContextId());
Assertions.assertEquals("get", requestSent.getRequest().getMethod().toLowerCase());
}
}

@Test
void canListenToResponseStartedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onResponseStarted(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");

ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();

Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(200L, response.getResponseData().getStatus());
}
}

@Test
void canListenToResponseCompletedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onResponseCompleted(future::complete);
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");

ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();

Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(200L, response.getResponseData().getStatus());
}
}

@Test
void canListenToResponseCompletedEventWithCookie()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>();

driver.get("https://www.selenium.dev/selenium/web/blankPage");
driver.manage().addCookie(new Cookie("foo", "bar"));
network.onBeforeRequestSent(future::complete);
driver.navigate().refresh();

BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();

Assertions.assertEquals(windowHandle, requestSent.getBrowsingContextId());
Assertions.assertEquals("get", requestSent.getRequest().getMethod().toLowerCase());

Assertions.assertEquals("foo", requestSent.getRequest().getCookies().get(0).getName());
Assertions.assertEquals("bar", requestSent.getRequest().getCookies().get(0).getValue().getValue());
}
}

@Test
void canListenToOnAuthRequiredEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Network network = new Network(driver)) {
CompletableFuture<ResponseDetails> future = new CompletableFuture<>();
network.onAuthRequired(future::complete);
driver.get("https://the-internet.herokuapp.com/basic_auth");

ResponseDetails response = future.get(5, TimeUnit.SECONDS);
String windowHandle = driver.getWindowHandle();
Assertions.assertEquals(windowHandle, response.getBrowsingContextId());
Assertions.assertEquals("get", response.getRequest().getMethod().toLowerCase());
Assertions.assertEquals(401L, response.getResponseData().getStatus());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package dev.selenium.bidirectional.webdriver_bidi;

import dev.selenium.BaseTest;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.bidi.Script;
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
import org.openqa.selenium.bidi.script.LocalValue;
import org.openqa.selenium.bidi.script.Message;
import org.openqa.selenium.bidi.script.RealmInfo;
import org.openqa.selenium.bidi.script.RealmType;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

class ScriptEventsTest extends BaseTest {

@BeforeEach
public void setup() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
driver = new FirefoxDriver(options);
}

@Test
void canListenToChannelMessage()
throws ExecutionException, InterruptedException, TimeoutException {
try (Script script = new Script(driver)) {
CompletableFuture<Message> future = new CompletableFuture<>();
script.onMessage(future::complete);

script.callFunctionInBrowsingContext(
driver.getWindowHandle(),
"(channel) => channel('foo')",
false,
Optional.of(List.of(LocalValue.channelValue("channel_name"))),
Optional.empty(),
Optional.empty());

Message message = future.get(5, TimeUnit.SECONDS);
Assertions.assertEquals("channel_name", message.getChannel());
}
}

@Test
void canListenToRealmCreatedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Script script = new Script(driver)) {
CompletableFuture<RealmInfo> future = new CompletableFuture<>();
script.onRealmCreated(future::complete);

BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());

context.navigate("https://www.selenium.dev/selenium/blankPage");
RealmInfo realmInfo = future.get(5, TimeUnit.SECONDS);
Assertions.assertNotNull(realmInfo.getRealmId());
Assertions.assertEquals(RealmType.WINDOW, realmInfo.getRealmType());
}
}

@Test
@Disabled
void canListenToRealmDestroyedEvent()
throws ExecutionException, InterruptedException, TimeoutException {
try (Script script = new Script(driver)) {
CompletableFuture<RealmInfo> future = new CompletableFuture<>();
script.onRealmDestroyed(future::complete);

BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());

context.close();
RealmInfo realmInfo = future.get(5, TimeUnit.SECONDS);
Assertions.assertNotNull(realmInfo.getRealmId());
Assertions.assertEquals(RealmType.WINDOW, realmInfo.getRealmType());
}
}
}

0 comments on commit e046b56

Please sign in to comment.