-
-
Notifications
You must be signed in to change notification settings - Fork 862
feat: playwright extension #1764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
63b3e10
feat: playwright build extension
MendyLanda a367708
feat: add playwright task
MendyLanda b0bf624
full dep list & added some comments
MendyLanda 9d6f959
Merge branch 'main' into pr/1764
nicktrn cb93119
fix v3-catalog
nicktrn 2e46fe7
log image size for local builds
nicktrn 87741e6
detect playwright version
nicktrn 0f3e618
add changesets
nicktrn 20714e1
add docs
nicktrn 1825557
additional checks
nicktrn fb6252f
Merge remote-tracking branch 'origin/main' into pr/1764
nicktrn 51f41e7
fix unit test workflow for forks
nicktrn 76ac0f0
use middleware and update docs
nicktrn b4611f1
Merge remote-tracking branch 'origin/main' into pr/1764
nicktrn a703894
update jsdoc
nicktrn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"trigger.dev": patch | ||
--- | ||
|
||
Log images sizes for self-hosted deploys |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@trigger.dev/build": patch | ||
--- | ||
|
||
Add playwright extension |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
--- | ||
title: "Playwright" | ||
sidebarTitle: "playwright" | ||
description: "Use the playwright build extension to use Playwright with Trigger.dev" | ||
--- | ||
|
||
If you are using [Playwright](https://playwright.dev/), you should use the Playwright build extension. | ||
|
||
- Automatically installs Playwright and required browser dependencies | ||
- Allows you to specify which browsers to install (chromium, firefox, webkit) | ||
- Supports headless or non-headless mode | ||
- Lets you specify the Playwright version, or auto-detects it | ||
- Installs only the dependencies needed for the selected browsers to optimize build time and image size | ||
|
||
<Note> | ||
This extension only affects the build and deploy process, not the `dev` command. | ||
</Note> | ||
|
||
You can use it for a simple Playwright setup like this: | ||
|
||
```ts | ||
import { defineConfig } from "@trigger.dev/sdk/v3"; | ||
import { playwright } from "@trigger.dev/build/extensions/playwright"; | ||
|
||
export default defineConfig({ | ||
project: "<project ref>", | ||
// Your other config settings... | ||
build: { | ||
extensions: [ | ||
playwright(), | ||
], | ||
}, | ||
}); | ||
``` | ||
|
||
### Options | ||
|
||
- `browsers`: Array of browsers to install. Valid values: `"chromium"`, `"firefox"`, `"webkit"`. Default: `["chromium"]`. | ||
- `headless`: Run browsers in headless mode. Default: `true`. If set to `false`, a virtual display (Xvfb) will be set up automatically. | ||
- `version`: Playwright version to install. If not provided, the version will be auto-detected from your dependencies (recommended). | ||
|
||
<Warning> | ||
Using a different version in your app than specified here will break things. We recommend not setting this option to automatically detect the version. | ||
</Warning> | ||
|
||
### Custom browsers and version | ||
|
||
```ts | ||
import { defineConfig } from "@trigger.dev/sdk/v3"; | ||
import { playwright } from "@trigger.dev/build/extensions/playwright"; | ||
|
||
export default defineConfig({ | ||
project: "<project ref>", | ||
build: { | ||
extensions: [ | ||
playwright({ | ||
browsers: ["chromium", "webkit"], // optional, will use ["chromium"] if not provided | ||
version: "1.43.1", // optional, will automatically detect the version if not provided | ||
}), | ||
], | ||
}, | ||
}); | ||
``` | ||
|
||
### Headless mode | ||
|
||
By default, browsers are run in headless mode. If you need to run browsers with a UI (for example, for debugging), set `headless: false`. This will automatically set up a virtual display using Xvfb. | ||
|
||
```ts | ||
import { defineConfig } from "@trigger.dev/sdk/v3"; | ||
import { playwright } from "@trigger.dev/build/extensions/playwright"; | ||
|
||
export default defineConfig({ | ||
project: "<project ref>", | ||
build: { | ||
extensions: [ | ||
playwright({ | ||
headless: false, | ||
}), | ||
], | ||
}, | ||
}); | ||
``` | ||
|
||
### Environment variables | ||
|
||
The extension sets the following environment variables during the build: | ||
|
||
- `PLAYWRIGHT_BROWSERS_PATH`: Set to `/ms-playwright` so Playwright finds the installed browsers | ||
- `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD`: Set to `1` to skip browser download at runtime | ||
- `PLAYWRIGHT_SKIP_BROWSER_VALIDATION`: Set to `1` to skip browser validation at runtime | ||
- `DISPLAY`: Set to `:99` if `headless: false` (for Xvfb) | ||
|
||
## Managing browser instances | ||
|
||
To prevent issues with waits and resumes, you can use middleware and locals to manage the browser instance. This will ensure the browser is available for the whole run, and is properly cleaned up on waits, resumes, and after the run completes. | ||
|
||
Here's an example using `chromium`, but you can adapt it for other browsers: | ||
|
||
```ts | ||
import { logger, tasks, locals } from "@trigger.dev/sdk"; | ||
import { chromium, type Browser } from "playwright"; | ||
|
||
// Create a locals key for the browser instance | ||
const PlaywrightBrowserLocal = locals.create<{ browser: Browser }>("playwright-browser"); | ||
|
||
export function getBrowser() { | ||
return locals.getOrThrow(PlaywrightBrowserLocal).browser; | ||
} | ||
|
||
export function setBrowser(browser: Browser) { | ||
locals.set(PlaywrightBrowserLocal, { browser }); | ||
} | ||
|
||
tasks.middleware("playwright-browser", async ({ next }) => { | ||
// Launch the browser before the task runs | ||
const browser = await chromium.launch(); | ||
setBrowser(browser); | ||
logger.log("[chromium]: Browser launched (middleware)"); | ||
|
||
try { | ||
await next(); | ||
} finally { | ||
// Always close the browser after the task completes | ||
await browser.close(); | ||
logger.log("[chromium]: Browser closed (middleware)"); | ||
} | ||
}); | ||
|
||
tasks.onWait("playwright-browser", async () => { | ||
// Close the browser when the run is waiting | ||
const browser = getBrowser(); | ||
await browser.close(); | ||
logger.log("[chromium]: Browser closed (onWait)"); | ||
}); | ||
|
||
tasks.onResume("playwright-browser", async () => { | ||
// Relaunch the browser when the run resumes | ||
// Note: You will have to have to manually get a new browser instance in the run function | ||
const browser = await chromium.launch(); | ||
setBrowser(browser); | ||
logger.log("[chromium]: Browser launched (onResume)"); | ||
}); | ||
``` | ||
|
||
You can then use `getBrowser()` in your task's `run` function to access the browser instance: | ||
|
||
```ts | ||
export const playwrightTestTask = task({ | ||
id: "playwright-test", | ||
run: async () => { | ||
const browser = getBrowser(); | ||
const page = await browser.newPage(); | ||
await page.goto("https://google.com"); | ||
await page.screenshot({ path: "screenshot.png" }); | ||
await page.close(); | ||
|
||
// Waits will gracefully close the browser | ||
await wait.for({ seconds: 10 }); | ||
|
||
// On resume, we will re-launch the browser but you will have to manually get the new instance | ||
const newBrowser = getBrowser(); | ||
const newPage = await newBrowser.newPage(); | ||
await newPage.goto("https://playwright.dev"); | ||
await newPage.screenshot({ path: "screenshot2.png" }); | ||
await newPage.close(); | ||
}, | ||
}); | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.