1
0
mirror of synced 2026-05-22 18:53:15 +00:00

feat: support device name in playwright fixtures (#1472)

Reference https://github.com/microsoft/playwright-java/issues/1369
Reference https://github.com/microsoft/playwright-java/issues/939
This commit is contained in:
Yury Semikhatsky
2024-02-04 19:07:24 -08:00
committed by GitHub
parent 197ee801ad
commit 7ce6c7f0a0
7 changed files with 130 additions and 15 deletions
@@ -29,6 +29,10 @@ class LocalUtils extends ChannelOwner {
super(parent, type, guid, initializer);
}
JsonArray deviceDescriptors() {
return initializer.getAsJsonArray("deviceDescriptors");
}
void zip(Path zipFile, JsonArray entries, String stacksId, boolean appendMode, boolean includeSources) {
JsonObject params = new JsonObject();
params.addProperty("zipFile", zipFile.toString());
@@ -16,6 +16,7 @@
package com.microsoft.playwright.impl;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.microsoft.playwright.APIRequest;
import com.microsoft.playwright.Playwright;
@@ -89,6 +90,10 @@ public class PlaywrightImpl extends ChannelOwner implements Playwright {
sharedSelectors.removeChannel(selectors);
}
public JsonArray deviceDescriptors() {
return connection.localUtils.deviceDescriptors();
}
@Override
public BrowserTypeImpl chromium() {
return chromium;
@@ -14,7 +14,8 @@ public class Options {
public ViewportSize viewportSize;
public String channel;
public Boolean headless;
public String browserName = "chromium";
public String browserName;
public String deviceName;
public BrowserType.LaunchOptions launchOptions;
public Browser.NewContextOptions contextOption;
public APIRequest.NewContextOptions apiRequestOptions;
@@ -83,6 +84,15 @@ public class Options {
return this;
}
public String getDeviceName() {
return deviceName;
}
public Options setDeviceName(String deviceName) {
this.deviceName = deviceName;
return this;
}
public String getChannel() {
return channel;
}
@@ -2,6 +2,8 @@ package com.microsoft.playwright.junit.impl;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.PlaywrightException;
import com.microsoft.playwright.impl.Utils;
import com.microsoft.playwright.junit.Options;
import org.junit.jupiter.api.extension.*;
@@ -38,14 +40,15 @@ public class BrowserContextExtension implements ParameterResolver, AfterEachCall
}
Options options = OptionsExtension.getOptions(extensionContext);
Playwright playwright = PlaywrightExtension.getOrCreatePlaywright(extensionContext);
Browser browser = BrowserExtension.getOrCreateBrowser(extensionContext);
Browser.NewContextOptions contextOptions = getContextOptions(options);
Browser.NewContextOptions contextOptions = getContextOptions(playwright, options);
browserContext = browser.newContext(contextOptions);
threadLocalBrowserContext.set(browserContext);
return browserContext;
}
private static Browser.NewContextOptions getContextOptions(Options options) {
private static Browser.NewContextOptions getContextOptions(Playwright playwright, Options options) {
Browser.NewContextOptions contextOptions = Utils.clone(options.getContextOption());
if (contextOptions == null) {
contextOptions = new Browser.NewContextOptions();
@@ -59,6 +62,20 @@ public class BrowserContextExtension implements ParameterResolver, AfterEachCall
contextOptions.setStorageStatePath(options.getStorageStatePath());
}
if (options.getDeviceName() != null) {
DeviceDescriptor deviceDescriptor = DeviceDescriptor.findByName(playwright, options.getDeviceName());
if (deviceDescriptor == null) {
throw new PlaywrightException("Unknown device name: " + options.getDeviceName());
}
contextOptions.userAgent = deviceDescriptor.userAgent;
if (deviceDescriptor.viewport != null) {
contextOptions.setViewportSize(deviceDescriptor.viewport.width, deviceDescriptor.viewport.height);
}
contextOptions.deviceScaleFactor = deviceDescriptor.deviceScaleFactor;
contextOptions.isMobile = deviceDescriptor.isMobile;
contextOptions.hasTouch = deviceDescriptor.hasTouch;
}
if (options.getViewportSize() != null) {
contextOptions.setViewportSize(options.getViewportSize());
}
@@ -42,24 +42,34 @@ public class BrowserExtension implements ParameterResolver, AfterAllCallback {
Playwright playwright = PlaywrightExtension.getOrCreatePlaywright(extensionContext);
BrowserType.LaunchOptions launchOptions = getLaunchOptions(options);
switch (options.getBrowserName()) {
case "webkit":
browser = playwright.webkit().launch(launchOptions);
break;
case "firefox":
browser = playwright.firefox().launch(launchOptions);
break;
case "chromium":
browser = playwright.chromium().launch(launchOptions);
break;
default:
throw new PlaywrightException("Invalid browser name.");
BrowserType browserType = playwright.chromium();
if (options.getBrowserName() != null) {
browserType = getBrowserTypeForName(playwright, options.getBrowserName());
} else if (options.deviceName != null) {
DeviceDescriptor deviceDescriptor = DeviceDescriptor.findByName(playwright, options.deviceName);
if (deviceDescriptor != null && deviceDescriptor.defaultBrowserType != null) {
browserType = getBrowserTypeForName(playwright, deviceDescriptor.defaultBrowserType);
}
}
browser = browserType.launch(launchOptions);
threadLocalBrowser.set(browser);
return browser;
}
private static BrowserType getBrowserTypeForName(Playwright playwright, String name) {
switch (name) {
case "webkit":
return playwright.webkit();
case "firefox":
return playwright.firefox();
case "chromium":
return playwright.chromium();
default:
throw new PlaywrightException("Invalid browser name.");
}
}
private static BrowserType.LaunchOptions getLaunchOptions(Options options) {
BrowserType.LaunchOptions launchOptions = Utils.clone(options.getLaunchOptions());
if (launchOptions == null) {
@@ -0,0 +1,34 @@
package com.microsoft.playwright.junit.impl;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.impl.PlaywrightImpl;
import com.microsoft.playwright.options.ViewportSize;
class DeviceDescriptor {
public String userAgent;
public ViewportSize viewport;
public Double deviceScaleFactor;
public Boolean isMobile;
public Boolean hasTouch;
public String defaultBrowserType;
static DeviceDescriptor findByName(Playwright playwright, String name) {
JsonArray devices = ((PlaywrightImpl) playwright).deviceDescriptors();
JsonObject descriptor = null;
for (JsonElement item : devices) {
if (name.equals(item.getAsJsonObject().get("name").getAsString())) {
descriptor = item.getAsJsonObject().getAsJsonObject("descriptor");
break;
}
}
if (descriptor == null) {
return null;
}
return new Gson().fromJson(descriptor, DeviceDescriptor.class);
}
}
@@ -0,0 +1,35 @@
package com.microsoft.playwright;
import com.microsoft.playwright.junit.Options;
import com.microsoft.playwright.junit.OptionsFactory;
import com.microsoft.playwright.junit.UsePlaywright;
import org.junit.jupiter.api.Test;
import static com.microsoft.playwright.ServerLifecycle.serverMap;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
@FixtureTest
@UsePlaywright(TestPlaywrightDeviceOption.CustomOptions.class)
public class TestPlaywrightDeviceOption {
public static class CustomOptions implements OptionsFactory {
@Override
public Options getOptions() {
return new Options().setDeviceName("iPhone 14");
}
}
private Server server() {
return serverMap.get(this.getClass());
}
@Test
public void testPredifinedDeviceParameters(Page page) {
page.navigate(server().EMPTY_PAGE);
assertEquals("webkit", page.context().browser().browserType().name());
assertEquals(3, page.evaluate("window.devicePixelRatio"));
assertEquals(980, page.evaluate("window.innerWidth"));
assertEquals(1668, page.evaluate("window.innerHeight"));
}
}