Skip to content

Commit

Permalink
[java][bidi] Add test for using BiDi to navigate and get exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Dec 14, 2022
1 parent a379331 commit e8ae58d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
1 change: 0 additions & 1 deletion java/test/org/openqa/selenium/bidi/BiDiSessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
91 changes: 91 additions & 0 deletions java/test/org/openqa/selenium/bidi/BiDiTest.java
Original file line number Diff line number Diff line change
@@ -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<JavascriptLogEntry> 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);
}
}

0 comments on commit e8ae58d

Please sign in to comment.