Repository navigation
feat(junit): add Browser.NewContextOptions fixture #2005
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ght/src/main/java/com/microsoft/playwright/impl/junit/BrowserContextOptionsExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...src/test/java/com/microsoft/playwright/impl/junit/TestBrowserContextOptionsExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
| } |
98 changes: 98 additions & 0 deletions
98
playwright/src/test/java/com/microsoft/playwright/junit/TestFixtureNewContextOptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert browser type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in decf3d4: setting both
deviceNameand abrowserNamethat differs from the device'sdefaultBrowserTypenow throws. With onlydeviceNameset, the browser is already taken from the device.