|
20 | 20 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
21 | 21 |
|
22 | 22 | import org.junit.jupiter.api.Test; |
| 23 | +import org.openqa.selenium.By; |
23 | 24 | import org.openqa.selenium.WebDriver; |
| 25 | +import org.openqa.selenium.WindowType; |
24 | 26 | import org.openqa.selenium.bidi.BiDi; |
25 | 27 | import org.openqa.selenium.bidi.BiDiSessionStatus; |
26 | 28 | import org.openqa.selenium.bidi.Command; |
27 | 29 | import org.openqa.selenium.bidi.HasBiDi; |
| 30 | +import org.openqa.selenium.bidi.LogInspector; |
| 31 | +import org.openqa.selenium.bidi.browsingcontext.BrowsingContext; |
| 32 | +import org.openqa.selenium.bidi.browsingcontext.NavigationResult; |
| 33 | +import org.openqa.selenium.bidi.log.ConsoleLogEntry; |
| 34 | +import org.openqa.selenium.bidi.log.LogLevel; |
| 35 | +import org.openqa.selenium.environment.webserver.AppServer; |
| 36 | +import org.openqa.selenium.environment.webserver.NettyAppServer; |
28 | 37 | import org.openqa.selenium.firefox.FirefoxOptions; |
29 | 38 | import org.openqa.selenium.grid.config.TomlConfig; |
30 | 39 | import org.openqa.selenium.grid.router.DeploymentTypes.Deployment; |
|
34 | 43 |
|
35 | 44 | import java.io.StringReader; |
36 | 45 | import java.util.Collections; |
| 46 | +import java.util.concurrent.CompletableFuture; |
| 47 | +import java.util.concurrent.ExecutionException; |
| 48 | +import java.util.concurrent.TimeUnit; |
| 49 | +import java.util.concurrent.TimeoutException; |
37 | 50 |
|
38 | 51 | class RemoteWebDriverBiDiTest { |
39 | 52 |
|
@@ -61,4 +74,74 @@ void ensureBiDiSessionCreation() { |
61 | 74 | assertThat(status.getMessage()).isEqualTo("Session already started"); |
62 | 75 | } |
63 | 76 | } |
| 77 | + |
| 78 | + @Test |
| 79 | + void canListenToLogs() throws ExecutionException, InterruptedException, TimeoutException { |
| 80 | + Browser browser = Browser.FIREFOX; |
| 81 | + |
| 82 | + Deployment deployment = DeploymentTypes.STANDALONE.start( |
| 83 | + browser.getCapabilities(), |
| 84 | + new TomlConfig(new StringReader( |
| 85 | + "[node]\n" + |
| 86 | + "driver-implementation = " + browser.displayName()))); |
| 87 | + |
| 88 | + FirefoxOptions options = new FirefoxOptions(); |
| 89 | + // Enable BiDi |
| 90 | + options.setCapability("webSocketUrl", true); |
| 91 | + |
| 92 | + WebDriver driver = new RemoteWebDriver(deployment.getServer().getUrl(), options); |
| 93 | + driver = new Augmenter().augment(driver); |
| 94 | + |
| 95 | + AppServer server = new NettyAppServer(); |
| 96 | + server.start(); |
| 97 | + |
| 98 | + try (LogInspector logInspector = new LogInspector(driver)) { |
| 99 | + CompletableFuture<ConsoleLogEntry> future = new CompletableFuture<>(); |
| 100 | + logInspector.onConsoleEntry(future::complete); |
| 101 | + |
| 102 | + String page = server.whereIs("/bidi/logEntryAdded.html"); |
| 103 | + driver.get(page); |
| 104 | + driver.findElement(By.id("consoleLog")).click(); |
| 105 | + |
| 106 | + ConsoleLogEntry logEntry = future.get(5, TimeUnit.SECONDS); |
| 107 | + |
| 108 | + assertThat(logEntry.getText()).isEqualTo("Hello, world!"); |
| 109 | + assertThat(logEntry.getRealm()).isNull(); |
| 110 | + assertThat(logEntry.getArgs().size()).isEqualTo(1); |
| 111 | + assertThat(logEntry.getType()).isEqualTo("console"); |
| 112 | + assertThat(logEntry.getLevel()).isEqualTo(LogLevel.INFO); |
| 113 | + assertThat(logEntry.getMethod()).isEqualTo("log"); |
| 114 | + assertThat(logEntry.getStackTrace()).isNull(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void canNavigateToUrl() throws ExecutionException, InterruptedException, TimeoutException { |
| 120 | + Browser browser = Browser.FIREFOX; |
| 121 | + |
| 122 | + Deployment deployment = DeploymentTypes.STANDALONE.start( |
| 123 | + browser.getCapabilities(), |
| 124 | + new TomlConfig(new StringReader( |
| 125 | + "[node]\n" + |
| 126 | + "driver-implementation = " + browser.displayName()))); |
| 127 | + |
| 128 | + FirefoxOptions options = new FirefoxOptions(); |
| 129 | + // Enable BiDi |
| 130 | + options.setCapability("webSocketUrl", true); |
| 131 | + |
| 132 | + WebDriver driver = new RemoteWebDriver(deployment.getServer().getUrl(), options); |
| 133 | + driver = new Augmenter().augment(driver); |
| 134 | + |
| 135 | + AppServer server = new NettyAppServer(); |
| 136 | + server.start(); |
| 137 | + |
| 138 | + BrowsingContext browsingContext = new BrowsingContext(driver, WindowType.TAB); |
| 139 | + |
| 140 | + String url = server.whereIs("/bidi/logEntryAdded.html"); |
| 141 | + NavigationResult info = browsingContext.navigate(url); |
| 142 | + |
| 143 | + assertThat(browsingContext.getId()).isNotEmpty(); |
| 144 | + assertThat(info.getNavigationId()).isNull(); |
| 145 | + assertThat(info.getUrl()).contains("/bidi/logEntryAdded.html"); |
| 146 | + } |
64 | 147 | } |
0 commit comments