Skip to content

Commit 1263bc6

Browse files
committed
add BiDi method "BrowsingContext.resetViewport"
Implementation detail: according to BiDi spec (https://www.w3.org/TR/webdriver-bidi/#command-browsingContext-setViewport), we have to call method "setViewport" with "viewport" parameter equal to "null".
1 parent 5fdd334 commit 1263bc6

File tree

4 files changed

+62
-36
lines changed

4 files changed

+62
-36
lines changed

java/src/org/openqa/selenium/bidi/Command.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
package org.openqa.selenium.bidi;
1919

20+
import static java.util.Collections.unmodifiableMap;
21+
2022
import java.lang.reflect.Type;
23+
import java.util.HashMap;
2124
import java.util.Map;
2225
import java.util.function.Function;
2326
import org.openqa.selenium.internal.Require;
@@ -48,7 +51,8 @@ public Command(
4851
Function<JsonInput, X> mapper,
4952
boolean sendsResponse) {
5053
this.method = Require.nonNull("Method name", method);
51-
this.params = Map.copyOf(Require.nonNull("Command parameters", params));
54+
this.params =
55+
unmodifiableMap(new HashMap<String, Object>(Require.nonNull("Command parameters", params)));
5256
this.mapper = Require.nonNull("Mapper for result", mapper);
5357
this.sendsResponse = sendsResponse;
5458
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ public void setViewport(double width, double height, double devicePixelRatio) {
329329
devicePixelRatio)));
330330
}
331331

332+
public void resetViewport() {
333+
Map<String, Object> params = new HashMap<>();
334+
params.put(CONTEXT, id);
335+
params.put("viewport", null);
336+
params.put("devicePixelRatio", null);
337+
this.bidi.send(new Command<>("browsingContext.setViewport", params));
338+
}
339+
332340
public void activate() {
333341
this.bidi.send(new Command<>("browsingContext.activate", Map.of(CONTEXT, id)));
334342
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,9 @@ public static BrowsingContextInfo fromJson(JsonInput input) {
134134
return new BrowsingContextInfo(
135135
id, url, children, clientWindow, originalOpener, userContext, parentBrowsingContext);
136136
}
137+
138+
@Override
139+
public String toString() {
140+
return String.format("BrowsingContextInfo(%s %s)", id, url);
141+
}
137142
}

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

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
package org.openqa.selenium.bidi.browsingcontext;
1919

20-
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
21-
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2222
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
2323
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
2424
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;
2525

26+
import java.awt.*;
2627
import java.util.List;
28+
import java.util.regex.Pattern;
2729
import org.junit.jupiter.api.Test;
2830
import org.openqa.selenium.By;
2931
import org.openqa.selenium.JavascriptExecutor;
@@ -40,6 +42,9 @@
4042

