|
| 1 | +package dev.selenium.bidirectional.log; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.AfterEach; |
| 4 | +import org.junit.jupiter.api.Assertions; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.openqa.selenium.By; |
| 8 | +import org.openqa.selenium.WebDriver; |
| 9 | +import org.openqa.selenium.bidi.LogInspector; |
| 10 | +import org.openqa.selenium.bidi.log.BaseLogEntry; |
| 11 | +import org.openqa.selenium.bidi.log.ConsoleLogEntry; |
| 12 | +import org.openqa.selenium.bidi.log.JavascriptLogEntry; |
| 13 | +import org.openqa.selenium.bidi.log.StackTrace; |
| 14 | +import org.openqa.selenium.firefox.FirefoxDriver; |
| 15 | +import org.openqa.selenium.firefox.FirefoxOptions; |
| 16 | + |
| 17 | +import java.util.concurrent.CompletableFuture; |
| 18 | +import java.util.concurrent.ExecutionException; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import java.util.concurrent.TimeoutException; |
| 21 | + |
| 22 | +class LogInspectorTest { |
| 23 | + |
| 24 | + private WebDriver driver; |
| 25 | + |
| 26 | + @BeforeEach |
| 27 | + public void setup() { |
| 28 | + FirefoxOptions options = new FirefoxOptions(); |
| 29 | + options.setCapability("webSocketUrl", true); |
| 30 | + driver = new FirefoxDriver(options); |
| 31 | + } |
| 32 | + |
| 33 | + @AfterEach |
| 34 | + public void cleanup() { |
| 35 | + driver.quit(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void testListenToConsoleLog() throws ExecutionException, InterruptedException, TimeoutException { |
| 40 | + try (LogInspector logInspector = new LogInspector(driver)) { |
| 41 | + CompletableFuture<ConsoleLogEntry> future = new CompletableFuture<>(); |
| 42 | + logInspector.onConsoleLog(future::complete); |
| 43 | + |
| 44 | + driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html"); |
| 45 | + driver.findElement(By.id("consoleLog")).click(); |
| 46 | + |
| 47 | + ConsoleLogEntry logEntry = future.get(5, TimeUnit.SECONDS); |
| 48 | + |
| 49 | + Assertions.assertEquals("Hello, world!", logEntry.getText()); |
| 50 | + Assertions.assertNull(logEntry.getRealm()); |
| 51 | + Assertions.assertEquals(1, logEntry.getArgs().size()); |
| 52 | + Assertions.assertEquals("console", logEntry.getType()); |
| 53 | + Assertions.assertEquals("log", logEntry.getMethod()); |
| 54 | + Assertions.assertNull(logEntry.getStackTrace()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testListenToJavascriptLog() |
| 60 | + throws ExecutionException, InterruptedException, TimeoutException { |
| 61 | + try (LogInspector logInspector = new LogInspector(driver)) { |
| 62 | + CompletableFuture<JavascriptLogEntry> future = new CompletableFuture<>(); |
| 63 | + logInspector.onJavaScriptLog(future::complete); |
| 64 | + |
| 65 | + driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html"); |
| 66 | + driver.findElement(By.id("jsException")).click(); |
| 67 | + |
| 68 | + JavascriptLogEntry logEntry = future.get(5, TimeUnit.SECONDS); |
| 69 | + |
| 70 | + Assertions.assertEquals("Error: Not working", logEntry.getText()); |
| 71 | + Assertions.assertEquals("javascript", logEntry.getType()); |
| 72 | + Assertions.assertEquals(BaseLogEntry.LogLevel.ERROR, logEntry.getLevel()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testListenToJavascriptErrorLog() |
| 78 | + throws ExecutionException, InterruptedException, TimeoutException { |
| 79 | + try (LogInspector logInspector = new LogInspector(driver)) { |
| 80 | + CompletableFuture<JavascriptLogEntry> future = new CompletableFuture<>(); |
| 81 | + logInspector.onJavaScriptException(future::complete); |
| 82 | + |
| 83 | + driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html"); |
| 84 | + driver.findElement(By.id("jsException")).click(); |
| 85 | + |
| 86 | + JavascriptLogEntry logEntry = future.get(5, TimeUnit.SECONDS); |
| 87 | + |
| 88 | + Assertions.assertEquals("Error: Not working", logEntry.getText()); |
| 89 | + Assertions.assertEquals("javascript", logEntry.getType()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testRetrieveStacktraceForALog() |
| 95 | + throws ExecutionException, InterruptedException, TimeoutException { |
| 96 | + try (LogInspector logInspector = new LogInspector(driver)) { |
| 97 | + CompletableFuture<JavascriptLogEntry> future = new CompletableFuture<>(); |
| 98 | + logInspector.onJavaScriptException(future::complete); |
| 99 | + |
| 100 | + driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html"); |
| 101 | + driver.findElement(By.id("logWithStacktrace")).click(); |
| 102 | + |
| 103 | + JavascriptLogEntry logEntry = future.get(5, TimeUnit.SECONDS); |
| 104 | + |
| 105 | + StackTrace stackTrace = logEntry.getStackTrace(); |
| 106 | + Assertions.assertNotNull(stackTrace); |
| 107 | + Assertions.assertEquals(4, stackTrace.getCallFrames().size()); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void testListenToLogsWithMultipleConsumers() |
| 113 | + throws ExecutionException, InterruptedException, TimeoutException { |
| 114 | + try (LogInspector logInspector = new LogInspector(driver)) { |
| 115 | + CompletableFuture<JavascriptLogEntry> completableFuture1 = new CompletableFuture<>(); |
| 116 | + logInspector.onJavaScriptLog(completableFuture1::complete); |
| 117 | + |
| 118 | + CompletableFuture<JavascriptLogEntry> completableFuture2 = new CompletableFuture<>(); |
| 119 | + logInspector.onJavaScriptLog(completableFuture2::complete); |
| 120 | + |
| 121 | + driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html"); |
| 122 | + driver.findElement(By.id("jsException")).click(); |
| 123 | + |
| 124 | + JavascriptLogEntry logEntry = completableFuture1.get(5, TimeUnit.SECONDS); |
| 125 | + |
| 126 | + Assertions.assertEquals("Error: Not working", logEntry.getText()); |
| 127 | + Assertions.assertEquals("javascript", logEntry.getType()); |
| 128 | + |
| 129 | + logEntry = completableFuture2.get(5, TimeUnit.SECONDS); |
| 130 | + |
| 131 | + Assertions.assertEquals("Error: Not working", logEntry.getText()); |
| 132 | + Assertions.assertEquals("javascript", logEntry.getType()); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments