Skip to content

Commit dfff04d

Browse files
committed
Split the pull request as requested by @pujagani #12678 (comment)
Using Require.precondition()
1 parent 017b05a commit dfff04d

6 files changed

Lines changed: 199 additions & 262 deletions

File tree

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

Lines changed: 119 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,31 @@ public class BrowsingContext {
4242
private static final String HANDLE_USER_PROMPT = "browsingContext.handleUserPrompt";
4343

4444
protected static final Type LIST_OF_BROWSING_CONTEXT_INFO =
45-
new TypeToken<List<BrowsingContextInfo>>() {}.getType();
45+
new TypeToken<List<BrowsingContextInfo>>() {}.getType();
4646

4747
private final Function<JsonInput, String> browsingContextIdMapper =
48-
jsonInput -> {
49-
Map<String, Object> result = jsonInput.read(Map.class);
50-
return result.getOrDefault(CONTEXT, "").toString();
51-
};
48+
jsonInput -> {
49+
Map<String, Object> result = jsonInput.read(Map.class);
50+
return result.getOrDefault(CONTEXT, "").toString();
51+
};
5252

5353
private final Function<JsonInput, NavigationResult> navigationInfoMapper =
54-
jsonInput -> (NavigationResult) jsonInput.read(NavigationResult.class);
54+
jsonInput -> (NavigationResult) jsonInput.read(NavigationResult.class);
5555

5656
private final Function<JsonInput, List<BrowsingContextInfo>> browsingContextInfoListMapper =
57-
jsonInput -> {
58-
Map<String, Object> result = jsonInput.read(Map.class);
59-
List<Object> contexts = (List<Object>) result.getOrDefault("contexts", new ArrayList<>());
57+
jsonInput -> {
58+
Map<String, Object> result = jsonInput.read(Map.class);
59+
List<Object> contexts = (List<Object>) result.getOrDefault("contexts", new ArrayList<>());
6060

61-
if (contexts.isEmpty()) {
62-
return new ArrayList<>();
63-
}
61+
if (contexts.isEmpty()) {
62+
return new ArrayList<>();
63+
}
6464

65-
Json json = new Json();
66-
String dtr = json.toJson(contexts);
65+
Json json = new Json();
66+
String dtr = json.toJson(contexts);
6767

68-
return json.toType(dtr, LIST_OF_BROWSING_CONTEXT_INFO);
69-
};
68+
return json.toType(dtr, LIST_OF_BROWSING_CONTEXT_INFO);
69+
};
7070

7171
public BrowsingContext(WebDriver driver, String id) {
7272
Require.nonNull("WebDriver", driver);
@@ -96,6 +96,9 @@ public BrowsingContext(WebDriver driver, WindowType type) {
9696
public BrowsingContext(WebDriver driver, WindowType type, String referenceContextId) {
9797
Require.nonNull("WebDriver", driver);
9898
Require.nonNull("Reference browsing context id", referenceContextId);
99+
100+
Require.precondition(!referenceContextId.isEmpty(),"Reference Context id cannot be empty");
101+
99102
if (!(driver instanceof HasBiDi)) {
100103
throw new IllegalArgumentException("WebDriver instance must support BiDi protocol");
101104
}
@@ -110,51 +113,51 @@ public String getId() {
110113

111114
private String create(WindowType type) {
112115
return this.bidi.send(
113-
new Command<>(
114-
"browsingContext.create", Map.of("type", type.toString()), browsingContextIdMapper));
116+
new Command<>(
117+
"browsingContext.create", Map.of("type", type.toString()), browsingContextIdMapper));
115118
}
116119

117120
private String create(WindowType type, String referenceContext) {
118121
return this.bidi.send(
119-
new Command<>(
120-
"browsingContext.create",
121-
Map.of("type", type.toString(), "referenceContext", referenceContext),
122-
browsingContextIdMapper));
122+
new Command<>(
123+
"browsingContext.create",
124+
Map.of("type", type.toString(), "referenceContext", referenceContext),
125+
browsingContextIdMapper));
123126
}
124127

125128
public NavigationResult navigate(String url) {
126129
return this.bidi.send(
127-
new Command<>(
128-
"browsingContext.navigate", Map.of(CONTEXT, id, "url", url), navigationInfoMapper));
130+
new Command<>(
131+
"browsingContext.navigate", Map.of(CONTEXT, id, "url", url), navigationInfoMapper));
129132
}
130133

131134
public NavigationResult navigate(String url, ReadinessState readinessState) {
132135
return this.bidi.send(
133-
new Command<>(
134-
"browsingContext.navigate",
135-
Map.of(CONTEXT, id, "url", url, "wait", readinessState.toString()),
136-
navigationInfoMapper));
136+
new Command<>(
137+
"browsingContext.navigate",
138+
Map.of(CONTEXT, id, "url", url, "wait", readinessState.toString()),
139+
navigationInfoMapper));
137140
}
138141

139142
public List<BrowsingContextInfo> getTree() {
140143
return this.bidi.send(
141-
new Command<>(
142-
"browsingContext.getTree", Map.of("root", id), browsingContextInfoListMapper));
144+
new Command<>(
145+
"browsingContext.getTree", Map.of("root", id), browsingContextInfoListMapper));
143146
}
144147

