Skip to content

Commit

Permalink
Allow for customization of the BrowserContext (#112)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
edeandrea authored Jan 31, 2025
1 parent 4b7b757 commit 1fe11d7
Show file tree
Hide file tree
Showing 14 changed files with 643 additions and 4 deletions.
19 changes: 19 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
3 changes: 2 additions & 1 deletion docs/modules/ROOT/pages/includes/attributes.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
:project-version: 2.0.0

:examples-dir: ./../examples/
:examples-dir: ./../examples/
:runtime-dir: ./../../../../runtime
16 changes: 16 additions & 0 deletions docs/modules/ROOT/pages/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,20 @@ USER 1001
CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]
----

== Additional Configuration

There is additional configuration options available on the `@BrowserContextConfig` and `@WithPlaywright` annotations:

.io.quarkiverse.playwright.BrowserContextConfig
[source,java,subs="+attributes,macros+"]
----
include::{runtime-dir}/src/main/java/io/quarkiverse/playwright/BrowserContextConfig.java[]
----

.io.quarkiverse.playwright.WithPlaywright
[source,java,subs="+attributes,macros+"]
----
include::{runtime-dir}/src/main/java/io/quarkiverse/playwright/WithPlaywright.java[]
----

include::includes/quarkus-playwright.adoc[leveloffset=+1, opts=optional]
6 changes: 6 additions & 0 deletions docs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<configuration>
<attributes>
<allow-uri-read/>
<safe-mode>UNSAFE</safe-mode>
</attributes>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
3 changes: 2 additions & 1 deletion docs/templates/includes/attributes.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
:project-version: ${release.current-version}

:examples-dir: ./../examples/
:examples-dir: ./../examples/
:runtime-dir: ./../../../../runtime
6 changes: 6 additions & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
<artifactId>quarkus-junit5</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.27.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
Expand Down
76 changes: 76 additions & 0 deletions integration-tests/src/test/java/org/acme/PlaywrightConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.acme;

import static org.assertj.core.api.Assertions.*;

import java.net.URL;
import java.util.concurrent.TimeUnit;

import jakarta.ws.rs.core.HttpHeaders;

import org.junit.jupiter.api.Test;

import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Response;
import com.microsoft.playwright.TimeoutError;
import com.microsoft.playwright.assertions.PlaywrightAssertions;

import io.quarkiverse.playwright.BrowserContextConfig;
import io.quarkiverse.playwright.InjectPlaywright;
import io.quarkiverse.playwright.WithPlaywright;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
@WithPlaywright(browserContext = @BrowserContextConfig(userAgent = "playwright-browser", defaultNavigationTimeout = "PT10s"))
class PlaywrightConfigTest {
@InjectPlaywright
BrowserContext context;

@TestHTTPResource("/")
URL index;

@Test
void timeoutWorks() {
var page = context.newPage();
page.route(
index.toString(),
r -> {
try {
// Introduce intentional timeout
TimeUnit.SECONDS.sleep(12);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

r.resume();
});

assertThatExceptionOfType(TimeoutError.class)
.isThrownBy(() -> page.navigate(index.toString()));
}

@Test
void testConfig() {
var page = context.newPage();

page.onRequest(r -> assertThat(r.headerValue(HttpHeaders.USER_AGENT)).isEqualTo("playwright-browser"));

var response = page.navigate(index.toString());

assertThat(response)
.extracting(Response::status)
.isEqualTo(200);

page.waitForLoadState();

PlaywrightAssertions.assertThat(page)
.hasTitle("My Awesome App");

// Make sure the web app is loaded and hits the backend
var quinoaEl = page.waitForSelector(".toast-body.received");
var greeting = quinoaEl.innerText();

assertThat(greeting)
.isEqualTo("Hello from RESTEasy Reactive");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.acme;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URL;

import jakarta.ws.rs.core.HttpHeaders;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -28,6 +32,8 @@ public class WithDefaultPlaywrightTest {
@Test
public void testIndex() {
final Page page = context.newPage();
page.onRequest(r -> assertThat(r.headerValue(HttpHeaders.USER_AGENT)).isNotEqualTo("playwright-browser"));

Response response = page.navigate(index.toString());
Assertions.assertEquals("OK", response.statusText());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.acme;

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

import java.net.URL;

import jakarta.ws.rs.core.HttpHeaders;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -30,6 +33,8 @@ public class WithFirefoxPlaywrightTest {
@Test
public void testIndex() {
final Page page = context.newPage();
page.onRequest(r -> assertThat(r.headerValue(HttpHeaders.USER_AGENT)).isNotEqualTo("playwright-browser"));

Response response = page.navigate(index.toString());
Assertions.assertEquals("OK", response.statusText());

Expand Down
Loading

0 comments on commit 1fe11d7

Please sign in to comment.