|
| 1 | +--- |
| 2 | +name: implement-playwright-method |
| 3 | +description: Implement a Playwright method in effect-playwright by analyzing source code and mapping types |
| 4 | +--- |
| 5 | + |
| 6 | +## What I do |
| 7 | + |
| 8 | +I guide you through implementing a Playwright method in the `effect-playwright` library. This involves analyzing the original Playwright source code to determine behavior (throwing vs. non-throwing) and mapping types to the Effect ecosystem (Effect, Option, Stream). |
| 9 | + |
| 10 | +## When to use me |
| 11 | + |
| 12 | +Use this skill when you need to add a missing method to `effect-playwright` or update an existing one. |
| 13 | + |
| 14 | +## Procedure |
| 15 | + |
| 16 | +### 1. Locate Playwright Source Code |
| 17 | + |
| 18 | +First, find the implementation of the method in the Playwright codebase to understand its behavior. |
| 19 | + |
| 20 | +- Look in `context/playwright/packages/playwright-core/src/client/`. |
| 21 | +- Common files: `page.ts`, `locator.ts`, `browser.ts`, `frame.ts`, `elementHandle.ts`. |
| 22 | + |
| 23 | +### 2. Analyze the Method |
| 24 | + |
| 25 | +Determine if the method can throw and what it returns. **Do not blindly follow existing patterns in `effect-playwright` if they wrap safe synchronous methods in Effects.** |
| 26 | + |
| 27 | +#### Can it throw? |
| 28 | + |
| 29 | +- **Async / Promise-based**: If the method returns a `Promise`, it interacts with the Playwright server and **can throw** (e.g., timeouts, target closed). |
| 30 | + - **Mapping**: Map to `Effect<Return, PlaywrightError>`. |
| 31 | +- **Sync but Unsafe**: If a synchronous method performs validation or logic that explicitly throws errors. |
| 32 | + - **Mapping**: Map to `Effect<Return, PlaywrightError>` (using `Effect.sync` or `Effect.try`). |
| 33 | +- **Sync and Safe**: If the method purely returns a property or creates a new object without side effects or throwing (e.g., `url()`, `locator()`, `getByRole()`). |
| 34 | + - **Mapping**: **Map 1:1**. Return the value directly. Do NOT wrap in `Effect`. |
| 35 | + - **Preference**: Prefer 1:1 mappings over abstractions like getters. If Playwright uses a method `page.url()`, use `page.url()` in the effect wrapper, not a getter `page.url`. |
| 36 | + |
| 37 | +#### Return Type Mapping |
| 38 | + |
| 39 | +- **`Promise<T>`** -> `Effect<T, PlaywrightError>` |
| 40 | +- **`Promise<void>`** -> `Effect<void, PlaywrightError>` |
| 41 | +- **`T` (Safe Sync)** -> `T` (Direct return) |
| 42 | +- **`T` (Factory)** -> `Wrapper<T>` (e.g., `PlaywrightLocator.Service`) |
| 43 | +- **`T | null`** -> `Option<T>` (if sync) or `Effect<Option<T>, PlaywrightError>` (if async) |
| 44 | +- **Playwright Object (e.g., `Page`)** -> **Wrapped Object (e.g., `PlaywrightPage`)** |
| 45 | + |
| 46 | +### 3. Define the Interface |
| 47 | + |
| 48 | +Add the method to the Service interface in the corresponding `src/X.ts` file (e.g., `PlaywrightPageService` in `src/page.ts`). |
| 49 | + |
| 50 | +**Example (Async Method - Throws):** |
| 51 | + |
| 52 | +```typescript |
| 53 | +/** |
| 54 | + * Click the element. |
| 55 | + * @see {@link Page.click} |
| 56 | + */ |
| 57 | +readonly click: ( |
| 58 | + selector: string, |
| 59 | + options?: Parameters<Page["click"]>[1] |
| 60 | +) => Effect.Effect<void, PlaywrightError>; |
| 61 | +``` |
| 62 | + |
| 63 | +**Example (Sync Factory - Safe):** |
| 64 | + |
| 65 | +```typescript |
| 66 | +/** |
| 67 | + * Creates a locator. |
| 68 | + * @see {@link Page.locator} |
| 69 | + */ |
| 70 | +readonly locator: ( |
| 71 | + selector: string, |
| 72 | + options?: Parameters<Page["locator"]>[1] |
| 73 | +) => typeof PlaywrightLocator.Service; |
| 74 | +``` |
| 75 | + |
| 76 | +**Example (Sync Method - Safe):** |
| 77 | + |
| 78 | +```typescript |
| 79 | +/** |
| 80 | + * Returns the page URL. |
| 81 | + * @see {@link Page.url} |
| 82 | + */ |
| 83 | +readonly url: () => string; |
| 84 | +``` |
| 85 | + |
| 86 | +**Example (Nullable Return):** |
| 87 | + |
| 88 | +```typescript |
| 89 | +/** |
| 90 | + * Returns the text content (Async). |
| 91 | + * @see {@link Locator.textContent} |
| 92 | + */ |
| 93 | +readonly textContent: Effect.Effect<Option.Option<string>, PlaywrightError>; |
| 94 | +``` |
| 95 | + |
| 96 | +### 4. Implement the Method |
| 97 | + |
| 98 | +Implement the method in the `make` function of the implementation class (e.g., `PlaywrightPage.make`). |
| 99 | + |
| 100 | +- **Async Methods**: Use `useHelper(originalObject)`. |
| 101 | + |
| 102 | + ```typescript |
| 103 | + click: (selector, options) => use((p) => p.click(selector, options)), |
| 104 | + ``` |
| 105 | + |
| 106 | +- **Sync Safe Methods**: Return directly. |
| 107 | + |
| 108 | + ```typescript |
| 109 | + url: () => page.url(), |
| 110 | + ``` |
| 111 | + |
| 112 | +- **Factories**: Return the wrapped object directly. |
| 113 | + |
| 114 | + ```typescript |
| 115 | + locator: (selector, options) => PlaywrightLocator.make(page.locator(selector, options)), |
| 116 | + ``` |
| 117 | + |
| 118 | +- **Nullable Returns**: Use `Option.fromNullable`. |
| 119 | + |
| 120 | + ```typescript |
| 121 | + // Async nullable |
| 122 | + textContent: use((l) => l.textContent()).pipe( |
| 123 | + Effect.map(Option.fromNullable) |
| 124 | + ), |
| 125 | + |
| 126 | + // Sync nullable safe |
| 127 | + attribute: (name) => Option.fromNullable(element.getAttribute(name)), |
| 128 | + ``` |
| 129 | + |
| 130 | +- **Returning Playwright Objects**: Wrap them. |
| 131 | + ```typescript |
| 132 | + // Async returning object (e.g., waitForEvent returning a Page) |
| 133 | + waitForPopup: use((p) => p.waitForEvent("popup")).pipe( |
| 134 | + Effect.map(PlaywrightPage.make) |
| 135 | + ), |
| 136 | + ``` |
| 137 | + |
| 138 | +### 5. Verify |
| 139 | + |
| 140 | +- Ensure types match `PlaywrightXService`. |
| 141 | +- Run `npm run typecheck` (or equivalent) to verify implementation. |
0 commit comments