-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[java][bidi] Add mechanism to listen to network event - beforeRequest…
…Sent
- Loading branch information
Showing
4 changed files
with
182 additions
and
6 deletions.
There are no files selected for viewing
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,69 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.bidi; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.bidi.network.BeforeRequestSent; | ||
import org.openqa.selenium.internal.Require; | ||
|
||
public class Network implements AutoCloseable { | ||
|
||
private final Set<String> browsingContextIds; | ||
|
||
private final BiDi bidi; | ||
|
||
private final Event<BeforeRequestSent> beforeRequestSentEvent = | ||
new Event<>("network.beforeRequestSent", BeforeRequestSent::fromJsonMap); | ||
|
||
public Network(WebDriver driver) { | ||
this(new HashSet<>(), driver); | ||
} | ||
|
||
public Network(String browsingContextId, WebDriver driver) { | ||
this(Collections.singleton(Require.nonNull("Browsing context id", browsingContextId)), driver); | ||
} | ||
|
||
public Network(Set<String> browsingContextIds, WebDriver driver) { | ||
Require.nonNull("WebDriver", driver); | ||
Require.nonNull("Browsing context id list", browsingContextIds); | ||
|
||
if (!(driver instanceof HasBiDi)) { | ||
throw new IllegalArgumentException("WebDriver instance must support BiDi protocol"); | ||
} | ||
|
||
this.bidi = ((HasBiDi) driver).getBiDi(); | ||
this.browsingContextIds = browsingContextIds; | ||
} | ||
|
||
public void onBeforeRequestSent(Consumer<BeforeRequestSent> consumer) { | ||
if (browsingContextIds.isEmpty()) { | ||
this.bidi.addListener(beforeRequestSentEvent, consumer); | ||
} else { | ||
this.bidi.addListener(browsingContextIds, beforeRequestSentEvent, consumer); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
this.bidi.clearListener(beforeRequestSentEvent); | ||
} | ||
} |
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
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,28 @@ | ||
load("@rules_jvm_external//:defs.bzl", "artifact") | ||
load("//java:defs.bzl", "JUNIT5_DEPS", "java_selenium_test_suite") | ||
|
||
java_selenium_test_suite( | ||
name = "large-tests", | ||
size = "large", | ||
srcs = glob(["*Test.java"]), | ||
browsers = [ | ||
"firefox", | ||
], | ||
tags = [ | ||
"selenium-remote", | ||
], | ||
deps = [ | ||
"//java/src/org/openqa/selenium/bidi", | ||
"//java/src/org/openqa/selenium/firefox", | ||
"//java/src/org/openqa/selenium/grid/security", | ||
"//java/src/org/openqa/selenium/json", | ||
"//java/src/org/openqa/selenium/remote", | ||
"//java/src/org/openqa/selenium/support", | ||
"//java/test/org/openqa/selenium/environment", | ||
"//java/test/org/openqa/selenium/testing:annotations", | ||
"//java/test/org/openqa/selenium/testing:test-base", | ||
"//java/test/org/openqa/selenium/testing/drivers", | ||
artifact("org.junit.jupiter:junit-jupiter-api"), | ||
artifact("org.assertj:assertj-core"), | ||
] + JUNIT5_DEPS, | ||
) |
80 changes: 80 additions & 0 deletions
80
java/test/org/openqa/selenium/bidi/network/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,80 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.bidi.network; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.openqa.selenium.testing.Safely.safelyCall; | ||
|
||
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.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.openqa.selenium.bidi.Network; | ||
import org.openqa.selenium.environment.webserver.AppServer; | ||
import org.openqa.selenium.environment.webserver.NettyAppServer; | ||
import org.openqa.selenium.firefox.FirefoxDriver; | ||
import org.openqa.selenium.firefox.FirefoxOptions; | ||
import org.openqa.selenium.testing.drivers.Browser; | ||
|
||
class NetworkEventsTest { | ||
|
||
private String page; | ||
private AppServer server; | ||
private FirefoxDriver driver; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
FirefoxOptions options = (FirefoxOptions) Browser.FIREFOX.getCapabilities(); | ||
options.setCapability("webSocketUrl", true); | ||
|
||
driver = new FirefoxDriver(options); | ||
|
||
server = new NettyAppServer(); | ||
server.start(); | ||
} | ||
|
||
@Test | ||
void canListenToBeforeRequestSentEvent() | ||
throws ExecutionException, InterruptedException, TimeoutException { | ||
try (Network network = new Network(driver)) { | ||
CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>(); | ||
network.onBeforeRequestSent(future::complete); | ||
page = server.whereIs("/bidi/logEntryAdded.html"); | ||
driver.get(page); | ||
|
||
BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS); | ||
String windowHandle = driver.getWindowHandle(); | ||
assertThat(requestSent.getBrowsingContextId()).isEqualTo(windowHandle); | ||
assertThat(requestSent.getRequest().getRequestId()).isNotNull(); | ||
assertThat(requestSent.getRequest().getMethod()).isEqualToIgnoringCase("get"); | ||
assertThat(requestSent.getRequest().getUrl()).isNotNull(); | ||
assertThat(requestSent.getInitiator().getType().toString()).isEqualToIgnoringCase("other"); | ||
} | ||
} | ||
|
||
@AfterEach | ||
public void quitDriver() { | ||
if (driver != null) { | ||
driver.quit(); | ||
} | ||
safelyCall(server::stop); | ||
} | ||
} |