|
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.microsoft.playwright.junit; |
| 18 | + |
| 19 | +import com.microsoft.playwright.Browser; |
| 20 | +import com.microsoft.playwright.BrowserContext; |
| 21 | +import com.microsoft.playwright.Page; |
| 22 | +import org.junit.jupiter.api.AfterAll; |
| 23 | +import org.junit.jupiter.api.BeforeAll; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import static org.junit.jupiter.api.Assertions.*; |
| 27 | + |
| 28 | +@FixtureTest |
| 29 | +@UsePlaywright(TestFixtureNewContextOptions.CustomOptions.class) |
| 30 | +public class TestFixtureNewContextOptions { |
| 31 | + // Only checked as an option value, never navigated to. |
| 32 | + private static final String BASE_URL = "http://localhost:1234/base/"; |
| 33 | + private static Browser.NewContextOptions contextOptionsFromBeforeAll; |
| 34 | + |
| 35 | + public static class CustomOptions implements OptionsFactory { |
| 36 | + @Override |
| 37 | + public Options getOptions() { |
| 38 | + return new Options() |
| 39 | + .setBaseUrl(BASE_URL) |
| 40 | + .setDeviceName("iPhone 14") |
| 41 | + .setIgnoreHTTPSErrors(true) |
| 42 | + .setContextOptions(new Browser.NewContextOptions().setLocale("de-DE")); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static void assertConfiguredFromOptionsFactory(Browser.NewContextOptions contextOptions) { |
| 47 | + assertNotNull(contextOptions); |
| 48 | + assertEquals(BASE_URL, contextOptions.baseURL); |
| 49 | + assertEquals(true, contextOptions.ignoreHTTPSErrors); |
| 50 | + assertEquals("de-DE", contextOptions.locale); |
| 51 | + assertEquals(390, contextOptions.viewportSize.get().width); |
| 52 | + assertEquals(664, contextOptions.viewportSize.get().height); |
| 53 | + assertEquals(true, contextOptions.isMobile); |
| 54 | + assertEquals(true, contextOptions.hasTouch); |
| 55 | + assertTrue(contextOptions.userAgent.contains("iPhone"), contextOptions.userAgent); |
| 56 | + } |
| 57 | + |
| 58 | + @BeforeAll |
| 59 | + static void beforeAll(Browser browser, Browser.NewContextOptions contextOptions) { |
| 60 | + assertConfiguredFromOptionsFactory(contextOptions); |
| 61 | + contextOptionsFromBeforeAll = contextOptions; |
| 62 | + |
| 63 | + BrowserContext context = browser.newContext(contextOptions); |
| 64 | + Page page = context.newPage(); |
| 65 | + assertEquals(390, page.viewportSize().width); |
| 66 | + assertEquals(664, page.viewportSize().height); |
| 67 | + assertEquals("de-DE", page.evaluate("() => navigator.language")); |
| 68 | + assertTrue(((String) page.evaluate("() => navigator.userAgent")).contains("iPhone")); |
| 69 | + context.close(); |
| 70 | + |
| 71 | + // Modifying the options must not affect other tests. |
| 72 | + contextOptions.setBaseURL("http://localhost:1234/modified/").setLocale("fr-FR"); |
| 73 | + } |
| 74 | + |
| 75 | + @AfterAll |
| 76 | + static void afterAll(Browser.NewContextOptions contextOptions) { |
| 77 | + assertConfiguredFromOptionsFactory(contextOptions); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void shouldBeConfiguredFromOptionsFactory(Browser.NewContextOptions contextOptions) { |
| 82 | + assertConfiguredFromOptionsFactory(contextOptions); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void shouldReturnNewInstanceEveryTime(Browser.NewContextOptions contextOptions1, Browser.NewContextOptions contextOptions2) { |
| 87 | + assertNotSame(contextOptionsFromBeforeAll, contextOptions1); |
| 88 | + assertNotSame(contextOptions1, contextOptions2); |
| 89 | + contextOptions1.setBaseURL("http://localhost:1234/modified/").setLocale("fr-FR"); |
| 90 | + assertConfiguredFromOptionsFactory(contextOptions2); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void shouldNotAffectBrowserContextFixture(Page page) { |
| 95 | + assertEquals(390, page.viewportSize().width); |
| 96 | + assertEquals("de-DE", page.evaluate("() => navigator.language")); |
| 97 | + } |
| 98 | +} |
0 commit comments