4143
class BrowsingContextTest extends JupiterTestBase {
4244

45+
private static final Pattern CONTEXT_NOT_FOUND =
46+
Pattern.compile(".*Browsing Context with id .+ not found.*", Pattern.DOTALL);
47+
4348
@Test
4449
@NeedsFreshDriver
4550
void canCreateABrowsingContextForGivenId() {
@@ -136,9 +141,9 @@ void canGetTreeWithAChild() {
136141

137142
List<BrowsingContextInfo> contextInfoList = parentWindow.getTree();
138143

139-
assertThat(contextInfoList.size()).isEqualTo(1);
144+
assertThat(contextInfoList).hasSize(1);
140145
BrowsingContextInfo info = contextInfoList.get(0);
141-
assertThat(info.getChildren().size()).isEqualTo(1);
146+
assertThat(info.getChildren()).hasSize(1);
142147
assertThat(info.getId()).isEqualTo(referenceContextId);
143148
assertThat(info.getChildren().get(0).getUrl()).contains("formPage.html");
144149
}
@@ -155,7 +160,7 @@ void canGetTreeWithDepth() {
155160

156161
List<BrowsingContextInfo> contextInfoList = parentWindow.getTree(0);
157162

158-
assertThat(contextInfoList.size()).isEqualTo(1);
163+
assertThat(contextInfoList).hasSize(1);
159164
BrowsingContextInfo info = contextInfoList.get(0);
160165
assertThat(info.getChildren()).isNull(); // since depth is 0
161166
assertThat(info.getId()).isEqualTo(referenceContextId);
@@ -176,7 +181,7 @@ void canGetTreeWithRootAndDepth() {
176181

177182
List<BrowsingContextInfo> contextInfoList = parentWindow.getTree(referenceContextId, 1);
178183

179-
assertThat(contextInfoList.size()).isEqualTo(1);
184+
assertThat(contextInfoList).hasSize(1);
180185
BrowsingContextInfo info = contextInfoList.get(0);
181186
assertThat(info.getChildren()).isNotNull(); // since depth is 1
182187
assertThat(info.getId()).isEqualTo(referenceContextId);
@@ -199,7 +204,7 @@ void canGetTreeWithRoot() {
199204

200205
List<BrowsingContextInfo> contextInfoList = parentWindow.getTree(tab.getId());
201206

202-
assertThat(contextInfoList.size()).isEqualTo(1);
207+
assertThat(contextInfoList).hasSize(1);
203208
BrowsingContextInfo info = contextInfoList.get(0);
204209
assertThat(info.getId()).isEqualTo(tab.getId());
205210
assertThat(info.getOriginalOpener()).isNull();
@@ -215,7 +220,7 @@ void canGetAllTopLevelContexts() {
215220

216221
List<BrowsingContextInfo> contextInfoList = window1.getTopLevelContexts();
217222

218-
assertThat(contextInfoList.size()).isEqualTo(2);
223+
assertThat(contextInfoList).hasSize(2);
219224
}
220225

221226
@Test
@@ -226,7 +231,9 @@ void canCloseAWindow() {
226231

227232
window2.close();
228233

229-
assertThatExceptionOfType(BiDiException.class).isThrownBy(window2::getTree);
234+
assertThatThrownBy(window2::getTree)
235+
.isInstanceOf(BiDiException.class)
236+
.hasMessageMatching(CONTEXT_NOT_FOUND);
230237
}
231238

232239
@Test
@@ -237,7 +244,9 @@ void canCloseATab() {
237244

238245
tab2.close();
239246

240-
assertThatExceptionOfType(BiDiException.class).isThrownBy(tab2::getTree);
247+
assertThatThrownBy(tab2::getTree)
248+
.isInstanceOf(BiDiException.class)
249+
.hasMessageMatching(CONTEXT_NOT_FOUND);
241250
}
242251

243252
@Test
@@ -397,7 +406,7 @@ void canCaptureScreenshot() {
397406

398407
String screenshot = browsingContext.captureScreenshot();
399408

400-
assertThat(screenshot.length()).isPositive();
409+
assertThat(screenshot).isNotEmpty();
401410
}
402411

403412
@Test
@@ -423,7 +432,7 @@ void canCaptureScreenshotWithAllParameters() {
423432
.origin(CaptureScreenshotParameters.Origin.DOCUMENT)
424433
.clipRectangle(clipRectangle));
425434

426-
assertThat(screenshot.length()).isPositive();
435+
assertThat(screenshot).isNotEmpty();
427436
}
428437

429438
@Test
@@ -440,7 +449,7 @@ void canCaptureScreenshotOfViewport() {
440449
browsingContext.captureBoxScreenshot(
441450
elementRectangle.getX(), elementRectangle.getY(), 5, 5);
442451

443-
assertThat(screenshot.length()).isPositive();
452+
assertThat(screenshot).isNotEmpty();
444453
}
445454

446455
@Test
@@ -455,24 +464,22 @@ void canCaptureElementScreenshot() {
455464
String screenshot =
456465
browsingContext.captureElementScreenshot(((RemoteWebElement) element).getId());
457466

458-
assertThat(screenshot.length()).isPositive();
467+
assertThat(screenshot).isNotEmpty();
459468
}
460469

461470
@Test
462471
@NeedsFreshDriver
463472
void canSetViewport() {
473+
Dimension initialViewportSize = getViewportSize();
474+
464475
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
465476
driver.get(appServer.whereIs("formPage.html"));
466477

467478
browsingContext.setViewport(250, 300);
479+
assertThat(getViewportSize()).isEqualTo(new Dimension(250, 300));
468480

469-
List<Long> newViewportSize =
470-
(List<Long>)
471-
((JavascriptExecutor) driver)
472-
.executeScript("return [window.innerWidth, window.innerHeight];");
473-
474-
assertThat(newViewportSize.get(0)).isEqualTo(250);
475-
assertThat(newViewportSize.get(1)).isEqualTo(300);
481+
browsingContext.resetViewport();
482+
assertThat(getViewportSize()).isEqualTo(initialViewportSize);
476483
}
477484

478485
@Test
@@ -481,20 +488,10 @@ void canSetViewportWithDevicePixelRatio() {
481488
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
482489
driver.get(appServer.whereIs("formPage.html"));
483490

484-
browsingContext.setViewport(250, 300, 5);
485-
486-
List<Long> newViewportSize =
487-
(List<Long>)
488-
((JavascriptExecutor) driver)
489-
.executeScript("return [window.innerWidth, window.innerHeight];");
490-
491-
assertThat(newViewportSize.get(0)).isEqualTo(250);
492-
assertThat(newViewportSize.get(1)).isEqualTo(300);
493-
494-
Long newDevicePixelRatio =
495-
(Long) ((JavascriptExecutor) driver).executeScript("return window.devicePixelRatio");
491+
browsingContext.setViewport(250, 300, 5.5);
496492

497-
assertThat(newDevicePixelRatio).isEqualTo(5);
493+
assertThat(getViewportSize()).isEqualTo(new Dimension(250, 300));
494+
assertThat(getDevicePixelRatio()).isEqualTo(5.5);
498495
}
499496

500497
@Test
@@ -507,7 +504,7 @@ void canPrintPage() {
507504

508505
String printPage = browsingContext.print(printOptions);
509506

510-
assertThat(printPage.length()).isPositive();
507+
assertThat(printPage).isNotEmpty();
511508
// Comparing expected PDF is a hard problem.
512509
// As long as we are sending the parameters correctly it should be fine.
513510
// Trusting the browsers to do the right thing.
@@ -571,4 +568,16 @@ private String promptPage() {
571568
private boolean getDocumentFocus() {
572569
return (boolean) ((JavascriptExecutor) driver).executeScript("return document.hasFocus();");
573570
}
571+
572+
private Dimension getViewportSize() {
573+
List<Number> dimensions =
574+
(List<Number>)
575+
((JavascriptExecutor) driver)
576+
.executeScript("return [window.innerWidth, window.innerHeight];");
577+
return new Dimension(dimensions.get(0).intValue(), dimensions.get(1).intValue());
578+
}
579+
580+
private Number getDevicePixelRatio() {
581+
return (Number) ((JavascriptExecutor) driver).executeScript("return window.devicePixelRatio");
582+
}
574583
}

0 commit comments

Comments
 (0)