Skip to content

Commit ef6adb8

Browse files
authored
fix: allow to build options from DeviceDescriptor (#151)
1 parent ec86901 commit ef6adb8

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,10 @@ void writeTo(List<String> output, String offset) {
11021102
}
11031103
} else {
11041104
writeBuilderMethods(output, bodyOffset);
1105+
if ("Browser.newContext.options".equals(jsonPath) ||
1106+
"Browser.newPage.options".equals(jsonPath)) {
1107+
writeDeviceDescriptorBuilder(output, bodyOffset);
1108+
}
11051109
}
11061110
output.add(offset + "}");
11071111
}
@@ -1133,6 +1137,16 @@ private void writeConstructor(List<String> output, String bodyOffset) {
11331137
output.add(bodyOffset + "}");
11341138
}
11351139

1140+
private void writeDeviceDescriptorBuilder(List<String> output, String bodyOffset) {
1141+
output.add(bodyOffset + "public " + name + " withDevice(DeviceDescriptor device) {");
1142+
output.add(bodyOffset + " withViewport(device.viewport().width(), device.viewport().height());");
1143+
output.add(bodyOffset + " withUserAgent(device.userAgent());");
1144+
output.add(bodyOffset + " withDeviceScaleFactor(device.deviceScaleFactor());");
1145+
output.add(bodyOffset + " withIsMobile(device.isMobile());");
1146+
output.add(bodyOffset + " withHasTouch(device.hasTouch());");
1147+
output.add(bodyOffset + " return this;");
1148+
output.add(bodyOffset + "}");
1149+
}
11361150
}
11371151

11381152
class Enum extends TypeDefinition {

playwright/src/main/java/com/microsoft/playwright/Browser.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ public NewContextOptions withStorageState(Path storageStatePath) {
326326
this.storageStatePath = storageStatePath;
327327
return this;
328328
}
329+
public NewContextOptions withDevice(DeviceDescriptor device) {
330+
withViewport(device.viewport().width(), device.viewport().height());
331+
withUserAgent(device.userAgent());
332+
withDeviceScaleFactor(device.deviceScaleFactor());
333+
withIsMobile(device.isMobile());
334+
withHasTouch(device.hasTouch());
335+
return this;
336+
}
329337
}
330338
class NewPageOptions {
331339
public class RecordHar {
@@ -600,6 +608,14 @@ public NewPageOptions withStorageState(Path storageStatePath) {
600608
this.storageStatePath = storageStatePath;
601609
return this;
602610
}
611+
public NewPageOptions withDevice(DeviceDescriptor device) {
612+
withViewport(device.viewport().width(), device.viewport().height());
613+
withUserAgent(device.userAgent());
614+
withDeviceScaleFactor(device.deviceScaleFactor());
615+
withIsMobile(device.isMobile());
616+
withHasTouch(device.hasTouch());
617+
return this;
618+
}
603619
}
604620
/**
605621
* In case this browser is obtained using browserType.launch([options]), closes the browser and all of its pages (if any were

0 commit comments

Comments
 (0)