Skip to content

Commit 97af1c4

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

File tree

3 files changed

+69
-31
lines changed

3 files changed

+69
-31
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,8 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.function.Function;
27+
import org.jspecify.annotations.NullMarked;
28+
import org.jspecify.annotations.Nullable;
2729
import org.openqa.selenium.WebDriver;
2830
import org.openqa.selenium.WindowType;
2931
import org.openqa.selenium.bidi.BiDi;
@@ -36,6 +38,7 @@
3638
import org.openqa.selenium.json.TypeToken;
3739
import org.openqa.selenium.print.PrintOptions;
3840

41+
@NullMarked
3942
public class BrowsingContext {
4043

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

9295
public BrowsingContext(WebDriver driver, WindowType type) {
9396
Require.nonNull("WebDriver", driver);
97+
Require.nonNull("WindowType", type);
9498

9599
if (!(driver instanceof HasBiDi)) {
96100
throw new IllegalArgumentException("WebDriver instance must support BiDi protocol");
@@ -103,6 +107,7 @@ public BrowsingContext(WebDriver driver, WindowType type) {
103107

104108
public BrowsingContext(WebDriver driver, CreateContextParameters parameters) {
105109
Require.nonNull("WebDriver", driver);
110+
Require.nonNull("CreateContextParameters", parameters);
106111

107112
if (!(driver instanceof HasBiDi)) {
108113
throw new IllegalArgumentException("WebDriver instance must support BiDi protocol");
@@ -302,41 +307,70 @@ public String captureElementScreenshot(String elementId, String handle) {
302307
}));
303308
}
304309

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

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

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);
318+
/**
319+
* Set viewport size to given width and height (aka "mobile emulation" mode).
320+
*
321+
* <p>If both {@code width} and {@code height} are null, then resets viewport to the initial size
322+
* (aka "desktop" mode).
323+
*
324+
* @param width null or positive
325+
* @param height null or positive
326+
*/
327+
public void setViewport(@Nullable Double width, @Nullable Double height) {
328+
validate(width, height);
319329

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)));
330+
Map<String, Object> params = new HashMap<>();
331+
params.put(CONTEXT, id);
332+
params.put("viewport", width == null ? null : Map.of("width", width, "height", height));
333+
this.bidi.send(new Command<>("browsingContext.setViewport", params));
330334
}
331335

332-
public void resetViewport() {
336+
/**
337+
* Set viewport's size and pixel ratio (aka "mobile emulation" mode).
338+
*
339+
* <p>If both {@code width} and {@code height} are null then resets viewport to the initial size
340+
* (aka "desktop" mode).
341+
*
342+
* <p>If {@code devicePixelRatio} is null then resets DPR to browser’s default DPR (usually 1.0 on
343+
* desktop).
344+
*
345+
* @param width null or positive
346+
* @param height null or positive
347+
* @param devicePixelRatio null or positive
348+
*/
349+
public void setViewport(
350+
@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: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;
2525

2626
import java.util.List;
27-
import java.util.regex.Pattern;
2827
import org.junit.jupiter.api.Test;
2928
import org.openqa.selenium.By;
3029
import org.openqa.selenium.Dimension;
@@ -42,9 +41,6 @@
4241

4342
class BrowsingContextTest extends JupiterTestBase {
4443

45-
private static final Pattern CONTEXT_NOT_FOUND =
46-
Pattern.compile(".*Browsing Context with id .+ not found.*", Pattern.DOTALL);
47-
4844
@Test
4945
@NeedsFreshDriver
5046
void canCreateABrowsingContextForGivenId() {
@@ -233,7 +229,7 @@ void canCloseAWindow() {
233229

234230
assertThatThrownBy(window2::getTree)
235231
.isInstanceOf(BiDiException.class)
236-
.hasMessageMatching(CONTEXT_NOT_FOUND);
232+
.hasMessageContaining("not found");
237233
}
238234

239235
@Test
@@ -246,7 +242,7 @@ void canCloseATab() {
246242

247243
assertThatThrownBy(tab2::getTree)
248244
.isInstanceOf(BiDiException.class)
249-
.hasMessageMatching(CONTEXT_NOT_FOUND);
245+
.hasMessageContaining("not found");
250246
}
251247

252248
@Test
@@ -478,20 +474,27 @@ void canSetViewport() {
478474
browsingContext.setViewport(250, 300);
479475
assertThat(getViewportSize()).isEqualTo(new Dimension(250, 300));
480476

481-
browsingContext.resetViewport();
477+
browsingContext.setViewport(null, null);
482478
assertThat(getViewportSize()).isEqualTo(initialViewportSize);
483479
}
484480

485481
@Test
486482
@NeedsFreshDriver
487483
void canSetViewportWithDevicePixelRatio() {
484+
Dimension initialViewportSize = getViewportSize();
485+
double initialPixelRation = getDevicePixelRatio();
486+
488487
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
489488
driver.get(appServer.whereIs("formPage.html"));
490489

491490
browsingContext.setViewport(250, 300, 5.5);
492491

493492
assertThat(getViewportSize()).isEqualTo(new Dimension(250, 300));
494493
assertThat(getDevicePixelRatio()).isEqualTo(5.5);
494+
495+
browsingContext.setViewport(null, null, null);
496+
assertThat(getViewportSize()).isEqualTo(initialViewportSize);
497+
assertThat(getDevicePixelRatio()).isEqualTo(initialPixelRation);
495498
}
496499

497500
@Test

0 commit comments

Comments
 (0)