1
0
mirror of synced 2026-09-11 00:49:39 +00:00

fix: remove devices from the API (#280)

This commit is contained in:
Yury Semikhatsky
2021-02-11 12:53:18 -08:00
committed by GitHub
parent 8db59ba9a1
commit 0afb42bb0f
10 changed files with 3 additions and 167 deletions
@@ -237,14 +237,6 @@ public interface Browser extends AutoCloseable {
this.viewportSize = Optional.ofNullable(viewportSize);
return this;
}
public NewContextOptions withDevice(DeviceDescriptor device) {
withViewportSize(device.viewportSize());
withUserAgent(device.userAgent());
withDeviceScaleFactor(device.deviceScaleFactor());
withIsMobile(device.isMobile());
withHasTouch(device.hasTouch());
return this;
}
}
class NewPageOptions {
/**
@@ -452,14 +444,6 @@ public interface Browser extends AutoCloseable {
this.viewportSize = Optional.ofNullable(viewportSize);
return this;
}
public NewPageOptions withDevice(DeviceDescriptor device) {
withViewportSize(device.viewportSize());
withUserAgent(device.userAgent());
withDeviceScaleFactor(device.deviceScaleFactor());
withIsMobile(device.isMobile());
withHasTouch(device.hasTouch());
return this;
}
}
/**
* In case this browser is obtained using [{@code method: BrowserType.launch}], closes the browser and all of its pages (if any
@@ -478,14 +478,6 @@ public interface BrowserType {
this.viewportSize = Optional.ofNullable(viewportSize);
return this;
}
public LaunchPersistentContextOptions withDevice(DeviceDescriptor device) {
withViewportSize(device.viewportSize());
withUserAgent(device.userAgent());
withDeviceScaleFactor(device.deviceScaleFactor());
withIsMobile(device.isMobile());
withHasTouch(device.hasTouch());
return this;
}
}
/**
* A path where Playwright expects to find a bundled browser executable.
@@ -1,28 +0,0 @@
/*
* Copyright (c) Microsoft Corporation.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright;
import com.microsoft.playwright.options.*;
public interface DeviceDescriptor {
ViewportSize viewportSize();
String userAgent();
double deviceScaleFactor();
boolean isMobile();
boolean hasTouch();
BrowserType defaultBrowserType();
}
@@ -28,10 +28,6 @@ public interface Playwright extends AutoCloseable {
* This object can be used to launch or connect to Chromium, returning instances of {@code ChromiumBrowser}.
*/
BrowserType chromium();
/**
* Returns a dictionary of devices to be used with [{@code method: Browser.newContext}] or [{@code method: Browser.newPage}].
*/
Map<String, DeviceDescriptor> devices();
/**
* This object can be used to launch or connect to Firefox, returning instances of {@code FirefoxBrowser}.
*/
@@ -1,67 +0,0 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright.impl;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.DeviceDescriptor;
import com.microsoft.playwright.PlaywrightException;
import com.microsoft.playwright.options.ViewportSize;
class DeviceDescriptorImpl implements DeviceDescriptor {
PlaywrightImpl playwright;
private ViewportSize viewport;
private String userAgent;
private double deviceScaleFactor;
private boolean isMobile;
private boolean hasTouch;
private String defaultBrowserType;
@Override
public ViewportSize viewportSize() {
return viewport;
}
@Override
public String userAgent() {
return userAgent;
}
@Override
public double deviceScaleFactor() {
return deviceScaleFactor;
}
@Override
public boolean isMobile() {
return isMobile;
}
@Override
public boolean hasTouch() {
return hasTouch;
}
@Override
public BrowserType defaultBrowserType() {
switch (defaultBrowserType) {
case "chromium": return playwright.chromium();
case "firefox": return playwright.firefox();
case "webkit": return playwright.webkit();
default: throw new PlaywrightException("Unknown type: " + defaultBrowserType);
}
}
}
@@ -16,18 +16,13 @@
package com.microsoft.playwright.impl;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.DeviceDescriptor;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.PlaywrightException;
import com.microsoft.playwright.Selectors;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class PlaywrightImpl extends ChannelOwner implements Playwright {
@@ -53,7 +48,6 @@ public class PlaywrightImpl extends ChannelOwner implements Playwright {
private final BrowserTypeImpl firefox;
private final BrowserTypeImpl webkit;
private final Selectors selectors;
private final Map<String, DeviceDescriptor> devices = new HashMap<>();
PlaywrightImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
@@ -61,15 +55,6 @@ public class PlaywrightImpl extends ChannelOwner implements Playwright {
firefox = parent.connection.getExistingObject(initializer.getAsJsonObject("firefox").get("guid").getAsString());
webkit = parent.connection.getExistingObject(initializer.getAsJsonObject("webkit").get("guid").getAsString());
selectors = parent.connection.getExistingObject(initializer.getAsJsonObject("selectors").get("guid").getAsString());
Gson gson = Serialization.gson();
for (JsonElement item : initializer.getAsJsonArray("deviceDescriptors")) {
JsonObject o = item.getAsJsonObject();
String name = o.get("name").getAsString();
DeviceDescriptorImpl descriptor = gson.fromJson(o.get("descriptor"), DeviceDescriptorImpl.class);
descriptor.playwright = this;
devices.put(name, descriptor);
}
}
@Override
@@ -87,11 +72,6 @@ public class PlaywrightImpl extends ChannelOwner implements Playwright {
return webkit;
}
@Override
public Map<String, DeviceDescriptor> devices() {
return devices;
}
@Override
public Selectors selectors() {
return selectors;
@@ -240,10 +240,9 @@ public class TestClick extends TestBase {
@Test
void shouldNotHangWithTouchEnabledViewports() {
// @see https://github.com/GoogleChrome/puppeteer/issues/161
DeviceDescriptor descriptor = playwright.devices().get("iPhone 6");
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.withViewportSize(descriptor.viewportSize())
.withHasTouch(descriptor.hasTouch()));
.withViewportSize(375, 667)
.withHasTouch(true));
Page page = context.newPage();
page.mouse().down();
page.mouse().move(100, 10);
+1 -1
View File
@@ -1 +1 @@
1.9.0-next-1613002324000
1.9.0-next-1613075170000
@@ -1056,11 +1056,6 @@ class NestedClass extends TypeDefinition {
if (!isReturnType) {
writeConstructor(output, bodyOffset);
writeBuilderMethods(output, bodyOffset);
if (asList("Browser.newContext.options",
"Browser.newPage.options",
"BrowserType.launchPersistentContext.options").contains(jsonPath)) {
writeDeviceDescriptorBuilder(output, bodyOffset);
}
}
output.add(offset + "}");
}
@@ -1083,17 +1078,6 @@ class NestedClass extends TypeDefinition {
requiredFields.forEach(f -> output.add(bodyOffset + " this." + f.name + " = " + f.name + ";"));
output.add(bodyOffset + "}");
}
private void writeDeviceDescriptorBuilder(List<String> output, String bodyOffset) {
output.add(bodyOffset + "public " + name + " withDevice(DeviceDescriptor device) {");
output.add(bodyOffset + " withViewportSize(device.viewportSize());");
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 {
@@ -56,7 +56,6 @@ class Types {
add("Selectors.register.script", "Object|function|string", "String");
// The method has custom signatures
add("BrowserContext.cookies", "Array<Object>", "Cookie");
add("BrowserContext.route.url", "RegExp|function(URL):boolean|string", "String");
add("BrowserContext.unroute.url", "RegExp|function(URL):boolean|string", "String");
add("Page.waitForNavigation.options.url", "RegExp|function(URL):boolean|string", "Custom");
@@ -75,9 +74,6 @@ class Types {
add("ElementHandle.setInputFiles.files", "Array<Object>|Array<path>|Object|path", "String");
add("FileChooser.setFiles.files", "Array<Object>|Array<path>|Object|path", "String");
add("Route.resume.options.postData", "Buffer|string", "byte[]", new Empty());
// TODO: fix upstream types!
add("Playwright.devices", "Object", "Map<String, DeviceDescriptor>", new Empty());
}
Mapping findForPath(String jsonPath) {