|
| 1 | +# Playwright OnPageLoad Test |
| 2 | + |
| 3 | +This example demonstrates how to expose JavaScript functions to your Swift/WebAssembly tests using Playwright's `page.exposeFunction` API. |
| 4 | + |
| 5 | +## How it works |
| 6 | + |
| 7 | +1. **Expose Script**: A JavaScript file that exports functions to be exposed in the browser context |
| 8 | +2. **Swift Tests**: Call these exposed functions using JavaScriptKit's `JSObject.global` API |
| 9 | +3. **Test Runner**: The `--playwright-expose` flag loads the script and exposes the functions before running tests |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Define exposed functions in a JavaScript file |
| 14 | + |
| 15 | +**Important:** All functions exposed via Playwright's `page.exposeFunction` are async from the browser's perspective, meaning they always return Promises. Define them as `async` for clarity. |
| 16 | + |
| 17 | +#### Option 1: Function with Page Access (Recommended) |
| 18 | + |
| 19 | +Export a function that receives the Playwright `page` object. This allows your exposed functions to interact with the browser page: |
| 20 | + |
| 21 | +```javascript |
| 22 | +/** |
| 23 | + * @param {import('playwright').Page} page - The Playwright Page object |
| 24 | + */ |
| 25 | +export async function exposedFunctions(page) { |
| 26 | + return { |
| 27 | + expectToBeTrue: async () => { |
| 28 | + return true; |
| 29 | + }, |
| 30 | + |
| 31 | + // Use the page object to interact with the browser |
| 32 | + getTitle: async () => { |
| 33 | + return await page.title(); |
| 34 | + }, |
| 35 | + |
| 36 | + clickButton: async (selector) => { |
| 37 | + await page.click(selector); |
| 38 | + return true; |
| 39 | + }, |
| 40 | + |
| 41 | + evaluate: async (script) => { |
| 42 | + return await page.evaluate(script); |
| 43 | + }, |
| 44 | + |
| 45 | + screenshot: async () => { |
| 46 | + const buffer = await page.screenshot(); |
| 47 | + return buffer.toString('base64'); |
| 48 | + } |
| 49 | + }; |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +#### Option 2: Static Object (Simple Cases) |
| 54 | + |
| 55 | +For simple functions that don't need page access: |
| 56 | + |
| 57 | +```javascript |
| 58 | +export const exposedFunctions = { |
| 59 | + expectToBeTrue: async () => { |
| 60 | + return true; |
| 61 | + }, |
| 62 | + |
| 63 | + addNumbers: async (a, b) => { |
| 64 | + return a + b; |
| 65 | + } |
| 66 | +}; |
| 67 | +``` |
| 68 | + |
| 69 | +### Use the functions in Swift tests |
| 70 | + |
| 71 | +```swift |
| 72 | +import XCTest |
| 73 | +import JavaScriptKit |
| 74 | +import JavaScriptEventLoop |
| 75 | + |
| 76 | +final class CheckTests: XCTestCase { |
| 77 | + func testExpectToBeTrue() async throws { |
| 78 | + guard let expectToBeTrue = JSObject.global.expectToBeTrue.function |
| 79 | + else { return XCTFail("Function expectToBeTrue not found") } |
| 80 | + |
| 81 | + // Functions exposed via Playwright return Promises |
| 82 | + guard let promiseObject = expectToBeTrue().object |
| 83 | + else { return XCTFail("expectToBeTrue() did not return an object") } |
| 84 | + |
| 85 | + guard let promise = JSPromise(promiseObject) |
| 86 | + else { return XCTFail("expectToBeTrue() did not return a Promise") } |
| 87 | + |
| 88 | + let resultValue = try await promise.value |
| 89 | + guard let result = resultValue.boolean |
| 90 | + else { return XCTFail("expectToBeTrue() returned nil") } |
| 91 | + |
| 92 | + XCTAssertTrue(result) |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### Run tests with the expose script |
| 98 | + |
| 99 | +```bash |
| 100 | +swift package js test --environment browser --playwright-expose path/to/expose.js |
| 101 | +``` |
| 102 | + |
| 103 | +### Backward Compatibility |
| 104 | + |
| 105 | +You can also use `--prelude` to define exposed functions, which allows combining WASM setup options (`setupOptions`) and Playwright exposed functions in one file: |
| 106 | + |
| 107 | +```bash |
| 108 | +swift package js test --environment browser --prelude path/to/prelude.js |
| 109 | +``` |
| 110 | + |
| 111 | +However, using `--playwright-expose` is recommended for clarity and separation of concerns. |
| 112 | + |
| 113 | +## Advanced Usage |
| 114 | + |
| 115 | +### Access to Page Context |
| 116 | + |
| 117 | +When you export a function (as shown in Option 1), you receive the Playwright `page` object, which gives you full access to the browser page. This is powerful because you can: |
| 118 | + |
| 119 | +- **Query the DOM**: `await page.$('selector')` |
| 120 | +- **Execute JavaScript**: `await page.evaluate('...')` |
| 121 | +- **Take screenshots**: `await page.screenshot()` |
| 122 | +- **Navigate**: `await page.goto('...')` |
| 123 | +- **Handle events**: `page.on('console', ...)` |
| 124 | + |
| 125 | +### Async Initialization |
| 126 | + |
| 127 | +You can perform async initialization before returning your functions: |
| 128 | + |
| 129 | +```javascript |
| 130 | +export async function exposedFunctions(page) { |
| 131 | + // Perform async setup |
| 132 | + const config = await loadConfiguration(); |
| 133 | + |
| 134 | + // Navigate to a specific page if needed |
| 135 | + await page.goto('http://example.com'); |
| 136 | + |
| 137 | + return { |
| 138 | + expectToBeTrue: async () => true, |
| 139 | + |
| 140 | + getConfig: async () => config, |
| 141 | + |
| 142 | + // Function that uses both page and initialization data |
| 143 | + checkElement: async (selector) => { |
| 144 | + const element = await page.$(selector); |
| 145 | + return element !== null; |
| 146 | + } |
| 147 | + }; |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +### Best Practices |
| 152 | + |
| 153 | +1. **Always use `async` functions**: All exposed functions are async from the browser's perspective |
| 154 | +2. **Capture `page` in closures**: Functions returned from `exposedFunctions(page)` can access `page` via closure |
| 155 | +3. **Handle errors**: Wrap page interactions in try-catch blocks |
| 156 | +4. **Return serializable data**: Functions can only return JSON-serializable values |
0 commit comments