fix: allow to build options from DeviceDescriptor (#151)

This commit is contained in:
Yury Semikhatsky 2020-12-18 13:45:02 -08:00 committed by GitHub
parent ec86901e97
commit ef6adb8123
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -1102,6 +1102,10 @@ class NestedClass extends TypeDefinition {
} }
} else { } else {
writeBuilderMethods(output, bodyOffset); writeBuilderMethods(output, bodyOffset);
if ("Browser.newContext.options".equals(jsonPath) ||
"Browser.newPage.options".equals(jsonPath)) {
writeDeviceDescriptorBuilder(output, bodyOffset);
}
} }
output.add(offset + "}"); output.add(offset + "}");
} }
@ -1133,6 +1137,16 @@ class NestedClass extends TypeDefinition {
output.add(bodyOffset + "}"); output.add(bodyOffset + "}");
} }
private void writeDeviceDescriptorBuilder(List<String> 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 { class Enum extends TypeDefinition {

View File

@ -326,6 +326,14 @@ public interface Browser {
this.storageStatePath = storageStatePath; this.storageStatePath = storageStatePath;
return this; 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 { class NewPageOptions {
public class RecordHar { public class RecordHar {
@ -600,6 +608,14 @@ public interface Browser {
this.storageStatePath = storageStatePath; this.storageStatePath = storageStatePath;
return this; 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 * In case this browser is obtained using browserType.launch([options]), closes the browser and all of its pages (if any were