Skip to content

Commit

Permalink
[java][bidi] Add mechanism to listen to network event - beforeRequest…
Browse files Browse the repository at this point in the history
…Sent
  • Loading branch information
pujagani committed Oct 23, 2023
1 parent 83944ed commit fd9fbaa
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 6 deletions.
69 changes: 69 additions & 0 deletions java/src/org/openqa/selenium/bidi/Network.java
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);
}
}
11 changes: 5 additions & 6 deletions java/src/org/openqa/selenium/bidi/network/BeforeRequestSent.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.openqa.selenium.bidi.network;

import java.io.StringReader;
import java.util.Map;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonInput;

import java.io.StringReader;

public class BeforeRequestSent extends BaseParameters {
private static final Json JSON = new Json();

Expand All @@ -22,12 +22,11 @@ private BeforeRequestSent(BaseParameters baseParameters, Initiator initiator) {
this.initiator = initiator;
}

public static BeforeRequestSent fromJsonString(String jsonString) {
try (StringReader baseParameterReader = new StringReader(jsonString);
StringReader initiatorReader = new StringReader(jsonString);
public static BeforeRequestSent fromJsonMap(Map<String, Object> jsonMap) {
try (StringReader baseParameterReader = new StringReader(JSON.toJson(jsonMap));
StringReader initiatorReader = new StringReader(JSON.toJson(jsonMap.get("initiator")));
JsonInput baseParamsInput = JSON.newInput(baseParameterReader);
JsonInput initiatorInput = JSON.newInput(initiatorReader)) {

return new BeforeRequestSent(
BaseParameters.fromJson(baseParamsInput), Initiator.fromJson(initiatorInput));
}
Expand Down
28 changes: 28 additions & 0 deletions java/test/org/openqa/selenium/bidi/network/BUILD.bazel
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 java/test/org/openqa/selenium/bidi/network/NetworkEventsTest.java
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);
}
}

0 comments on commit fd9fbaa

Please sign in to comment.