|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.bidi.network; |
| 19 | + |
| 20 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 21 | +import static org.openqa.selenium.testing.Safely.safelyCall; |
| 22 | + |
| 23 | +import java.util.concurrent.CompletableFuture; |
| 24 | +import java.util.concurrent.ExecutionException; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import java.util.concurrent.TimeoutException; |
| 27 | +import org.junit.jupiter.api.AfterEach; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.openqa.selenium.bidi.Network; |
| 31 | +import org.openqa.selenium.environment.webserver.AppServer; |
| 32 | +import org.openqa.selenium.environment.webserver.NettyAppServer; |
| 33 | +import org.openqa.selenium.firefox.FirefoxDriver; |
| 34 | +import org.openqa.selenium.firefox.FirefoxOptions; |
| 35 | +import org.openqa.selenium.testing.drivers.Browser; |
| 36 | + |
| 37 | +class NetworkEventsTest { |
| 38 | + |
| 39 | + private String page; |
| 40 | + private AppServer server; |
| 41 | + private FirefoxDriver driver; |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + public void setUp() { |
| 45 | + FirefoxOptions options = (FirefoxOptions) Browser.FIREFOX.getCapabilities(); |
| 46 | + options.setCapability("webSocketUrl", true); |
| 47 | + |
| 48 | + driver = new FirefoxDriver(options); |
| 49 | + |
| 50 | + server = new NettyAppServer(); |
| 51 | + server.start(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void canListenToBeforeRequestSentEvent() |
| 56 | + throws ExecutionException, InterruptedException, TimeoutException { |
| 57 | + try (Network network = new Network(driver)) { |
| 58 | + CompletableFuture<BeforeRequestSent> future = new CompletableFuture<>(); |
| 59 | + network.onBeforeRequestSent(future::complete); |
| 60 | + page = server.whereIs("/bidi/logEntryAdded.html"); |
| 61 | + driver.get(page); |
| 62 | + |
| 63 | + BeforeRequestSent requestSent = future.get(5, TimeUnit.SECONDS); |
| 64 | + String windowHandle = driver.getWindowHandle(); |
| 65 | + assertThat(requestSent.getBrowsingContextId()).isEqualTo(windowHandle); |
| 66 | + assertThat(requestSent.getRequest().getRequestId()).isNotNull(); |
| 67 | + assertThat(requestSent.getRequest().getMethod()).isEqualToIgnoringCase("get"); |
| 68 | + assertThat(requestSent.getRequest().getUrl()).isNotNull(); |
| 69 | + assertThat(requestSent.getInitiator().getType().toString()).isEqualToIgnoringCase("other"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @AfterEach |
| 74 | + public void quitDriver() { |
| 75 | + if (driver != null) { |
| 76 | + driver.quit(); |
| 77 | + } |
| 78 | + safelyCall(server::stop); |
| 79 | + } |
| 80 | +} |
0 commit comments