-
Notifications
You must be signed in to change notification settings - Fork 224
Add browser integration tests using Playwright #222
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 all commits
Commits
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
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,3 @@ | ||
package-lock=false | ||
audit=false | ||
fund=false |
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,39 @@ | ||
# Browser integration tests | ||
|
||
Uses [`playwright`](https://playwright.dev/docs) to run a basic integration test against the three most common browser engines, Firefox, Chromium and WebKit. | ||
|
||
It uses the `replicate/canary` model for the moment, which requires a Replicate API token available in the environment under `REPLICATE_API_TOKEN`. | ||
|
||
The entire suite is a single `main()` function that calls a single model exercising the streaming API. | ||
|
||
The test uses `esbuild` within the test generate a browser friendly version of the `index.js` file which is loaded into the given browser and calls the `main()` function asserting the response content. | ||
|
||
## CORS | ||
|
||
The Replicate API doesn't support Cross Origin Resource Sharing at this time. We work around this in Playwright by intercepting the request in a `page.route` handler. We don't modify the request/response, but this seems to work around the restriction. | ||
|
||
## Setup | ||
|
||
npm install | ||
|
||
## Local | ||
|
||
The following command will run the tests across all browsers. | ||
|
||
npm test | ||
|
||
To run against the default browser (chromium) run: | ||
|
||
npm exec playwright test | ||
|
||
Or, specify a browser with: | ||
|
||
npm exec playwright test --browser firefox | ||
|
||
## Debugging | ||
|
||
Running `playwright test` with the `--debug` flag opens a browser window with a debugging interface, and a breakpoint set at the start of the test. It can also be connected directly to VSCode. | ||
|
||
npm exec playwright test --debug | ||
|
||
The browser.js file is injected into the page via a script tag, to be able to set breakpoints in this file you'll need to use a `debugger` statement and open the devtools in the spawned browser window before continuing the test suite. |
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,22 @@ | ||
import Replicate from "replicate"; | ||
|
||
/** | ||
* @param {string} - token the REPLICATE_API_TOKEN | ||
*/ | ||
window.main = async (token) => { | ||
const replicate = new Replicate({ auth: token }); | ||
const stream = replicate.stream( | ||
"replicate/canary:30e22229542eb3f79d4f945dacb58d32001b02cc313ae6f54eef27904edf3272", | ||
{ | ||
input: { | ||
text: "Betty Browser", | ||
}, | ||
} | ||
); | ||
|
||
const output = []; | ||
for await (const event of stream) { | ||
output.push(String(event)); | ||
} | ||
return output.join(""); | ||
}; |
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,34 @@ | ||
import { test, expect } from "@playwright/test"; | ||
import { build } from "esbuild"; | ||
|
||
// Convert the source file from commonjs to a browser script. | ||
const result = await build({ | ||
entryPoints: ["index.js"], | ||
bundle: true, | ||
platform: "browser", | ||
external: ["node:crypto"], | ||
write: false, | ||
}); | ||
const source = new TextDecoder().decode(result.outputFiles[0].contents); | ||
|
||
// https://playwright.dev/docs/network#modify-requests | ||
|
||
test("browser", async ({ page }) => { | ||
// Patch the API endpoint to work around CORS for now. | ||
await page.route( | ||
"https://api.replicate.com/v1/predictions", | ||
async (route) => { | ||
// Fetch original response. | ||
const response = await route.fetch(); | ||
// Add a prefix to the title. | ||
return route.fulfill({ response }); | ||
} | ||
); | ||
|
||
await page.addScriptTag({ content: source }); | ||
const result = await page.evaluate( | ||
(token) => window.main(token), | ||
[process.env.REPLICATE_API_TOKEN] | ||
); | ||
expect(result).toBe("hello there, Betty Browser"); | ||
}); |
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,19 @@ | ||
{ | ||
"name": "replicate-app-browser", | ||
"private": true, | ||
"version": "0.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"test": "playwright test --browser all" | ||
}, | ||
"license": "ISC", | ||
"dependencies": { | ||
"replicate": "../../" | ||
}, | ||
"devDependencies": { | ||
"@playwright/test": "^1.42.1", | ||
"esbuild": "^0.20.1" | ||
} | ||
} |
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,3 @@ | ||
import { defineConfig } from "@playwright/test"; | ||
|
||
export default defineConfig({}); |
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
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.