Skip to content

Commit 75bc83e

Browse files
authored
[java] Add BiDi browsing context module code examples (SeleniumHQ#1578)
1 parent 9fb98df commit 75bc83e

File tree

2 files changed

+361
-0
lines changed

2 files changed

+361
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package dev.selenium.bidirectional.webdriver_bidi;
2+
3+
import dev.selenium.BaseTest;
4+
import java.util.concurrent.CompletableFuture;
5+
import java.util.concurrent.ExecutionException;
6+
import java.util.concurrent.TimeUnit;
7+
import java.util.concurrent.TimeoutException;
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
import org.openqa.selenium.By;
12+
import org.openqa.selenium.WindowType;
13+
import org.openqa.selenium.bidi.BrowsingContextInspector;
14+
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
15+
import org.openqa.selenium.bidi.browsingcontext.BrowsingContextInfo;
16+
import org.openqa.selenium.bidi.browsingcontext.NavigationInfo;
17+
import org.openqa.selenium.bidi.browsingcontext.ReadinessState;
18+
import org.openqa.selenium.bidi.browsingcontext.UserPromptClosed;
19+
import org.openqa.selenium.bidi.browsingcontext.UserPromptOpened;
20+
import org.openqa.selenium.firefox.FirefoxDriver;
21+
import org.openqa.selenium.firefox.FirefoxOptions;
22+
23+
class BrowsingContextInspectorTest extends BaseTest {
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+
void canListenToWindowBrowsingContextCreatedEvent()
33+
throws ExecutionException, InterruptedException, TimeoutException {
34+
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
35+
CompletableFuture<BrowsingContextInfo> future = new CompletableFuture<>();
36+
37+
inspector.onBrowsingContextCreated(future::complete);
38+
39+
String windowHandle = driver.switchTo().newWindow(WindowType.WINDOW).getWindowHandle();
40+
41+
BrowsingContextInfo browsingContextInfo = future.get(5, TimeUnit.SECONDS);
42+
43+
Assertions.assertEquals(windowHandle, browsingContextInfo.getId());
44+
}
45+
}
46+
47+
@Test
48+
void canListenToTabBrowsingContextCreatedEvent()
49+
throws ExecutionException, InterruptedException, TimeoutException {
50+
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
51+
CompletableFuture<BrowsingContextInfo> future = new CompletableFuture<>();
52+
inspector.onBrowsingContextCreated(future::complete);
53+
54+
String windowHandle = driver.switchTo().newWindow(WindowType.TAB).getWindowHandle();
55+
56+
BrowsingContextInfo browsingContextInfo = future.get(5, TimeUnit.SECONDS);
57+
58+
Assertions.assertEquals(windowHandle, browsingContextInfo.getId());
59+
}
60+
}
61+
62+
@Test
63+
void canListenToDomContentLoadedEvent()
64+
throws ExecutionException, InterruptedException, TimeoutException {
65+
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
66+
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();
67+
inspector.onDomContentLoaded(future::complete);
68+
69+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
70+
context.navigate("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html", ReadinessState.COMPLETE);
71+
72+
NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
73+
74+
Assertions.assertTrue(navigationInfo.getUrl().contains("bidi/logEntryAdded"));
75+
}
76+
}
77+
78+
@Test
79+
void canListenToBrowsingContextLoadedEvent()
80+
throws ExecutionException, InterruptedException, TimeoutException {
81+
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
82+
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();
83+
inspector.onBrowsingContextLoaded(future::complete);
84+
85+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
86+
context.navigate("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html", ReadinessState.COMPLETE);
87+
88+
NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
89+
90+
Assertions.assertTrue(navigationInfo.getUrl().contains("bidi/logEntryAdded"));
91+
}
92+
}
93+
94+
@Test
95+
void canListenToNavigationStartedEvent()
96+
throws ExecutionException, InterruptedException, TimeoutException {
97+
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
98+
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();
99+
inspector.onNavigationStarted(future::complete);
100+
101+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
102+
context.navigate("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html", ReadinessState.COMPLETE);
103+
104+
NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
105+
106+
Assertions.assertTrue(navigationInfo.getUrl().contains("bidi/logEntryAdded"));
107+
}
108+
}
109+
110+
@Test
111+
void canListenToFragmentNavigatedEvent()
112+
throws ExecutionException, InterruptedException, TimeoutException {
113+
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
114+
CompletableFuture<NavigationInfo> future = new CompletableFuture<>();
115+
116+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
117+
context.navigate("https://www.selenium.dev/selenium/web/linked_image.html", ReadinessState.COMPLETE);
118+
119+
inspector.onFragmentNavigated(future::complete);
120+
121+
context.navigate("https://www.selenium.dev/selenium/web/linked_image.html#linkToAnchorOnThisPage", ReadinessState.COMPLETE);
122+
123+
NavigationInfo navigationInfo = future.get(5, TimeUnit.SECONDS);
124+
125+
Assertions.assertTrue(navigationInfo.getUrl().contains("linkToAnchorOnThisPage"));
126+
}
127+
}
128+
129+
@Test
130+
void canListenToUserPromptOpenedEvent()
131+
throws ExecutionException, InterruptedException, TimeoutException {
132+
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
133+
CompletableFuture<UserPromptOpened> future = new CompletableFuture<>();
134+
135+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
136+
inspector.onUserPromptOpened(future::complete);
137+
138+
driver.get("https://www.selenium.dev/selenium/web/alerts.html");
139+
140+
driver.findElement(By.id("alert")).click();
141+
142+
UserPromptOpened userPromptOpened = future.get(5, TimeUnit.SECONDS);
143+
Assertions.assertEquals(context.getId(), userPromptOpened.getBrowsingContextId());
144+
}
145+
}
146+
147+
@Test
148+
void canListenToUserPromptClosedEvent()
149+
throws ExecutionException, InterruptedException, TimeoutException {
150+
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
151+
CompletableFuture<UserPromptClosed> future = new CompletableFuture<>();
152+
153+
BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
154+
inspector.onUserPromptClosed(future::complete);
155+
156+
driver.get("https://www.selenium.dev/selenium/web/alerts.html");
157+
158+
driver.findElement(By.id("prompt")).click();
159+
160+
context.handleUserPrompt(true, "selenium");
161+
162+
UserPromptClosed userPromptClosed = future.get(5, TimeUnit.SECONDS);
163+
Assertions.assertEquals(context.getId(), userPromptClosed.getBrowsingContextId());
164+
}
165+
}
166+
}

examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/BrowsingContextTest.java

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
import java.util.List;
55
import org.junit.jupiter.api.Assertions;
66
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Disabled;
78
import org.junit.jupiter.api.Test;
9+
import org.openqa.selenium.By;
10+
import org.openqa.selenium.JavascriptExecutor;
11+
import org.openqa.selenium.Rectangle;
12+
import org.openqa.selenium.WebElement;
813
import org.openqa.selenium.WindowType;
914
import org.openqa.selenium.bidi.BiDiException;
1015
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
@@ -13,6 +18,11 @@
1318
import org.openqa.selenium.bidi.browsingcontext.ReadinessState;
1419
import org.openqa.selenium.firefox.FirefoxDriver;
1520
import org.openqa.selenium.firefox.FirefoxOptions;
21+
import org.openqa.selenium.print.PrintOptions;
22+
import org.openqa.selenium.remote.RemoteWebElement;
23+
24+
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
25+
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;
1626

1727
class BrowsingContextTest extends BaseTest {
1828

@@ -141,4 +151,189 @@ void testCloseATab() {
141151

142152
Assertions.assertThrows(BiDiException.class, tab2::getTree);
143153
}
154+
155+
@Test
156+
void testActivateABrowsingContext() {
157+
BrowsingContext window1 = new BrowsingContext(driver, driver.getWindowHandle());
158+
BrowsingContext window2 = new BrowsingContext(driver, WindowType.WINDOW);
159+
160+
window1.activate();
161+
162+
boolean isFocused = (boolean) ((JavascriptExecutor) driver).executeScript("return document.hasFocus();");
163+
164+
Assertions.assertTrue(isFocused);
165+
}
166+
167+
@Test
168+
void testReloadABrowsingContext() {
169+
BrowsingContext browsingContext = new BrowsingContext(driver, WindowType.TAB);
170+
171+
browsingContext.navigate("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html", ReadinessState.COMPLETE);
172+
173+
NavigationResult reloadInfo = browsingContext.reload(ReadinessState.INTERACTIVE);
174+
175+
Assertions.assertNotNull(reloadInfo.getNavigationId());
176+
Assertions.assertTrue(reloadInfo.getUrl().contains("/bidi/logEntryAdded.html"));
177+
}
178+
179+
@Test
180+
void testHandleUserPrompt() {
181+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
182+
183+
driver.get("https://www.selenium.dev/selenium/web/alerts.html");
184+
185+
driver.findElement(By.id("alert")).click();
186+
187+
browsingContext.handleUserPrompt();
188+
189+
Assertions.assertTrue(driver.getPageSource().contains("Testing Alerts and Stuff"));
190+
}
191+
192+
@Test
193+
void testAcceptUserPrompt() {
194+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
195+
196+
driver.get("https://www.selenium.dev/selenium/web/alerts.html");
197+
198+
driver.findElement(By.id("alert")).click();
199+
200+
browsingContext.handleUserPrompt("true");
201+
202+
Assertions.assertTrue(driver.getPageSource().contains("Testing Alerts and Stuff"));
203+
}
204+
205+
@Test
206+
void testDismissUserPrompt() {
207+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
208+
209+
driver.get("https://www.selenium.dev/selenium/web/alerts.html");
210+
211+
driver.findElement(By.id("alert")).click();
212+
213+
browsingContext.handleUserPrompt("true");
214+
215+
Assertions.assertTrue(driver.getPageSource().contains("Testing Alerts and Stuff"));
216+
}
217+
218+
@Test
219+
void testPassUserTextToUserPrompt() {
220+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
221+
222+
driver.get("https://www.selenium.dev/selenium/web/alerts.html");
223+
224+
driver.findElement(By.id("prompt-with-default")).click();
225+
226+
String userText = "Selenium automates browsers";
227+
browsingContext.handleUserPrompt(true, userText);
228+
229+
Assertions.assertTrue(driver.getPageSource().contains(userText));
230+
}
231+
232+
@Test
233+
void testDismissUserPromptWithText() {
234+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
235+
236+
driver.get("https://www.selenium.dev/selenium/web/alerts.html");
237+
238+
driver.findElement(By.id("prompt-with-default")).click();
239+
240+
String userText = "Selenium automates browsers";
241+
browsingContext.handleUserPrompt(false, userText);
242+
243+
Assertions.assertFalse(driver.getPageSource().contains(userText));
244+
}
245+
246+
@Test
247+
void textCaptureScreenshot() {
248+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
249+
250+
driver.get("https://www.selenium.dev/selenium/web/alerts.html");
251+
252+
String screenshot = browsingContext.captureScreenshot();
253+
254+
Assertions.assertTrue(screenshot.length() > 0);
255+
}
256+
257+
@Test
258+
void textCaptureViewportScreenshot() {
259+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
260+
261+
driver.get("https://www.selenium.dev/selenium/web/coordinates_tests/simple_page.html");
262+
263+
WebElement element = driver.findElement(By.id("box"));
264+
Rectangle elementRectangle = element.getRect();
265+
266+
String screenshot =
267+
browsingContext.captureBoxScreenshot(
268+
elementRectangle.getX(), elementRectangle.getY(), 5, 5);
269+
270+
Assertions.assertTrue(screenshot.length() > 0);
271+
}
272+
273+
@Test
274+
void textCaptureElementScreenshot() {
275+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
276+
277+
driver.get("https://www.selenium.dev/selenium/web/formPage.html");
278+
WebElement element = driver.findElement(By.id("checky"));
279+
280+
String screenshot = browsingContext.captureElementScreenshot(((RemoteWebElement) element).getId());
281+
282+
Assertions.assertTrue(screenshot.length() > 0);
283+
}
284+
285+
@Test
286+
void textSetViewport() {
287+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
288+
driver.get("https://www.selenium.dev/selenium/web/formPage.html");
289+
290+
browsingContext.setViewport(250, 300);
291+
292+
List<Long> newViewportSize =
293+
(List<Long>)
294+
((JavascriptExecutor) driver)
295+
.executeScript("return [window.innerWidth, window.innerHeight];");
296+
297+
Assertions.assertEquals(250, newViewportSize.get(0));
298+
Assertions.assertEquals(300, newViewportSize.get(1));
299+
}
300+
301+
@Test
302+
@Disabled("Supported by Firefox Nightly 124")
303+
void textSetViewportWithDevicePixelRatio() {
304+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
305+
driver.get("https://www.selenium.dev/selenium/web/formPage.html");
306+
307+
browsingContext.setViewport(250, 300, 5);
308+
309+
Long newDevicePixelRatio =
310+
(Long) ((JavascriptExecutor) driver).executeScript("return window.devicePixelRatio");
311+
312+
Assertions.assertEquals(5, newDevicePixelRatio);
313+
}
314+
315+
@Test
316+
void canPrintPage() {
317+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
318+
319+
driver.get("https://www.selenium.dev/selenium/web/formPage.html");
320+
PrintOptions printOptions = new PrintOptions();
321+
322+
String printPage = browsingContext.print(printOptions);
323+
324+
Assertions.assertTrue(printPage.length() > 0);
325+
}
326+
327+
@Test
328+
@Disabled("Supported by Firefox Nightly 124")
329+
void testNavigateBackInTheBrowserHistory() {
330+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
331+
browsingContext.navigate("https://www.selenium.dev/selenium/web/formPage.html", ReadinessState.COMPLETE);
332+
333+
wait.until(visibilityOfElementLocated(By.id("imageButton"))).submit();
334+
wait.until(titleIs("We Arrive Here"));
335+
336+
browsingContext.back();
337+
Assertions.assertTrue(driver.getPageSource().contains("We Leave From Here"));
338+
}
144339
}

0 commit comments

Comments
 (0)