-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[bidi][java] Add code examples for Script and Network module events
- Loading branch information
Showing
2 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/NetworkEventsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/ScriptEventsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |