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