diff --git a/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java b/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java index 4a39ef93..caa0ba72 100644 --- a/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java +++ b/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java @@ -1102,6 +1102,10 @@ class NestedClass extends TypeDefinition { } } else { writeBuilderMethods(output, bodyOffset); + if ("Browser.newContext.options".equals(jsonPath) || + "Browser.newPage.options".equals(jsonPath)) { + writeDeviceDescriptorBuilder(output, bodyOffset); + } } output.add(offset + "}"); } @@ -1133,6 +1137,16 @@ class NestedClass extends TypeDefinition { output.add(bodyOffset + "}"); } + private void writeDeviceDescriptorBuilder(List output, String bodyOffset) { + output.add(bodyOffset + "public " + name + " withDevice(DeviceDescriptor device) {"); + output.add(bodyOffset + " withViewport(device.viewport().width(), device.viewport().height());"); + output.add(bodyOffset + " withUserAgent(device.userAgent());"); + output.add(bodyOffset + " withDeviceScaleFactor(device.deviceScaleFactor());"); + output.add(bodyOffset + " withIsMobile(device.isMobile());"); + output.add(bodyOffset + " withHasTouch(device.hasTouch());"); + output.add(bodyOffset + " return this;"); + output.add(bodyOffset + "}"); + } } class Enum extends TypeDefinition { diff --git a/playwright/src/main/java/com/microsoft/playwright/Browser.java b/playwright/src/main/java/com/microsoft/playwright/Browser.java index 63a52d48..e9e1023b 100644 --- a/playwright/src/main/java/com/microsoft/playwright/Browser.java +++ b/playwright/src/main/java/com/microsoft/playwright/Browser.java @@ -326,6 +326,14 @@ public interface Browser { this.storageStatePath = storageStatePath; return this; } + public NewContextOptions withDevice(DeviceDescriptor device) { + withViewport(device.viewport().width(), device.viewport().height()); + withUserAgent(device.userAgent()); + withDeviceScaleFactor(device.deviceScaleFactor()); + withIsMobile(device.isMobile()); + withHasTouch(device.hasTouch()); + return this; + } } class NewPageOptions { public class RecordHar { @@ -600,6 +608,14 @@ public interface Browser { this.storageStatePath = storageStatePath; return this; } + public NewPageOptions withDevice(DeviceDescriptor device) { + withViewport(device.viewport().width(), device.viewport().height()); + withUserAgent(device.userAgent()); + withDeviceScaleFactor(device.deviceScaleFactor()); + withIsMobile(device.isMobile()); + withHasTouch(device.hasTouch()); + return this; + } } /** * In case this browser is obtained using browserType.launch([options]), closes the browser and all of its pages (if any were