145148
public List<BrowsingContextInfo> getTree(int maxDepth) {
146149
return this.bidi.send(
147-
new Command<>(
148-
"browsingContext.getTree",
149-
Map.of(
150-
"root", id,
151-
"maxDepth", maxDepth),
152-
browsingContextInfoListMapper));
150+
new Command<>(
151+
"browsingContext.getTree",
152+
Map.of(
153+
"root", id,
154+
"maxDepth", maxDepth),
155+
browsingContextInfoListMapper));
153156
}
154157

155158
public List<BrowsingContextInfo> getTopLevelContexts() {
156159
return this.bidi.send(
157-
new Command<>("browsingContext.getTree", new HashMap<>(), browsingContextInfoListMapper));
160+
new Command<>("browsingContext.getTree", new HashMap<>(), browsingContextInfoListMapper));
158161
}
159162

160163
public NavigationResult reload() {
@@ -164,25 +167,25 @@ public NavigationResult reload() {
164167
// Yet to be implemented by browser vendors
165168
private NavigationResult reload(boolean ignoreCache) {
166169
return this.bidi.send(
167-
new Command<>(
168-
RELOAD, Map.of(CONTEXT, id, "ignoreCache", ignoreCache), navigationInfoMapper));
170+
new Command<>(
171+
RELOAD, Map.of(CONTEXT, id, "ignoreCache", ignoreCache), navigationInfoMapper));
169172
}
170173

171174
// TODO: Handle timeouts in case of Readiness state "interactive" and "complete".
172175
// Refer https://github.com/w3c/webdriver-bidi/issues/188
173176
public NavigationResult reload(ReadinessState readinessState) {
174177
return this.bidi.send(
175-
new Command<>(
176-
RELOAD, Map.of(CONTEXT, id, "wait", readinessState.toString()), navigationInfoMapper));
178+
new Command<>(
179+
RELOAD, Map.of(CONTEXT, id, "wait", readinessState.toString()), navigationInfoMapper));
177180
}
178181

179182
// Yet to be implemented by browser vendors
180183
private NavigationResult reload(boolean ignoreCache, ReadinessState readinessState) {
181184
return this.bidi.send(
182-
new Command<>(
183-
RELOAD,
184-
Map.of(CONTEXT, id, "ignoreCache", ignoreCache, "wait", readinessState.toString()),
185-
navigationInfoMapper));
185+
new Command<>(
186+
RELOAD,
187+
Map.of(CONTEXT, id, "ignoreCache", ignoreCache, "wait", readinessState.toString()),
188+
navigationInfoMapper));
186189
}
187190

188191
public void handleUserPrompt() {
@@ -199,91 +202,91 @@ public void handleUserPrompt(String userText) {
199202

200203
public void handleUserPrompt(boolean accept, String userText) {
201204
this.bidi.send(
202-
new Command<>(
203-
HANDLE_USER_PROMPT, Map.of(CONTEXT, id, "accept", accept, "userText", userText)));
205+
new Command<>(
206+
HANDLE_USER_PROMPT, Map.of(CONTEXT, id, "accept", accept, "userText", userText)));
204207
}
205208

206209
public String captureScreenshot() {
207210
return this.bidi.send(
208-
new Command<>(
209-
"browsingContext.captureScreenshot",
210-
Map.of(CONTEXT, id),
211-
jsonInput -> {
212-
Map<String, Object> result = jsonInput.read(Map.class);
213-
return (String) result.get("data");
214-
}));
211+
new Command<>(
212+
"browsingContext.captureScreenshot",
213+
Map.of(CONTEXT, id),
214+
jsonInput -> {
215+
Map<String, Object> result = jsonInput.read(Map.class);
216+
return (String) result.get("data");
217+
}));
215218
}
216219

