From e8ae58d3a95581f5d6ee04a55345cc1f038d2a47 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Wed, 14 Dec 2022 18:28:36 +0530 Subject: [PATCH] [java][bidi] Add test for using BiDi to navigate and get exceptions --- .../openqa/selenium/bidi/BiDiSessionTest.java | 1 - .../org/openqa/selenium/bidi/BiDiTest.java | 91 +++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 java/test/org/openqa/selenium/bidi/BiDiTest.java diff --git a/java/test/org/openqa/selenium/bidi/BiDiSessionTest.java b/java/test/org/openqa/selenium/bidi/BiDiSessionTest.java index 36c9c27705fe9..74c409205b8cb 100644 --- a/java/test/org/openqa/selenium/bidi/BiDiSessionTest.java +++ b/java/test/org/openqa/selenium/bidi/BiDiSessionTest.java @@ -18,7 +18,6 @@ package org.openqa.selenium.bidi; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; diff --git a/java/test/org/openqa/selenium/bidi/BiDiTest.java b/java/test/org/openqa/selenium/bidi/BiDiTest.java new file mode 100644 index 0000000000000..36648523c8fc3 --- /dev/null +++ b/java/test/org/openqa/selenium/bidi/BiDiTest.java @@ -0,0 +1,91 @@ +// 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 static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.openqa.selenium.testing.Safely.safelyCall; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.bidi.browsingcontext.BrowsingContext; +import org.openqa.selenium.bidi.browsingcontext.NavigationResult; +import org.openqa.selenium.bidi.log.BaseLogEntry; +import org.openqa.selenium.bidi.log.JavascriptLogEntry; +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 java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +class BiDiTest { + + String page; + private AppServer server; + private FirefoxDriver driver; + + @BeforeEach + public void setUp() { + FirefoxOptions options = new FirefoxOptions(); + options.setCapability("webSocketUrl", true); + + driver = new FirefoxDriver(options); + + server = new NettyAppServer(); + server.start(); + } + + @Test + void canNavigateAndListenToErrors() + throws ExecutionException, InterruptedException, TimeoutException { + try (LogInspector logInspector = new LogInspector(driver)) { + CompletableFuture future = new CompletableFuture<>(); + logInspector.onJavaScriptException(future::complete); + + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); + + page = server.whereIs("/bidi/logEntryAdded.html"); + NavigationResult info = browsingContext.navigate(page); + + assertThat(browsingContext.getId()).isNotEmpty(); + assertThat(info.getNavigationId()).isNull(); + assertThat(info.getUrl()).contains("/bidi/logEntryAdded.html"); + + driver.findElement(By.id("jsException")).click(); + + JavascriptLogEntry logEntry = future.get(5, TimeUnit.SECONDS); + + assertThat(logEntry.getText()).isEqualTo("Error: Not working"); + assertThat(logEntry.getType()).isEqualTo("javascript"); + assertThat(logEntry.getLevel()).isEqualTo(BaseLogEntry.LogLevel.ERROR); + } + } + + @AfterEach + public void quitDriver() { + if (driver != null) { + driver.quit(); + } + safelyCall(server::stop); + } +}