Skip to content

Added test fixture for Browser.NewContextOptions #1788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.microsoft.playwright.impl.junit;

import com.microsoft.playwright.*;
import com.microsoft.playwright.impl.Utils;
import com.microsoft.playwright.junit.Options;
import org.junit.jupiter.api.extension.*;

Expand All @@ -26,6 +25,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.microsoft.playwright.impl.junit.BrowserNewContextOptionsExtension.getBrowserContextOptions;
import static com.microsoft.playwright.impl.junit.ExtensionUtils.*;
import static com.microsoft.playwright.impl.junit.PageExtension.cleanUpPage;

Expand Down Expand Up @@ -59,7 +59,7 @@ public static BrowserContext getOrCreateBrowserContext(ExtensionContext extensio
Playwright playwright = PlaywrightExtension.getOrCreatePlaywright(extensionContext);
setTestIdAttribute(playwright, options);
Browser browser = BrowserExtension.getOrCreateBrowser(extensionContext);
Browser.NewContextOptions contextOptions = getContextOptions(playwright, options);
Browser.NewContextOptions contextOptions = getBrowserContextOptions(playwright, options);
browserContext = browser.newContext(contextOptions);
if (shouldRecordTrace(options)) {
Tracing.StartOptions startOptions = new Tracing.StartOptions().setSnapshots(true).setScreenshots(true).setTitle(extensionContext.getDisplayName());
Expand Down Expand Up @@ -152,34 +152,5 @@ private static boolean shouldRecordTrace(Options options) {
return options.trace.equals(Options.Trace.ON) || options.trace.equals(Options.Trace.RETAIN_ON_FAILURE);
}

private static Browser.NewContextOptions getContextOptions(Playwright playwright, Options options) {
Browser.NewContextOptions contextOptions = Utils.clone(options.contextOptions);
if (contextOptions == null) {
contextOptions = new Browser.NewContextOptions();
}

if (options.baseUrl != null) {
contextOptions.setBaseURL(options.baseUrl);
}

if (options.deviceName != null) {
DeviceDescriptor deviceDescriptor = DeviceDescriptor.findByName(playwright, options.deviceName);
if (deviceDescriptor == null) {
throw new PlaywrightException("Unknown device name: " + options.deviceName);
}
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.ignoreHTTPSErrors != null) {
contextOptions.setIgnoreHTTPSErrors(options.ignoreHTTPSErrors);
}

return contextOptions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.microsoft.playwright.impl.junit;

import com.microsoft.playwright.Browser;
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.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;

import static com.microsoft.playwright.impl.junit.ExtensionUtils.isParameterSupported;

public class BrowserNewContextOptionsExtension implements ParameterResolver {
private static final ThreadLocal<Browser.NewContextOptions> threadLocalBrowserNewContextOptions = new ThreadLocal<>();

@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return isParameterSupported(parameterContext, extensionContext, Browser.NewContextOptions.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should support this for Browser.NewPageOptions too as browser.newPage() is essentially a shortcut for newContext followed by newPage.

}

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Options options = OptionsExtension.getOptions(extensionContext);
Playwright playwright = PlaywrightExtension.getOrCreatePlaywright(extensionContext);
return getBrowserContextOptions(playwright, options);
}

static Browser.NewContextOptions getBrowserContextOptions(Playwright playwright, Options options) {
Browser.NewContextOptions contextOptions = threadLocalBrowserNewContextOptions.get();
if (contextOptions != null) {
return contextOptions;
}

contextOptions = Utils.clone(options.contextOptions);

if (contextOptions == null) {
contextOptions = new Browser.NewContextOptions();
}

if (options.baseUrl != null) {
contextOptions.setBaseURL(options.baseUrl);
}

if (options.deviceName != null) {
DeviceDescriptor deviceDescriptor = DeviceDescriptor.findByName(playwright, options.deviceName);
if (deviceDescriptor == null) {
throw new PlaywrightException("Unknown device name: " + options.deviceName);
}
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.ignoreHTTPSErrors != null) {
contextOptions.setIgnoreHTTPSErrors(options.ignoreHTTPSErrors);
}

threadLocalBrowserNewContextOptions.set(contextOptions);
return contextOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
* <a href="https://playwright.dev/java/docs/junit">JUnit guide</a>.
*/
@ExtendWith({OptionsExtension.class, PlaywrightExtension.class, BrowserExtension.class, BrowserContextExtension.class,
PageExtension.class, APIRequestContextExtension.class})
PageExtension.class, APIRequestContextExtension.class, BrowserNewContextOptionsExtension.class})
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public Options getOptions() {
}
}

@Test
public void newBrowserContextOptionsShouldBeConfiguredFromOptionsFactory(Browser.NewContextOptions newContextOptions) {
assertEquals(newContextOptions.baseURL, serverMap.get(TestFixtureOptions.class).EMPTY_PAGE);
}

@Test
public void testCustomBrowser(Browser browser) {
assertEquals(browser.browserType().name(), "webkit");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,26 @@ public class TestFixtures {
private Page pageFromBeforeEach;
private static APIRequestContext apiRequestContextFromBeforeAll;
private APIRequestContext apiRequestContextFromBeforeEach;
private static Browser.NewContextOptions newBrowserContextOptionsFromBeforeAll;

@BeforeAll
public static void beforeAll(Playwright playwright, Browser browser, APIRequestContext apiRequestContext) {
public static void beforeAll(Playwright playwright, Browser browser, APIRequestContext apiRequestContext, Browser.NewContextOptions newBrowserContextOptions) {
assertNotNull(playwright);
assertNotNull(browser);
assertNotNull(apiRequestContext);
assertNotNull(newBrowserContextOptions);

playwrightFromBeforeAll = playwright;
browserFromBeforeAll = browser;
apiRequestContextFromBeforeAll = apiRequestContext;
newBrowserContextOptionsFromBeforeAll = newBrowserContextOptions;
}

@BeforeEach
public void beforeEach(Playwright playwright, Browser browser, BrowserContext browserContext, Page page, APIRequestContext apiRequestContext) {
public void beforeEach(Playwright playwright, Browser browser, BrowserContext browserContext, Page page, APIRequestContext apiRequestContext, Browser.NewContextOptions newBrowserContextOptions) {
assertEquals(playwrightFromBeforeAll, playwright);
assertEquals(browserFromBeforeAll, browser);
assertEquals(newBrowserContextOptionsFromBeforeAll, newBrowserContextOptions);
assertNotEquals(apiRequestContextFromBeforeAll, apiRequestContext);

assertNotNull(browserContext);
Expand All @@ -57,9 +61,10 @@ public void beforeEach(Playwright playwright, Browser browser, BrowserContext br
}

@Test
public void objectShouldBeSameAsBeforeAll(Playwright playwright, Browser browser) {
public void objectShouldBeSameAsBeforeAll(Playwright playwright, Browser browser, Browser.NewContextOptions browserNextContextOptions) {
assertEquals(playwrightFromBeforeAll, playwright);
assertEquals(browserFromBeforeAll, browser);
assertEquals(newBrowserContextOptionsFromBeforeAll, browserNextContextOptions);
}

@Test
Expand Down
Loading