diff --git a/java/src/org/openqa/selenium/bidi/network/Cookie.java b/java/src/org/openqa/selenium/bidi/network/Cookie.java new file mode 100644 index 0000000000000..b7ccdada5bd17 --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/network/Cookie.java @@ -0,0 +1,169 @@ +// 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 java.util.Optional; +import org.openqa.selenium.json.JsonInput; + +public class Cookie { + public enum SameSite { + STRICT("strict"), + LAX("lax"), + NONE("none"); + + private final String type; + + SameSite(String type) { + this.type = type; + } + + @Override + public String toString() { + return type; + } + + public static SameSite findByName(String name) { + SameSite result = null; + for (SameSite type : values()) { + if (type.toString().equalsIgnoreCase(name)) { + result = type; + break; + } + } + return result; + } + } + + private final String name; + private final BytesValue value; + private final String domain; + private final String path; + private final long size; + private final boolean isSecure; + private final boolean isHttpOnly; + private final SameSite sameSite; + private final Optional expiry; + + public Cookie( + String name, + BytesValue value, + String domain, + String path, + long size, + boolean isSecure, + boolean httpOnly, + SameSite sameSite, + Optional expiry) { + this.name = name; + this.value = value; + this.domain = domain; + this.path = path; + this.size = size; + this.isSecure = isSecure; + this.isHttpOnly = httpOnly; + this.sameSite = sameSite; + this.expiry = expiry; + } + + public static Cookie fromJson(JsonInput input) { + String name = null; + BytesValue value = null; + String domain = null; + String path = null; + long size = 0; + boolean isSecure = false; + boolean isHttpOnly = false; + SameSite sameSite = null; + Optional expiry = Optional.empty(); + + input.beginObject(); + while (input.hasNext()) { + switch (input.nextName()) { + case "name": + name = input.read(String.class); + break; + case "value": + value = input.read(BytesValue.class); + break; + case "domain": + domain = input.read(String.class); + break; + case "path": + path = input.read(String.class); + break; + case "size": + size = input.read(Long.class); + break; + case "secure": + isSecure = input.read(Boolean.class); + break; + case "isHttpOnly": + isHttpOnly = input.read(Boolean.class); + break; + case "sameSite": + String sameSiteValue = input.read(String.class); + sameSite = SameSite.findByName(sameSiteValue); + break; + case "expiry": + expiry = input.read(Long.class); + break; + default: + input.skipValue(); + } + } + + input.endObject(); + + return new Cookie(name, value, domain, path, size, isSecure, isHttpOnly, sameSite, expiry); + } + + public String getName() { + return name; + } + + public BytesValue getValue() { + return value; + } + + public String getDomain() { + return domain; + } + + public String getPath() { + return path; + } + + public long getSize() { + return size; + } + + public boolean isSecure() { + return isSecure; + } + + public boolean isHttpOnly() { + return isHttpOnly; + } + + public SameSite getSameSite() { + return sameSite; + } + + public Optional getExpiry() { + return expiry; + } +} diff --git a/java/src/org/openqa/selenium/bidi/network/RequestData.java b/java/src/org/openqa/selenium/bidi/network/RequestData.java index 29b4d06736842..7f513293a1d76 100644 --- a/java/src/org/openqa/selenium/bidi/network/RequestData.java +++ b/java/src/org/openqa/selenium/bidi/network/RequestData.java @@ -19,7 +19,6 @@ import java.util.ArrayList; import java.util.List; -import org.openqa.selenium.Cookie; import org.openqa.selenium.json.JsonInput; import org.openqa.selenium.json.TypeToken; @@ -32,7 +31,6 @@ public class RequestData { private final List
headers; - // TODO: add from json method there private final List cookies; private final long headersSize; diff --git a/java/test/org/openqa/selenium/bidi/network/NetworkEventsTest.java b/java/test/org/openqa/selenium/bidi/network/NetworkEventsTest.java index 2847b9ab8f54d..4451dda2c1e13 100644 --- a/java/test/org/openqa/selenium/bidi/network/NetworkEventsTest.java +++ b/java/test/org/openqa/selenium/bidi/network/NetworkEventsTest.java @@ -19,6 +19,9 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.openqa.selenium.testing.Safely.safelyCall; +import static org.openqa.selenium.testing.drivers.Browser.EDGE; +import static org.openqa.selenium.testing.drivers.Browser.IE; +import static org.openqa.selenium.testing.drivers.Browser.SAFARI; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @@ -27,31 +30,29 @@ import org.junit.jupiter.api.AfterEach; 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.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; +import org.openqa.selenium.testing.JupiterTestBase; +import org.openqa.selenium.testing.NotYetImplemented; +import org.openqa.selenium.testing.Pages; -class NetworkEventsTest { +class NetworkEventsTest extends JupiterTestBase { 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 + @NotYetImplemented(SAFARI) + @NotYetImplemented(IE) + @NotYetImplemented(EDGE) void canListenToBeforeRequestSentEvent() throws ExecutionException, InterruptedException, TimeoutException { try (Network network = new Network(driver)) { @@ -71,6 +72,9 @@ void canListenToBeforeRequestSentEvent() } @Test + @NotYetImplemented(SAFARI) + @NotYetImplemented(IE) + @NotYetImplemented(EDGE) void canListenToResponseStartedEvent() throws ExecutionException, InterruptedException, TimeoutException { try (Network network = new Network(driver)) { @@ -92,6 +96,9 @@ void canListenToResponseStartedEvent() } @Test + @NotYetImplemented(SAFARI) + @NotYetImplemented(IE) + @NotYetImplemented(EDGE) void canListenToResponseCompletedEvent() throws ExecutionException, InterruptedException, TimeoutException { try (Network network = new Network(driver)) { @@ -112,6 +119,30 @@ void canListenToResponseCompletedEvent() } } + @Test + @NotYetImplemented(SAFARI) + @NotYetImplemented(IE) + @NotYetImplemented(EDGE) + void canListenToResponseCompletedEventWithCookie() + throws ExecutionException, InterruptedException, TimeoutException { + try (Network network = new Network(driver)) { + CompletableFuture future = new CompletableFuture<>(); + + driver.get(new Pages(server).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(); + assertThat(requestSent.getBrowsingContextId()).isEqualTo(windowHandle); + assertThat(requestSent.getRequest().getCookies().size()).isEqualTo(1); + assertThat(requestSent.getRequest().getCookies().get(0).getName()).isEqualTo("foo"); + assertThat(requestSent.getRequest().getCookies().get(0).getValue().getValue()) + .isEqualTo("bar"); + } + } + @AfterEach public void quitDriver() { if (driver != null) {