Skip to content

Commit 6f4ce7e

Browse files
committed
[bidi][java] Add method to get browsing context tree with root
1 parent 401e5dd commit 6f4ce7e

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ public List<BrowsingContextInfo> getTree() {
148148
"browsingContext.getTree", Map.of("root", id), browsingContextInfoListMapper));
149149
}
150150

151+
public List<BrowsingContextInfo> getTree(String root) {
152+
return this.bidi.send(
153+
new Command<>(
154+
"browsingContext.getTree", Map.of("root", root), browsingContextInfoListMapper));
155+
}
156+
151157
public List<BrowsingContextInfo> getTree(int maxDepth) {
152158
return this.bidi.send(
153159
new Command<>(
@@ -158,6 +164,16 @@ public List<BrowsingContextInfo> getTree(int maxDepth) {
158164
browsingContextInfoListMapper));
159165
}
160166

167+
public List<BrowsingContextInfo> getTree(String root, int maxDepth) {
168+
return this.bidi.send(
169+
new Command<>(
170+
"browsingContext.getTree",
171+
Map.of(
172+
"root", root,
173+
"maxDepth", maxDepth),
174+
browsingContextInfoListMapper));
175+
}
176+
161177
public List<BrowsingContextInfo> getTopLevelContexts() {
162178
return this.bidi.send(
163179
new Command<>("browsingContext.getTree", new HashMap<>(), browsingContextInfoListMapper));

java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;
2525

2626
import java.util.List;
27+
2728
import org.junit.jupiter.api.Test;
2829
import org.openqa.selenium.By;
2930
import org.openqa.selenium.JavascriptExecutor;
@@ -164,6 +165,49 @@ void canGetTreeWithDepth() {
164165
assertThat(info.getUserContext()).isEqualTo("default");
165166
}
166167

168+
@Test
169+
@NeedsFreshDriver
170+
void canGetTreeWithRootAndDepth() {
171+
String referenceContextId = driver.getWindowHandle();
172+
BrowsingContext parentWindow = new BrowsingContext(driver, referenceContextId);
173+
174+
String url = appServer.whereIs("iframes.html");
175+
176+
parentWindow.navigate(url, ReadinessState.COMPLETE);
177+
178+
List<BrowsingContextInfo> contextInfoList = parentWindow.getTree(referenceContextId, 1);
179+
180+
assertThat(contextInfoList.size()).isEqualTo(1);
181+
BrowsingContextInfo info = contextInfoList.get(0);
182+
assertThat(info.getChildren()).isNotNull(); // since depth is 1
183+
assertThat(info.getId()).isEqualTo(referenceContextId);
184+
assertThat(info.getOriginalOpener()).isNull();
185+
assertThat(info.getClientWindow()).isNotNull();
186+
assertThat(info.getUserContext()).isEqualTo("default");
187+
}
188+
189+
@Test
190+
@NeedsFreshDriver
191+
void canGetTreeWithRoot() {
192+
String referenceContextId = driver.getWindowHandle();
193+
BrowsingContext parentWindow = new BrowsingContext(driver, referenceContextId);
194+
195+
String url = appServer.whereIs("iframes.html");
196+
197+
parentWindow.navigate(url, ReadinessState.COMPLETE);
198+
199+
BrowsingContext tab = new BrowsingContext(driver, WindowType.TAB);
200+
201+
List<BrowsingContextInfo> contextInfoList = parentWindow.getTree(tab.getId());
202+
203+
assertThat(contextInfoList.size()).isEqualTo(1);
204+
BrowsingContextInfo info = contextInfoList.get(0);
205+
assertThat(info.getId()).isEqualTo(tab.getId());
206+
assertThat(info.getOriginalOpener()).isNull();
207+
assertThat(info.getClientWindow()).isNotNull();
208+
assertThat(info.getUserContext()).isEqualTo("default");
209+
}
210+
167211
@Test
168212
@NeedsFreshDriver
169213
void canGetAllTopLevelContexts() {

0 commit comments

Comments
 (0)