217220
public String captureBoxScreenshot(double x, double y, double width, double height) {
218221
return this.bidi.send(
219-
new Command<>(
220-
"browsingContext.captureScreenshot",
221-
Map.of(
222-
CONTEXT,
223-
id,
224-
"clip",
225-
Map.of(
226-
"type", "viewport",
227-
"x", x,
228-
"y", y,
229-
"width", width,
230-
"height", height)),
231-
jsonInput -> {
232-
Map<String, Object> result = jsonInput.read(Map.class);
233-
return (String) result.get("data");
234-
}));
222+
new Command<>(
223+
"browsingContext.captureScreenshot",
224+
Map.of(
225+
CONTEXT,
226+
id,
227+
"clip",
228+
Map.of(
229+
"type", "viewport",
230+
"x", x,
231+
"y", y,
232+
"width", width,
233+
"height", height)),
234+
jsonInput -> {
235+
Map<String, Object> result = jsonInput.read(Map.class);
236+
return (String) result.get("data");
237+
}));
235238
}
236239

237240
public String captureElementScreenshot(String elementId) {
238241
return this.bidi.send(
239-
new Command<>(
240-
"browsingContext.captureScreenshot",
241-
Map.of(
242-
CONTEXT,
243-
id,
244-
"clip",
245-
Map.of(
246-
"type",
247-
"element",
248-
"element",
249-
Map.of("sharedId", elementId),
250-
"scrollIntoView",
251-
false)),
252-
jsonInput -> {
253-
Map<String, Object> result = jsonInput.read(Map.class);
254-
return (String) result.get("data");
255-
}));
242+
new Command<>(
243+
"browsingContext.captureScreenshot",
244+
Map.of(
245+
CONTEXT,
246+
id,
247+
"clip",
248+
Map.of(
249+
"type",
250+
"element",
251+
"element",
252+
Map.of("sharedId", elementId),
253+
"scrollIntoView",
254+
false)),
255+
jsonInput -> {
256+
Map<String, Object> result = jsonInput.read(Map.class);
257+
return (String) result.get("data");
258+
}));
256259
}
257260

258261
public String captureElementScreenshot(String elementId, boolean scrollIntoView) {
259262
return this.bidi.send(
260-
new Command<>(
261-
"browsingContext.captureScreenshot",
262-
Map.of(
263-
CONTEXT,
264-
id,
265-
"clip",
266-
Map.of(
267-
"type",
268-
"element",
269-
"element",
270-
Map.of("sharedId", elementId),
271-
"scrollIntoView",
272-
scrollIntoView)),
273-
jsonInput -> {
274-
Map<String, Object> result = jsonInput.read(Map.class);
275-
return (String) result.get("data");
276-
}));
263+
new Command<>(
264+
"browsingContext.captureScreenshot",
265+
Map.of(
266+
CONTEXT,
267+
id,
268+
"clip",
269+
Map.of(
270+
"type",
271+
"element",
272+
"element",
273+
Map.of("sharedId", elementId),
274+
"scrollIntoView",
275+
scrollIntoView)),
276+
jsonInput -> {
277+
Map<String, Object> result = jsonInput.read(Map.class);
278+
return (String) result.get("data");
279+
}));
277280
}
278281

279282
public void setViewport(double width, double height) {
280283
Require.positive("Viewport width", width);
281284
Require.positive("Viewport height", height);
282285

283286
this.bidi.send(
284-
new Command<>(
285-
"browsingContext.setViewport",
286-
Map.of(CONTEXT, id, "viewport", Map.of("width", width, "height", height))));
287+
new Command<>(
288+
"browsingContext.setViewport",
289+
Map.of(CONTEXT, id, "viewport", Map.of("width", width, "height", height))));
287290
}
288291

289292
public void setViewport(double width, double height, double devicePixelRatio) {
@@ -292,15 +295,15 @@ public void setViewport(double width, double height, double devicePixelRatio) {
292295
Require.positive("Device pixel ratio.", devicePixelRatio);
293296

294297
this.bidi.send(
295-
new Command<>(
296-
"browsingContext.setViewport",
297-
Map.of(
298-
CONTEXT,
299-
id,
300-
"viewport",
301-
Map.of("width", width, "height", height),
302-
"devicePixelRatio",
303-
devicePixelRatio)));
298+
new Command<>(
299+
"browsingContext.setViewport",
300+
Map.of(
301+
CONTEXT,
302+
id,
303+
"viewport",
304+
Map.of("width", width, "height", height),
305+
"devicePixelRatio",
306+
devicePixelRatio)));
304307
}
305308

306309
public void activate() {

0 commit comments

Comments
 (0)