Skip to content

Commit 1aab8bb

Browse files
authored
Add more browser context configuration (#187)
Support a custom timezone ID and a custom viewport size. I tried to have it with configuration properties first, but that failed and later learned the others tried before.
1 parent 36be0b2 commit 1aab8bb

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

integration-tests/src/test/java/org/acme/PlaywrightConfigTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.acme;
22

3-
import static org.assertj.core.api.Assertions.*;
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
45

56
import java.net.URL;
67
import java.util.concurrent.TimeUnit;
@@ -21,7 +22,7 @@
2122
import io.quarkus.test.junit.QuarkusTest;
2223

2324
@QuarkusTest
24-
@WithPlaywright(browserContext = @BrowserContextConfig(userAgent = "playwright-browser", defaultNavigationTimeout = "PT10s"))
25+
@WithPlaywright(browserContext = @BrowserContextConfig(userAgent = "playwright-browser", timeZoneId = "Europe/Amsterdam", defaultNavigationTimeout = "PT10s", viewportSize = @BrowserContextConfig.ViewportSize(width = 1920, height = 1080)))
2526
class PlaywrightConfigTest {
2627
@InjectPlaywright
2728
BrowserContext context;

runtime/src/main/java/io/quarkiverse/playwright/BrowserContextConfig.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,35 @@
4949
*/
5050
String locale() default "";
5151

52+
/**
53+
* Changes the timezone of the context. See <a href=
54+
* "https://cs.chromium.org/chromium/src/third_party/icu/source/data/misc/metaZones.txt?rcl=faee8bc70570192d82d2978a71e2a615788597d1">ICU's
55+
* metaZones.txt</a> for a list of supported timezone IDs. Defaults to the system timezone.
56+
*
57+
* @see com.microsoft.playwright.Browser.NewContextOptions#setTimezoneId(String)
58+
*/
59+
String timeZoneId() default "";
60+
61+
/**
62+
* Emulates consistent viewport for each page. Defaults to an 1280x720 viewport.
63+
*
64+
* @see com.microsoft.playwright.Browser.NewContextOptions#setViewportSize(int, int)
65+
*/
66+
ViewportSize viewportSize() default @ViewportSize(width = 1080, height = 720);
67+
5268
/**
5369
* Whether to emulate the network being offline for the browser context
5470
*
5571
* @see com.microsoft.playwright.Browser.NewContextOptions#setOffline(boolean)
5672
*/
5773
boolean offline() default false;
74+
75+
@Target({})
76+
@Retention(RetentionPolicy.RUNTIME)
77+
@Inherited
78+
@interface ViewportSize {
79+
int width();
80+
81+
int height();
82+
}
5883
}

runtime/src/main/java/io/quarkiverse/playwright/QuarkusPlaywrightManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ private static void applyBrowserContextConfig(NewContextOptions contextOptions,
155155
contextOptions.setLocale(config.locale());
156156
}
157157

158+
if (StringUtils.isNotBlank(config.timeZoneId())) {
159+
contextOptions.setTimezoneId(config.timeZoneId());
160+
}
161+
162+
contextOptions.setViewportSize(config.viewportSize().width(), config.viewportSize().height());
163+
158164
if (StringUtils.isNotBlank(config.userAgent())) {
159165
contextOptions.setUserAgent(config.userAgent());
160166
}

0 commit comments

Comments
 (0)