Skip to content

Commit 2065762

Browse files
wakaleoclaude
andcommitted
docs(playwright): Add Serenity extensions to @UsePlaywright Screenplay examples
Include @ExtendWith(SerenityJUnit5Extension.class) and @ExtendWith(SerenityPlaywrightExtension.class) in all @UsePlaywright code examples, consistent with the established annotation pattern. Also add the @SerenityPlaywright shorthand tip and annotation table. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 411a5db commit 2065762

2 files changed

Lines changed: 153 additions & 0 deletions

File tree

docs/playwright/screenplay-tutorial.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,31 @@ public abstract class ScreenplayPlaywrightTest {
100100
The `BrowseTheWebWithPlaywright` ability subscribes to Serenity's test lifecycle events and automatically closes the browser, context, and page when each test completes. You don't need to write any teardown code!
101101
:::
102102

103+
:::info Alternative: Using `@UsePlaywright`
104+
If you prefer to use Playwright's `@UsePlaywright` annotation for browser lifecycle management, you can wrap the injected `Page` instead:
105+
106+
```java
107+
import com.microsoft.playwright.Page;
108+
import com.microsoft.playwright.junit.UsePlaywright;
109+
import net.serenitybdd.playwright.junit5.SerenityPlaywrightExtension;
110+
111+
@ExtendWith(SerenityPlaywrightExtension.class)
112+
@UsePlaywright
113+
public abstract class ScreenplayPlaywrightTest {
114+
115+
protected Actor toby;
116+
117+
@BeforeEach
118+
void setUpPlaywright(Page page) {
119+
toby = Actor.named("Toby");
120+
toby.can(BrowseTheWebWithPlaywright.withPage(page));
121+
}
122+
}
123+
```
124+
125+
The `SerenityPlaywrightExtension` registers Playwright pages with Serenity for automatic screenshot capture. This shares a single browser instance between `@UsePlaywright` and Screenplay, instead of creating two separate browsers. See [Using @UsePlaywright with Screenplay](#using-useplaywright-with-screenplay) at the end of this tutorial for a full walkthrough.
126+
:::
127+
103128
### Step 2: Define UI Targets
104129

105130
Create a class to hold all UI element locators:
@@ -739,6 +764,110 @@ class WhenDeletingTodosScreenplayTest extends ScreenplayPlaywrightTest {
739764
}
740765
```
741766

767+
## Using `@UsePlaywright` with Screenplay
768+
769+
Throughout this tutorial, we used `BrowseTheWebWithPlaywright.usingTheDefaultConfiguration()` — which creates and manages its own Playwright browser. But if your project already uses Playwright's `@UsePlaywright` annotation (for example, to share browser configuration or integrate with Playwright's built-in test fixtures), you can adapt the Screenplay tests to reuse that browser session.
770+
771+
### Updating the Base Test Class
772+
773+
Replace `usingTheDefaultConfiguration()` with `withPage(page)`, and add the `@UsePlaywright` and `SerenityPlaywrightExtension` annotations:
774+
775+
```java
776+
package todomvc.screenplay;
777+
778+
import com.microsoft.playwright.Page;
779+
import com.microsoft.playwright.junit.UsePlaywright;
780+
import net.serenitybdd.junit5.SerenityJUnit5Extension;
781+
import net.serenitybdd.playwright.junit5.SerenityPlaywrightExtension;
782+
import net.serenitybdd.screenplay.Actor;
783+
import net.serenitybdd.screenplay.playwright.abilities.BrowseTheWebWithPlaywright;
784+
import org.junit.jupiter.api.BeforeEach;
785+
import org.junit.jupiter.api.extension.ExtendWith;
786+
787+
@ExtendWith(SerenityJUnit5Extension.class)
788+
@ExtendWith(SerenityPlaywrightExtension.class)
789+
@UsePlaywright
790+
public abstract class ScreenplayPlaywrightTest {
791+
792+
protected Actor toby;
793+
794+
@BeforeEach
795+
void setUpPlaywright(Page page) {
796+
toby = Actor.named("Toby");
797+
toby.can(BrowseTheWebWithPlaywright.withPage(page));
798+
}
799+
}
800+
```
801+
802+
Three annotations work together:
803+
804+
| Annotation | Purpose |
805+
|-----------|---------|
806+
| `@ExtendWith(SerenityJUnit5Extension.class)` | Serenity BDD reporting and step injection |
807+
| `@ExtendWith(SerenityPlaywrightExtension.class)` | Registers Playwright pages with Serenity for automatic screenshots |
808+
| `@UsePlaywright` | Manages the full Playwright lifecycle (Playwright, Browser, BrowserContext, Page) |
809+
810+
:::tip Using @SerenityPlaywright
811+
You can replace both `@ExtendWith` annotations with the shorthand `@SerenityPlaywright`:
812+
```java
813+
@SerenityPlaywright
814+
@UsePlaywright
815+
public abstract class ScreenplayPlaywrightTest {
816+
// ...
817+
}
818+
```
819+
:::
820+
821+
That's it. All the tasks, questions, targets, and test classes from this tutorial work unchanged — the only difference is how the actor's ability is created.
822+
823+
### What Changes
824+
825+
| | `usingTheDefaultConfiguration()` | `withPage(page)` |
826+
|---|---|---|
827+
| **Browser lifecycle** | Managed by Screenplay | Managed by `@UsePlaywright` |
828+
| **Browser instance** | New browser per actor | Reuses `@UsePlaywright` browser |
829+
| **Teardown** | Closes browser, context, page | Only unregisters from Serenity |
830+
| **Tasks & Questions** | Work normally | Work normally |
831+
| **Screenshots & reports** | Captured automatically | Captured automatically |
832+
833+
### Custom Playwright Options
834+
835+
With `@UsePlaywright`, you can customize browser options by implementing `OptionsFactory`:
836+
837+
```java
838+
import com.microsoft.playwright.*;
839+
import com.microsoft.playwright.junit.Options;
840+
import com.microsoft.playwright.junit.OptionsFactory;
841+
842+
public class CustomOptions implements OptionsFactory {
843+
844+
@Override
845+
public Options getOptions() {
846+
return new Options()
847+
.setLaunchOptions(new BrowserType.LaunchOptions().setHeadless(true))
848+
.setContextOptions(new Browser.NewContextOptions()
849+
.setViewportSize(1920, 1080)
850+
.setLocale("en-US"));
851+
}
852+
}
853+
```
854+
855+
Then reference it in your tests:
856+
857+
```java
858+
@ExtendWith(SerenityJUnit5Extension.class)
859+
@ExtendWith(SerenityPlaywrightExtension.class)
860+
@UsePlaywright(CustomOptions.class)
861+
public abstract class ScreenplayPlaywrightTest {
862+
// ...same as above
863+
}
864+
```
865+
866+
### When to Use Which Approach
867+
868+
- **`usingTheDefaultConfiguration()`** — simplest setup; ideal when Screenplay is your only test framework and you don't need Playwright's JUnit integration features.
869+
- **`withPage(page)`** — use when you already have `@UsePlaywright` tests and want to add Screenplay actors to them, or when you need Playwright's built-in fixtures (tracing, video recording, custom options) alongside Screenplay.
870+
742871
## Key Takeaways
743872

744873
### The Screenplay Pattern Benefits

docs/playwright/screenplay.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,17 @@ If you're already using Playwright's `@UsePlaywright` annotation for lifecycle m
452452
```java
453453
import com.microsoft.playwright.Page;
454454
import com.microsoft.playwright.junit.UsePlaywright;
455+
import net.serenitybdd.junit5.SerenityJUnit5Extension;
456+
import net.serenitybdd.playwright.junit5.SerenityPlaywrightExtension;
455457
import net.serenitybdd.screenplay.Actor;
456458
import net.serenitybdd.screenplay.playwright.abilities.BrowseTheWebWithPlaywright;
457459
import net.serenitybdd.screenplay.playwright.interactions.Open;
458460
import net.serenitybdd.screenplay.playwright.assertions.Ensure;
459461
import org.junit.jupiter.api.*;
462+
import org.junit.jupiter.api.extension.ExtendWith;
460463

464+
@ExtendWith(SerenityJUnit5Extension.class)
465+
@ExtendWith(SerenityPlaywrightExtension.class)
461466
@UsePlaywright
462467
@DisplayName("TodoMVC with @UsePlaywright")
463468
class WhenUsingPlaywrightWithScreenplayTest {
@@ -483,6 +488,25 @@ class WhenUsingPlaywrightWithScreenplayTest {
483488
}
484489
```
485490

491+
Three annotations work together here:
492+
493+
| Annotation | Purpose |
494+
|-----------|---------|
495+
| `@ExtendWith(SerenityJUnit5Extension.class)` | Serenity BDD reporting and step injection |
496+
| `@ExtendWith(SerenityPlaywrightExtension.class)` | Registers Playwright pages with Serenity for automatic screenshots |
497+
| `@UsePlaywright` | Manages the full Playwright lifecycle (Playwright, Browser, BrowserContext, Page) |
498+
499+
:::tip Using @SerenityPlaywright
500+
You can use `@SerenityPlaywright` as a shorthand for both Serenity extensions:
501+
```java
502+
@SerenityPlaywright
503+
@UsePlaywright
504+
class WhenUsingPlaywrightWithScreenplayTest {
505+
// ...
506+
}
507+
```
508+
:::
509+
486510
Key points about `withPage()`:
487511
- **Shared browser session** — the actor reuses the `Page`, `BrowserContext`, and `Browser` that `@UsePlaywright` created, avoiding a duplicate browser process.
488512
- **No resource conflicts** — on teardown, Screenplay only unregisters its internal references. It does _not_ close the `Page`, `BrowserContext`, `Browser`, or `Playwright` instance, since `@UsePlaywright` owns their lifecycle.

0 commit comments

Comments
 (0)