You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>
Copy file name to clipboardExpand all lines: docs/playwright/screenplay-tutorial.md
+129Lines changed: 129 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -100,6 +100,31 @@ public abstract class ScreenplayPlaywrightTest {
100
100
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!
101
101
:::
102
102
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:
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
+
103
128
### Step 2: Define UI Targets
104
129
105
130
Create a class to hold all UI element locators:
@@ -739,6 +764,110 @@ class WhenDeletingTodosScreenplayTest extends ScreenplayPlaywrightTest {
739
764
}
740
765
```
741
766
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:
|`@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
+
publicabstractclassScreenplayPlaywrightTest {
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.
-**`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.
@@ -483,6 +488,25 @@ class WhenUsingPlaywrightWithScreenplayTest {
483
488
}
484
489
```
485
490
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
+
classWhenUsingPlaywrightWithScreenplayTest {
505
+
// ...
506
+
}
507
+
```
508
+
:::
509
+
486
510
Key points about `withPage()`:
487
511
-**Shared browser session** — the actor reuses the `Page`, `BrowserContext`, and `Browser` that `@UsePlaywright` created, avoiding a duplicate browser process.
488
512
-**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