Skip to content

Commit 738629d

Browse files
committed
restore java and javascript low level bidi examples
1 parent b769019 commit 738629d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+8753
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package dev.selenium.bidirectional.webdriver_bidi;
2+
3+
import dev.selenium.BaseTest;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Disabled;
10+
import org.junit.jupiter.api.Test;
11+
import org.openqa.selenium.By;
12+
import org.openqa.selenium.JavascriptExecutor;
13+
import org.openqa.selenium.Keys;
14+
import org.openqa.selenium.WebElement;
15+
import org.openqa.selenium.bidi.module.Input;
16+
import org.openqa.selenium.firefox.FirefoxDriver;
17+
import org.openqa.selenium.firefox.FirefoxOptions;
18+
import org.openqa.selenium.interactions.Actions;
19+
20+
class ActionsTest extends BaseTest {
21+
private Input input;
22+
23+
private String windowHandle;
24+
25+
@BeforeEach
26+
public void setup() {
27+
FirefoxOptions options = new FirefoxOptions();
28+
options.setCapability("webSocketUrl", true);
29+
driver = new FirefoxDriver(options);
30+
windowHandle = driver.getWindowHandle();
31+
input = new Input(driver);
32+
}
33+
34+
@Test
35+
void canPerformInputActions() {
36+
driver.get("https://www.selenium.dev/selenium/web/formSelectionPage.html");
37+
38+
List<WebElement> options = driver.findElements(By.tagName("option"));
39+
40+
Actions actions = new Actions(driver);
41+
Actions selectThreeOptions =
42+
actions.click(options.get(1)).keyDown(Keys.SHIFT).click(options.get(3)).keyUp(Keys.SHIFT);
43+
44+
input.perform(windowHandle, selectThreeOptions.getSequences());
45+
46+
WebElement showButton = driver.findElement(By.name("showselected"));
47+
showButton.click();
48+
49+
WebElement resultElement = driver.findElement(By.id("result"));
50+
Assertions.assertTrue(resultElement.getText().contains("roquefort parmigiano cheddar"));
51+
}
52+
53+
@Test
54+
void canPerformReleaseAction() {
55+
driver.get("https://www.selenium.dev/selenium/web/bidi/release_action.html");
56+
57+
WebElement inputTextBox = driver.findElement(By.id("keys"));
58+
59+
Actions sendLowercase =
60+
new Actions(driver).keyDown(inputTextBox, "a").keyDown(inputTextBox, "b");
61+
62+
input.perform(windowHandle, sendLowercase.getSequences());
63+
((JavascriptExecutor) driver).executeScript("resetEvents()");
64+
65+
input.release(windowHandle);
66+
67+
List<Map<String, Object>> events =
68+
(List<Map<String, Object>>)
69+
((JavascriptExecutor) driver).executeScript("return allEvents.events");
70+
Assertions.assertEquals("KeyB", events.get(0).get("code"));
71+
Assertions.assertEquals("KeyA", events.get(1).get("code"));
72+
}
73+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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.module.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+
167+
@Test
168+
void canListenToBrowsingContextDestroyedEvent()
169+
throws ExecutionException, InterruptedException, TimeoutException {
170+
try (BrowsingContextInspector inspector = new BrowsingContextInspector(driver)) {
171+
CompletableFuture<BrowsingContextInfo> future = new CompletableFuture<>();
172+
173+
inspector.onBrowsingContextDestroyed(future::complete);
174+
175+
String windowHandle = driver.switchTo().newWindow(WindowType.WINDOW).getWindowHandle();
176+
177+
driver.close();
178+
179+
BrowsingContextInfo browsingContextInfo = future.get(5, TimeUnit.SECONDS);
180+
181+
Assertions.assertEquals(windowHandle, browsingContextInfo.getId());
182+
Assertions.assertTrue(browsingContextInfo.getUrl().contains("about:blank"));
183+
}
184+
}
185+
}

0 commit comments

Comments
 (0)