|
| 1 | +package dev.selenium.bidirectional.webdriver_bidi.high_level; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Assertions; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.openqa.selenium.By; |
| 7 | +import org.openqa.selenium.bidi.log.ConsoleLogEntry; |
| 8 | +import org.openqa.selenium.bidi.log.JavascriptLogEntry; |
| 9 | +import org.openqa.selenium.firefox.FirefoxDriver; |
| 10 | +import org.openqa.selenium.firefox.FirefoxOptions; |
| 11 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 12 | + |
| 13 | +import dev.selenium.BaseTest; |
| 14 | + |
| 15 | +import java.util.concurrent.CompletableFuture; |
| 16 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 17 | +import java.util.concurrent.ExecutionException; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | +import java.util.concurrent.TimeoutException; |
| 20 | + |
| 21 | +class LogTest extends BaseTest { |
| 22 | + |
| 23 | + @BeforeEach |
| 24 | + public void setup() { |
| 25 | + FirefoxOptions options = new FirefoxOptions(); |
| 26 | + options.setCapability("webSocketUrl", true); |
| 27 | + driver = new FirefoxDriver(options); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void canAddConsoleMessageHandler() |
| 32 | + throws ExecutionException, InterruptedException, TimeoutException { |
| 33 | + CompletableFuture<ConsoleLogEntry> future = new CompletableFuture<>(); |
| 34 | + |
| 35 | + long id = ((RemoteWebDriver) driver).script().addConsoleMessageHandler(future::complete); |
| 36 | + |
| 37 | + driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html"); |
| 38 | + driver.findElement(By.id("consoleLog")).click(); |
| 39 | + |
| 40 | + ConsoleLogEntry logEntry = future.get(5, TimeUnit.SECONDS); |
| 41 | + |
| 42 | + Assertions.assertEquals("Hello, world!", logEntry.getText()); |
| 43 | + |
| 44 | + ((RemoteWebDriver) driver).script().removeConsoleMessageHandler(id); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void canRemoveConsoleMessageHandler() { |
| 49 | + CopyOnWriteArrayList<ConsoleLogEntry> logs = new CopyOnWriteArrayList<>(); |
| 50 | + |
| 51 | + driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html"); |
| 52 | + |
| 53 | + long id = ((RemoteWebDriver) driver).script().addConsoleMessageHandler(logs::add); |
| 54 | + ((RemoteWebDriver) driver).script().removeConsoleMessageHandler(id); |
| 55 | + |
| 56 | + driver.findElement(By.id("consoleLog")).click(); |
| 57 | + |
| 58 | + Assertions.assertEquals(0, logs.size()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void canAddJsErrorHandler() throws ExecutionException, InterruptedException, TimeoutException { |
| 63 | + CompletableFuture<JavascriptLogEntry> future = new CompletableFuture<>(); |
| 64 | + |
| 65 | + long id = ((RemoteWebDriver) driver).script().addJavaScriptErrorHandler(future::complete); |
| 66 | + |
| 67 | + driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html"); |
| 68 | + driver.findElement(By.id("jsException")).click(); |
| 69 | + |
| 70 | + JavascriptLogEntry logEntry = future.get(5, TimeUnit.SECONDS); |
| 71 | + |
| 72 | + Assertions.assertEquals("Error: Not working", logEntry.getText()); |
| 73 | + |
| 74 | + ((RemoteWebDriver) driver).script().removeJavaScriptErrorHandler(id); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void canRemoveJsErrorHandler() { |
| 79 | + CopyOnWriteArrayList<JavascriptLogEntry> logs = new CopyOnWriteArrayList<>(); |
| 80 | + |
| 81 | + long id = ((RemoteWebDriver) driver).script().addJavaScriptErrorHandler(logs::add); |
| 82 | + ((RemoteWebDriver) driver).script().removeJavaScriptErrorHandler(id); |
| 83 | + |
| 84 | + driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html"); |
| 85 | + driver.findElement(By.id("jsException")).click(); |
| 86 | + |
| 87 | + Assertions.assertEquals(0, logs.size()); |
| 88 | + } |
| 89 | +} |
0 commit comments