Skip to content

Commit

Permalink
feat: support device name in playwright fixtures
Browse files Browse the repository at this point in the history
Reference microsoft#1369
Reference microsoft#939
  • Loading branch information
yury-s committed Feb 3, 2024
1 parent 197ee80 commit eaa501a
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,6 +90,10 @@ void unregisterSelectors() {
sharedSelectors.removeChannel(selectors);
}

public JsonArray deviceDescriptors() {
return connection.localUtils.deviceDescriptors();
}

@Override
public BrowserTypeImpl chromium() {
return chromium;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 device;
public BrowserType.LaunchOptions launchOptions;
public Browser.NewContextOptions contextOption;
public APIRequest.NewContextOptions apiRequestOptions;
Expand Down Expand Up @@ -83,6 +84,15 @@ public Options setBrowserName(String browserName) {
return this;
}

public String getDevice() {
return device;
}

public Options setDevice(String device) {
this.device = device;
return this;
}

public String getChannel() {
return channel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

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 com.microsoft.playwright.options.ViewportSize;
import org.junit.jupiter.api.extension.*;

import static com.microsoft.playwright.junit.impl.ExtensionUtils.isClassHook;
Expand Down Expand Up @@ -38,14 +41,15 @@ static BrowserContext getOrCreateBrowserContext(ExtensionContext extensionContex
}

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();
Expand All @@ -59,6 +63,20 @@ private static Browser.NewContextOptions getContextOptions(Options options) {
contextOptions.setStorageStatePath(options.getStorageStatePath());
}

if (options.getDevice() != null) {
DeviceDescriptor deviceDescriptor = DeviceDescriptor.findByName(playwright, options.getDevice());
if (deviceDescriptor == null) {
throw new PlaywrightException("Unknown device name: " + options.getDevice());
}
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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.microsoft.playwright.junit.impl;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.PlaywrightException;
import com.microsoft.playwright.impl.PlaywrightImpl;
import com.microsoft.playwright.impl.Utils;
import com.microsoft.playwright.junit.Options;
import org.junit.jupiter.api.extension.*;
Expand Down Expand Up @@ -42,22 +46,32 @@ static Browser getOrCreateBrowser(ExtensionContext extensionContext) {
Playwright playwright = PlaywrightExtension.getOrCreatePlaywright(extensionContext);
BrowserType.LaunchOptions launchOptions = getLaunchOptions(options);

switch (options.getBrowserName()) {
BrowserType browserType = playwright.chromium();
if (options.getBrowserName() != null) {
browserType = getBrowserTypeForName(playwright, options.getBrowserName());
} else if (options.device != null) {
DeviceDescriptor deviceDescriptor = DeviceDescriptor.findByName(playwright, options.device);
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":
browser = playwright.webkit().launch(launchOptions);
break;
return playwright.webkit();
case "firefox":
browser = playwright.firefox().launch(launchOptions);
break;
return playwright.firefox();
case "chromium":
browser = playwright.chromium().launch(launchOptions);
break;
return playwright.chromium();
default:
throw new PlaywrightException("Invalid browser name.");
}

threadLocalBrowser.set(browser);
return browser;
}

private static BrowserType.LaunchOptions getLaunchOptions(Options options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 java.util.regex.Pattern;

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().setDevice("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"));
}
}

0 comments on commit eaa501a

Please sign in to comment.