Skip to content

Commit 1fe11d7

Browse files
authored
Allow for customization of the BrowserContext (#112)
This would also open up a pattern to be able to further customize the `BrowserContext`, or other things within Playwright. I initially tried to do it via properties (i.e. `quarkus.playwright.*`, but unfortunately a `QuarkusTestResourceConfigurableLifecycleManager` doesn't have access to `@ConfigMapping`s (I checked with Georgios).
1 parent 4b7b757 commit 1fe11d7

File tree

14 files changed

+643
-4
lines changed

14 files changed

+643
-4
lines changed

.mvn/wrapper/maven-wrapper.properties

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
wrapperVersion=3.3.2
18+
distributionType=only-script
19+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
:project-version: 2.0.0
22

3-
:examples-dir: ./../examples/
3+
:examples-dir: ./../examples/
4+
:runtime-dir: ./../../../../runtime

docs/modules/ROOT/pages/index.adoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,20 @@ USER 1001
101101
CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]
102102
----
103103

104+
== Additional Configuration
105+
106+
There is additional configuration options available on the `@BrowserContextConfig` and `@WithPlaywright` annotations:
107+
108+
.io.quarkiverse.playwright.BrowserContextConfig
109+
[source,java,subs="+attributes,macros+"]
110+
----
111+
include::{runtime-dir}/src/main/java/io/quarkiverse/playwright/BrowserContextConfig.java[]
112+
----
113+
114+
.io.quarkiverse.playwright.WithPlaywright
115+
[source,java,subs="+attributes,macros+"]
116+
----
117+
include::{runtime-dir}/src/main/java/io/quarkiverse/playwright/WithPlaywright.java[]
118+
----
119+
104120
include::includes/quarkus-playwright.adoc[leveloffset=+1, opts=optional]

docs/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@
9898
<plugin>
9999
<groupId>org.asciidoctor</groupId>
100100
<artifactId>asciidoctor-maven-plugin</artifactId>
101+
<configuration>
102+
<attributes>
103+
<allow-uri-read/>
104+
<safe-mode>UNSAFE</safe-mode>
105+
</attributes>
106+
</configuration>
101107
</plugin>
102108
</plugins>
103109
</build>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
:project-version: ${release.current-version}
22

3-
:examples-dir: ./../examples/
3+
:examples-dir: ./../examples/
4+
:runtime-dir: ./../../../../runtime

integration-tests/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
<artifactId>quarkus-junit5</artifactId>
5151
<optional>true</optional>
5252
</dependency>
53+
<dependency>
54+
<groupId>org.assertj</groupId>
55+
<artifactId>assertj-core</artifactId>
56+
<version>3.27.3</version>
57+
<scope>test</scope>
58+
</dependency>
5359
</dependencies>
5460
<build>
5561
<pluginManagement>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.acme;
2+
3+
import static org.assertj.core.api.Assertions.*;
4+
5+
import java.net.URL;
6+
import java.util.concurrent.TimeUnit;
7+
8+
import jakarta.ws.rs.core.HttpHeaders;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
import com.microsoft.playwright.BrowserContext;
13+
import com.microsoft.playwright.Response;
14+
import com.microsoft.playwright.TimeoutError;
15+
import com.microsoft.playwright.assertions.PlaywrightAssertions;
16+
17+
import io.quarkiverse.playwright.BrowserContextConfig;
18+
import io.quarkiverse.playwright.InjectPlaywright;
19+
import io.quarkiverse.playwright.WithPlaywright;
20+
import io.quarkus.test.common.http.TestHTTPResource;
21+
import io.quarkus.test.junit.QuarkusTest;
22+
23+
@QuarkusTest
24+
@WithPlaywright(browserContext = @BrowserContextConfig(userAgent = "playwright-browser", defaultNavigationTimeout = "PT10s"))
25+
class PlaywrightConfigTest {
26+
@InjectPlaywright
27+
BrowserContext context;
28+
29+
@TestHTTPResource("/")
30+
URL index;
31+
32+
@Test
33+
void timeoutWorks() {
34+
var page = context.newPage();
35+
page.route(
36+
index.toString(),
37+
r -> {
38+
try {
39+
// Introduce intentional timeout
40+
TimeUnit.SECONDS.sleep(12);
41+
} catch (InterruptedException e) {
42+
throw new RuntimeException(e);
43+
}
44+
45+
r.resume();
46+
});
47+
48+
assertThatExceptionOfType(TimeoutError.class)
49+
.isThrownBy(() -> page.navigate(index.toString()));
50+
}
51+
52+
@Test
53+
void testConfig() {
54+
var page = context.newPage();
55+
56+
page.onRequest(r -> assertThat(r.headerValue(HttpHeaders.USER_AGENT)).isEqualTo("playwright-browser"));
57+
58+
var response = page.navigate(index.toString());
59+
60+
assertThat(response)
61+
.extracting(Response::status)
62+
.isEqualTo(200);
63+
64+
page.waitForLoadState();
65+
66+
PlaywrightAssertions.assertThat(page)
67+
.hasTitle("My Awesome App");
68+
69+
// Make sure the web app is loaded and hits the backend
70+
var quinoaEl = page.waitForSelector(".toast-body.received");
71+
var greeting = quinoaEl.innerText();
72+
73+
assertThat(greeting)
74+
.isEqualTo("Hello from RESTEasy Reactive");
75+
}
76+
}

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

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

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
35
import java.net.URL;
46

7+
import jakarta.ws.rs.core.HttpHeaders;
8+
59
import org.junit.jupiter.api.Assertions;
610
import org.junit.jupiter.api.Test;
711

@@ -28,6 +32,8 @@ public class WithDefaultPlaywrightTest {
2832
@Test
2933
public void testIndex() {
3034
final Page page = context.newPage();
35+
page.onRequest(r -> assertThat(r.headerValue(HttpHeaders.USER_AGENT)).isNotEqualTo("playwright-browser"));
36+
3137
Response response = page.navigate(index.toString());
3238
Assertions.assertEquals("OK", response.statusText());
3339

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.acme;
22

33
import static io.quarkiverse.playwright.WithPlaywright.Browser.FIREFOX;
4+
import static org.assertj.core.api.Assertions.assertThat;
45

56
import java.net.URL;
67

8+
import jakarta.ws.rs.core.HttpHeaders;
9+
710
import org.junit.jupiter.api.Assertions;
811
import org.junit.jupiter.api.Test;
912

@@ -30,6 +33,8 @@ public class WithFirefoxPlaywrightTest {
3033
@Test
3134
public void testIndex() {
3235
final Page page = context.newPage();
36+
page.onRequest(r -> assertThat(r.headerValue(HttpHeaders.USER_AGENT)).isNotEqualTo("playwright-browser"));
37+
3338
Response response = page.navigate(index.toString());
3439
Assertions.assertEquals("OK", response.statusText());
3540

0 commit comments

Comments
 (0)