2424import java .util .List ;
2525import java .util .Map ;
2626import java .util .function .Function ;
27+
28+ import org .jspecify .annotations .NullMarked ;
29+ import org .jspecify .annotations .Nullable ;
2730import org .openqa .selenium .WebDriver ;
2831import org .openqa .selenium .WindowType ;
2932import org .openqa .selenium .bidi .BiDi ;
3639import org .openqa .selenium .json .TypeToken ;
3740import org .openqa .selenium .print .PrintOptions ;
3841
42+ @ NullMarked
3943public 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 }
0 commit comments