Skip to content

Commit 432f797

Browse files
committed
add BiDi method "BrowsingContext.setViewport(null, null, null)" for resetting mobile emulation mode
1 parent 328267e commit 432f797

File tree

3 files changed

+67
-25
lines changed

3 files changed

+67
-25
lines changed

java/src/org/openqa/selenium/bidi/browsingcontext/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ java_library(
2222
"//java/src/org/openqa/selenium/json",
2323
"//java/src/org/openqa/selenium/remote/http",
2424
artifact("com.google.auto.service:auto-service-annotations"),
25+
"@maven//:org_jspecify_jspecify",
2526
],
2627
)

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

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.function.Function;
27+
28+
import org.jspecify.annotations.NullMarked;
29+
import org.jspecify.annotations.Nullable;
2730
import org.openqa.selenium.WebDriver;
2831
import org.openqa.selenium.WindowType;
2932
import org.openqa.selenium.bidi.BiDi;
@@ -36,6 +39,7 @@
3639
import org.openqa.selenium.json.TypeToken;
3740
import org.openqa.selenium.print.PrintOptions;
3841

42+
@NullMarked
3943
public class BrowsingContext {
4044

4145
private static final Json JSON = new Json();
@@ -91,6 +95,7 @@ public BrowsingContext(WebDriver driver, String id) {
9195

9296
public BrowsingContext(WebDriver driver, WindowType type) {
9397
Require.nonNull("WebDriver", driver);
98+
Require.nonNull("WindowType", type);
9499

95100
if (!(driver instanceof HasBiDi)) {
96101
throw new IllegalArgumentException("WebDriver instance must support BiDi protocol");
@@ -103,6 +108,7 @@ public BrowsingContext(WebDriver driver, WindowType type) {
103108

104109
public BrowsingContext(WebDriver driver, CreateContextParameters parameters) {
105110
Require.nonNull("WebDriver", driver);
111+
Require.nonNull("CreateContextParameters", parameters);
106112

107113
if (!(driver instanceof HasBiDi)) {
108114
throw new IllegalArgumentException("WebDriver instance must support BiDi protocol");
@@ -302,41 +308,69 @@ public String captureElementScreenshot(String elementId, String handle) {
302308
}));
303309
}
304310

305-
public void setViewport(double width, double height) {
306-
Require.positive("Viewport width", width);
307-
Require.positive("Viewport height", height);
311+
public void setViewport(int width, int height) {
312+
setViewport((double) width, (double) height);
313+
}
308314

309-
this.bidi.send(
310-
new Command<>(
311-
"browsingContext.setViewport",
312-
Map.of(CONTEXT, id, "viewport", Map.of("width", width, "height", height))));
315+
public void setViewport(int width, int height, double devicePixelRatio) {
316+
setViewport((double) width, (double) height, devicePixelRatio);
313317
}
314318

315-
public void setViewport(double width, double height, double devicePixelRatio) {
316-
Require.positive("Viewport width", width);
317-
Require.positive("Viewport height", height);
318-
Require.positive("Device pixel ratio.", devicePixelRatio);
319+
/**
320+
* Set viewport size to given width and height (aka "mobile emulation" mode).
321+
* <p/>
322+
* If both {@code width} and {@code height} are null,
323+
* then resets viewport to the initial size (aka "desktop" mode).
324+
*
325+
* @param width null or positive
326+
* @param height null or positive
327+
*/
328+
public void setViewport(@Nullable Double width, @Nullable Double height) {
329+
validate(width, height);
319330

320-
this.bidi.send(
321-
new Command<>(
322-
"browsingContext.setViewport",
323-
Map.of(
324-
CONTEXT,
325-
id,
326-
"viewport",
327-
Map.of("width", width, "height", height),
328-
"devicePixelRatio",
329-
devicePixelRatio)));
331+
Map<String, Object> params = new HashMap<>();
332+
params.put(CONTEXT, id);
333+
params.put("viewport", width == null ? null : Map.of("width", width, "height", height));
334+
this.bidi.send(new Command<>("browsingContext.setViewport", params));
330335
}
331336

332-
public void resetViewport() {
337+
/**
338+
* Set viewport's size and pixel ratio (aka "mobile emulation" mode).
339+
* <p/>
340+
* If both {@code width} and {@code height} are null
341+
* then resets viewport to the initial size (aka "desktop" mode).
342+
* <p/>
343+
* If {@code devicePixelRatio} is null
344+
* then resets DPR to browser’s default DPR (usually 1.0 on desktop).
345+
*
346+
* @param width null or positive
347+
* @param height null or positive
348+
* @param devicePixelRatio null or positive
349+
*/
350+
public void setViewport(@Nullable Double width, @Nullable Double height, @Nullable Double devicePixelRatio) {
351+
validate(width, height);
352+
validate(devicePixelRatio);
353+
333354
Map<String, Object> params = new HashMap<>();
334355
params.put(CONTEXT, id);
335-
params.put("viewport", null);
336-
params.put("devicePixelRatio", null);
356+
params.put("viewport", width == null ? null : Map.of("width", width, "height", height));
357+
params.put("devicePixelRatio", devicePixelRatio);
337358
this.bidi.send(new Command<>("browsingContext.setViewport", params));
338359
}
339360

361+
private void validate(@Nullable Double width, @Nullable Double height) {
362+
if (width != null || height != null) {
363+
Require.positive("Viewport width", width);
364+
Require.positive("Viewport height", height);
365+
}
366+
}
367+
368+
private void validate(@Nullable Double devicePixelRatio) {
369+
if (devicePixelRatio != null) {
370+
Require.positive("Device pixel ratio.", devicePixelRatio);
371+
}
372+
}
373+
340374
public void activate() {
341375
this.bidi.send(new Command<>("browsingContext.activate", Map.of(CONTEXT, id)));
342376
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,20 +478,27 @@ void canSetViewport() {
478478
browsingContext.setViewport(250, 300);
479479
assertThat(getViewportSize()).isEqualTo(new Dimension(250, 300));
480480

481-
browsingContext.resetViewport();
481+
browsingContext.setViewport(null, null);
482482
assertThat(getViewportSize()).isEqualTo(initialViewportSize);
483483
}
484484

485485
@Test
486486
@NeedsFreshDriver
487487
void canSetViewportWithDevicePixelRatio() {
488+
Dimension initialViewportSize = getViewportSize();
489+
double initialPixelRation = getDevicePixelRatio();
490+
488491
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
489492
driver.get(appServer.whereIs("formPage.html"));
490493

491494
browsingContext.setViewport(250, 300, 5.5);
492495

493496
assertThat(getViewportSize()).isEqualTo(new Dimension(250, 300));
494497
assertThat(getDevicePixelRatio()).isEqualTo(5.5);
498+
499+
browsingContext.setViewport(null, null, null);
500+
assertThat(getViewportSize()).isEqualTo(initialViewportSize);
501+
assertThat(getDevicePixelRatio()).isEqualTo(initialPixelRation);
495502
}
496503

497504
@Test

0 commit comments

Comments
 (0)