Skip to content

Commit 7504adc

Browse files
authored
[java] Bidi code examples (#1272)
1 parent 178df34 commit 7504adc

File tree

5 files changed

+358
-4
lines changed

5 files changed

+358
-4
lines changed

examples/java/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
<dependency>
2323
<groupId>org.seleniumhq.selenium</groupId>
2424
<artifactId>selenium-java</artifactId>
25-
<version>4.7.1</version>
25+
<version>4.7.2</version>
2626
</dependency>
2727
<dependency>
2828
<groupId>org.seleniumhq.selenium</groupId>
2929
<artifactId>selenium-grid</artifactId>
30-
<version>4.7.1</version>
30+
<version>4.7.2</version>
3131
</dependency>
3232
<dependency>
3333
<groupId>org.slf4j</groupId>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package dev.selenium;
22

33
import org.junit.jupiter.api.BeforeEach;
4-
import org.openqa.selenium.chrome.ChromeDriver;
4+
import org.openqa.selenium.firefox.FirefoxDriver;
55

66
public class BaseFirefoxTest extends BaseTest {
77

88
@BeforeEach
99
public void setup() {
10-
driver = new ChromeDriver();
10+
driver = new FirefoxDriver();
1111
}
1212

1313
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package dev.selenium.bidirectional;
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.WebElement;
10+
import org.openqa.selenium.bidi.LogInspector;
11+
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
12+
import org.openqa.selenium.bidi.browsingcontext.NavigationResult;
13+
import org.openqa.selenium.bidi.log.JavascriptLogEntry;
14+
import org.openqa.selenium.firefox.FirefoxDriver;
15+
import org.openqa.selenium.firefox.FirefoxOptions;
16+
import org.openqa.selenium.support.ui.ExpectedCondition;
17+
import org.openqa.selenium.support.ui.ExpectedConditions;
18+
import org.openqa.selenium.support.ui.WebDriverWait;
19+
20+
import java.time.Duration;
21+
import java.util.concurrent.CompletableFuture;
22+
import java.util.concurrent.ExecutionException;
23+
import java.util.concurrent.TimeUnit;
24+
import java.util.concurrent.TimeoutException;
25+
26+
class BiDiTest {
27+
private WebDriver driver;
28+
29+
@BeforeEach
30+
public void setup() {
31+
FirefoxOptions options = new FirefoxOptions();
32+
options.setCapability("webSocketUrl", true);
33+
driver = new FirefoxDriver(options);
34+
}
35+
36+
@AfterEach
37+
public void cleanup() {
38+
driver.quit();
39+
}
40+
41+
@Test
42+
void testNavigateAndListenToErrors()
43+
throws ExecutionException, InterruptedException, TimeoutException {
44+
try (LogInspector logInspector = new LogInspector(driver)) {
45+
CompletableFuture<JavascriptLogEntry> future = new CompletableFuture<>();
46+
logInspector.onJavaScriptException(future::complete);
47+
48+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
49+
50+
NavigationResult info =
51+
browsingContext.navigate("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
52+
53+
Assertions.assertNotNull(browsingContext.getId());
54+
Assertions.assertNull(info.getNavigationId());
55+
Assertions.assertTrue(info.getUrl().contains("/bidi/logEntryAdded.html"));
56+
57+
new WebDriverWait(driver, Duration.ofSeconds(10))
58+
.until(ExpectedConditions.elementToBeClickable(By.id("jsException"))).click();
59+
60+
JavascriptLogEntry logEntry = future.get(5, TimeUnit.SECONDS);
61+
62+
Assertions.assertEquals("Error: Not working", logEntry.getText());
63+
Assertions.assertEquals("javascript", logEntry.getType());
64+
}
65+
}
66+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package dev.selenium.bidirectional.browsingcontext;
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.WebDriver;
8+
import org.openqa.selenium.WindowType;
9+
import org.openqa.selenium.bidi.BiDiException;
10+
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
11+
import org.openqa.selenium.bidi.browsingcontext.BrowsingContextInfo;
12+
import org.openqa.selenium.bidi.browsingcontext.NavigationResult;
13+
import org.openqa.selenium.bidi.browsingcontext.ReadinessState;
14+
import org.openqa.selenium.firefox.FirefoxDriver;
15+
import org.openqa.selenium.firefox.FirefoxOptions;
16+
17+
import java.util.List;
18+
19+
class BrowsingContextTest {
20+
21+
private WebDriver driver;
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+
@AfterEach
31+
public void cleanup() {
32+
driver.quit();
33+
}
34+
35+
@Test
36+
void testCreateABrowsingContextForGivenId() {
37+
String id = driver.getWindowHandle();
38+
BrowsingContext browsingContext = new BrowsingContext(driver, id);
39+
Assertions.assertEquals(id, browsingContext.getId());
40+
}
41+
42+
@Test
43+
void testCreateAWindow() {
44+
BrowsingContext browsingContext = new BrowsingContext(driver, WindowType.WINDOW);
45+
Assertions.assertNotNull(browsingContext.getId());
46+
}
47+
48+
@Test
49+
void testCreateAWindowWithAReferenceContext() {
50+
BrowsingContext
51+
browsingContext =
52+
new BrowsingContext(driver, WindowType.WINDOW, driver.getWindowHandle());
53+
Assertions.assertNotNull(browsingContext.getId());
54+
}
55+
56+
@Test
57+
void testCreateATab() {
58+
BrowsingContext browsingContext = new BrowsingContext(driver, WindowType.TAB);
59+
Assertions.assertNotNull(browsingContext.getId());
60+
}
61+
62+
@Test
63+
void testCreateATabWithAReferenceContext() {
64+
BrowsingContext
65+
browsingContext =
66+
new BrowsingContext(driver, WindowType.TAB, driver.getWindowHandle());
67+
Assertions.assertNotNull(browsingContext.getId());
68+
}
69+
70+
@Test
71+
void testNavigateToAUrl() {
72+
BrowsingContext browsingContext = new BrowsingContext(driver, WindowType.TAB);
73+
74+
NavigationResult info = browsingContext.navigate("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
75+
76+
Assertions.assertNotNull(browsingContext.getId());
77+
Assertions.assertNull(info.getNavigationId());
78+
Assertions.assertTrue(info.getUrl().contains("/bidi/logEntryAdded.html"));
79+
}
80+
81+
@Test
82+
void testNavigateToAUrlWithReadinessState() {
83+
BrowsingContext browsingContext = new BrowsingContext(driver, WindowType.TAB);
84+
85+
NavigationResult info = browsingContext.navigate("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html",
86+
ReadinessState.COMPLETE);
87+
88+
Assertions.assertNotNull(browsingContext.getId());
89+
Assertions.assertNull(info.getNavigationId());
90+
Assertions.assertTrue(info.getUrl().contains("/bidi/logEntryAdded.html"));
91+
}
92+
93+
@Test
94+
void testGetTreeWithAChild() {
95+
String referenceContextId = driver.getWindowHandle();
96+
BrowsingContext parentWindow = new BrowsingContext(driver, referenceContextId);
97+
98+
parentWindow.navigate("https://www.selenium.dev/selenium/web/iframes.html", ReadinessState.COMPLETE);
99+
100+
List<BrowsingContextInfo> contextInfoList = parentWindow.getTree();
101+
102+
Assertions.assertEquals(1, contextInfoList.size());
103+
BrowsingContextInfo info = contextInfoList.get(0);
104+
Assertions.assertEquals(1, info.getChildren().size());
105+
Assertions.assertEquals(referenceContextId, info.getId());
106+
Assertions.assertTrue(info.getChildren().get(0).getUrl().contains("formPage.html"));
107+
}
108+
109+
@Test
110+
void testGetTreeWithDepth() {
111+
String referenceContextId = driver.getWindowHandle();
112+
BrowsingContext parentWindow = new BrowsingContext(driver, referenceContextId);
113+
114+
parentWindow.navigate("https://www.selenium.dev/selenium/web/iframes.html", ReadinessState.COMPLETE);
115+
116+
List<BrowsingContextInfo> contextInfoList = parentWindow.getTree(0);
117+
118+
Assertions.assertEquals(1, contextInfoList.size());
119+
BrowsingContextInfo info = contextInfoList.get(0);
120+
Assertions.assertNull(info.getChildren()); // since depth is 0
121+
Assertions.assertEquals(referenceContextId, info.getId());
122+
}
123+
124+
@Test
125+
void testGetAllTopLevelContexts() {
126+
BrowsingContext window1 = new BrowsingContext(driver, driver.getWindowHandle());
127+
BrowsingContext window2 = new BrowsingContext(driver, WindowType.WINDOW);
128+
129+
List<BrowsingContextInfo> contextInfoList = window1.getTopLevelContexts();
130+
131+
Assertions.assertEquals(2, contextInfoList.size());
132+
}
133+
134+
@Test
135+
void testCloseAWindow() {
136+
BrowsingContext window1 = new BrowsingContext(driver, WindowType.WINDOW);
137+
BrowsingContext window2 = new BrowsingContext(driver, WindowType.WINDOW);
138+
139+
window2.close();
140+
141+
Assertions.assertThrows(BiDiException.class, window2::getTree);
142+
}
143+
144+
@Test
145+
void testCloseATab() {
146+
BrowsingContext tab1 = new BrowsingContext(driver, WindowType.TAB);
147+
BrowsingContext tab2 = new BrowsingContext(driver, WindowType.TAB);
148+
149+
tab2.close();
150+
151+
Assertions.assertThrows(BiDiException.class, tab2::getTree);
152+
}
153+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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

Comments
 (0)