diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/junit/BrowserContextExtension.java b/playwright/src/main/java/com/microsoft/playwright/impl/junit/BrowserContextExtension.java index e27b1c448..8e4678c56 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/junit/BrowserContextExtension.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/junit/BrowserContextExtension.java @@ -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.*; @@ -59,7 +58,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 = BrowserContextOptionsExtension.getContextOptions(playwright, options); browserContext = browser.newContext(contextOptions); if (options.defaultTimeout != null) { browserContext.setDefaultTimeout(options.defaultTimeout); @@ -154,35 +153,4 @@ private void closeBrowserContext() { 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; - } } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/junit/BrowserContextOptionsExtension.java b/playwright/src/main/java/com/microsoft/playwright/impl/junit/BrowserContextOptionsExtension.java new file mode 100644 index 000000000..f3d9463ef --- /dev/null +++ b/playwright/src/main/java/com/microsoft/playwright/impl/junit/BrowserContextOptionsExtension.java @@ -0,0 +1,77 @@ +/* + * 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.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.*; + +import static com.microsoft.playwright.impl.junit.ExtensionUtils.isParameterSupported; + +public class BrowserContextOptionsExtension implements ParameterResolver { + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { + return isParameterSupported(parameterContext, extensionContext, Browser.NewContextOptions.class); + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { + Options options = OptionsExtension.getOptions(extensionContext); + Playwright playwright = PlaywrightExtension.getOrCreatePlaywright(extensionContext); + return getContextOptions(playwright, options); + } + + // Returns a new instance every time, so that callers can modify it without affecting other tests. + 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); + } + if (options.browserName != null && deviceDescriptor.defaultBrowserType != null && + !options.browserName.equals(deviceDescriptor.defaultBrowserType)) { + throw new PlaywrightException("Device \"" + options.deviceName + "\" requires \"" + deviceDescriptor.defaultBrowserType + + "\" browser, but browserName is \"" + options.browserName + "\""); + } + 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; + } +} diff --git a/playwright/src/main/java/com/microsoft/playwright/junit/UsePlaywright.java b/playwright/src/main/java/com/microsoft/playwright/junit/UsePlaywright.java index 0d0497289..6f81c3fa4 100644 --- a/playwright/src/main/java/com/microsoft/playwright/junit/UsePlaywright.java +++ b/playwright/src/main/java/com/microsoft/playwright/junit/UsePlaywright.java @@ -36,10 +36,22 @@ *
{@code
+ * @BeforeAll
+ * static void beforeAll(Browser browser, Browser.NewContextOptions contextOptions) {
+ * BrowserContext context = browser.newContext(contextOptions);
+ * // ...
+ * context.close();
+ * }
+ * }
*
* An example of using {@code @UsePlaywright} annotation: *
{@code
@@ -73,7 +85,7 @@
* JUnit guide.
*/
@ExtendWith({OptionsExtension.class, PlaywrightExtension.class, BrowserExtension.class, BrowserContextExtension.class,
- PageExtension.class, APIRequestContextExtension.class})
+ PageExtension.class, APIRequestContextExtension.class, BrowserContextOptionsExtension.class})
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
diff --git a/playwright/src/test/java/com/microsoft/playwright/impl/junit/TestBrowserContextOptionsExtension.java b/playwright/src/test/java/com/microsoft/playwright/impl/junit/TestBrowserContextOptionsExtension.java
new file mode 100644
index 000000000..0365d4625
--- /dev/null
+++ b/playwright/src/test/java/com/microsoft/playwright/impl/junit/TestBrowserContextOptionsExtension.java
@@ -0,0 +1,46 @@
+/*
+ * 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.junit;
+
+import com.microsoft.playwright.Browser;
+import com.microsoft.playwright.Playwright;
+import com.microsoft.playwright.PlaywrightException;
+import com.microsoft.playwright.junit.Options;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class TestBrowserContextOptionsExtension {
+ @Test
+ void shouldThrowWhenBrowserNameDoesNotMatchDevice() {
+ try (Playwright playwright = Playwright.create()) {
+ Options options = new Options().setDeviceName("iPhone 14").setBrowserName("chromium");
+ PlaywrightException e = assertThrows(PlaywrightException.class,
+ () -> BrowserContextOptionsExtension.getContextOptions(playwright, options));
+ assertEquals("Device \"iPhone 14\" requires \"webkit\" browser, but browserName is \"chromium\"", e.getMessage());
+ }
+ }
+
+ @Test
+ void shouldAllowBrowserNameMatchingDevice() {
+ try (Playwright playwright = Playwright.create()) {
+ Options options = new Options().setDeviceName("iPhone 14").setBrowserName("webkit");
+ Browser.NewContextOptions contextOptions = BrowserContextOptionsExtension.getContextOptions(playwright, options);
+ assertTrue(contextOptions.isMobile);
+ }
+ }
+}
diff --git a/playwright/src/test/java/com/microsoft/playwright/junit/TestFixtureNewContextOptions.java b/playwright/src/test/java/com/microsoft/playwright/junit/TestFixtureNewContextOptions.java
new file mode 100644
index 000000000..0c448c211
--- /dev/null
+++ b/playwright/src/test/java/com/microsoft/playwright/junit/TestFixtureNewContextOptions.java
@@ -0,0 +1,98 @@
+/*
+ * 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.junit;
+
+import com.microsoft.playwright.Browser;
+import com.microsoft.playwright.BrowserContext;
+import com.microsoft.playwright.Page;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@FixtureTest
+@UsePlaywright(TestFixtureNewContextOptions.CustomOptions.class)
+public class TestFixtureNewContextOptions {
+ // Only checked as an option value, never navigated to.
+ private static final String BASE_URL = "http://localhost:1234/base/";
+ private static Browser.NewContextOptions contextOptionsFromBeforeAll;
+
+ public static class CustomOptions implements OptionsFactory {
+ @Override
+ public Options getOptions() {
+ return new Options()
+ .setBaseUrl(BASE_URL)
+ .setDeviceName("iPhone 14")
+ .setIgnoreHTTPSErrors(true)
+ .setContextOptions(new Browser.NewContextOptions().setLocale("de-DE"));
+ }
+ }
+
+ private static void assertConfiguredFromOptionsFactory(Browser.NewContextOptions contextOptions) {
+ assertNotNull(contextOptions);
+ assertEquals(BASE_URL, contextOptions.baseURL);
+ assertEquals(true, contextOptions.ignoreHTTPSErrors);
+ assertEquals("de-DE", contextOptions.locale);
+ assertEquals(390, contextOptions.viewportSize.get().width);
+ assertEquals(664, contextOptions.viewportSize.get().height);
+ assertEquals(true, contextOptions.isMobile);
+ assertEquals(true, contextOptions.hasTouch);
+ assertTrue(contextOptions.userAgent.contains("iPhone"), contextOptions.userAgent);
+ }
+
+ @BeforeAll
+ static void beforeAll(Browser browser, Browser.NewContextOptions contextOptions) {
+ assertConfiguredFromOptionsFactory(contextOptions);
+ contextOptionsFromBeforeAll = contextOptions;
+
+ BrowserContext context = browser.newContext(contextOptions);
+ Page page = context.newPage();
+ assertEquals(390, page.viewportSize().width);
+ assertEquals(664, page.viewportSize().height);
+ assertEquals("de-DE", page.evaluate("() => navigator.language"));
+ assertTrue(((String) page.evaluate("() => navigator.userAgent")).contains("iPhone"));
+ context.close();
+
+ // Modifying the options must not affect other tests.
+ contextOptions.setBaseURL("http://localhost:1234/modified/").setLocale("fr-FR");
+ }
+
+ @AfterAll
+ static void afterAll(Browser.NewContextOptions contextOptions) {
+ assertConfiguredFromOptionsFactory(contextOptions);
+ }
+
+ @Test
+ void shouldBeConfiguredFromOptionsFactory(Browser.NewContextOptions contextOptions) {
+ assertConfiguredFromOptionsFactory(contextOptions);
+ }
+
+ @Test
+ void shouldReturnNewInstanceEveryTime(Browser.NewContextOptions contextOptions1, Browser.NewContextOptions contextOptions2) {
+ assertNotSame(contextOptionsFromBeforeAll, contextOptions1);
+ assertNotSame(contextOptions1, contextOptions2);
+ contextOptions1.setBaseURL("http://localhost:1234/modified/").setLocale("fr-FR");
+ assertConfiguredFromOptionsFactory(contextOptions2);
+ }
+
+ @Test
+ void shouldNotAffectBrowserContextFixture(Page page) {
+ assertEquals(390, page.viewportSize().width);
+ assertEquals("de-DE", page.evaluate("() => navigator.language"));
+ }
+}