|
| 1 | +import { Context, Effect } from "effect"; |
| 2 | +import type { Frame } from "playwright-core"; |
| 3 | +import type { PlaywrightError } from "./errors"; |
| 4 | +import { PlaywrightLocator } from "./locator"; |
| 5 | +import type { PageFunction } from "./playwright-types"; |
| 6 | +import { useHelper } from "./utils"; |
| 7 | + |
| 8 | +/** |
| 9 | + * @category model |
| 10 | + * @since 0.1.2 |
| 11 | + */ |
| 12 | +export interface PlaywrightFrameService { |
| 13 | + /** |
| 14 | + * Navigates the frame to the given URL. |
| 15 | + * |
| 16 | + * @see {@link Frame.goto} |
| 17 | + * @since 0.1.3 |
| 18 | + */ |
| 19 | + readonly goto: ( |
| 20 | + url: string, |
| 21 | + options?: Parameters<Frame["goto"]>[1], |
| 22 | + ) => Effect.Effect<void, PlaywrightError>; |
| 23 | + /** |
| 24 | + * Waits for the frame to navigate to the given URL. |
| 25 | + * |
| 26 | + * @see {@link Frame.waitForURL} |
| 27 | + * @since 0.1.3 |
| 28 | + */ |
| 29 | + readonly waitForURL: ( |
| 30 | + url: Parameters<Frame["waitForURL"]>[0], |
| 31 | + options?: Parameters<Frame["waitForURL"]>[1], |
| 32 | + ) => Effect.Effect<void, PlaywrightError>; |
| 33 | + /** |
| 34 | + * Evaluates a function in the context of the frame. |
| 35 | + * |
| 36 | + * @see {@link Frame.evaluate} |
| 37 | + * @since 0.1.3 |
| 38 | + */ |
| 39 | + readonly evaluate: <R, Arg = void>( |
| 40 | + pageFunction: PageFunction<Arg, R>, |
| 41 | + arg?: Arg, |
| 42 | + ) => Effect.Effect<R, PlaywrightError>; |
| 43 | + /** |
| 44 | + * Returns the frame title. |
| 45 | + * |
| 46 | + * @see {@link Frame.title} |
| 47 | + * @since 0.1.3 |
| 48 | + */ |
| 49 | + readonly title: Effect.Effect<string, PlaywrightError>; |
| 50 | + /** |
| 51 | + * A generic utility to execute any promise-based method on the underlying Playwright `Frame`. |
| 52 | + * Can be used to access any Frame functionality not directly exposed by this service. |
| 53 | + * |
| 54 | + * @see {@link Frame} |
| 55 | + * @since 0.1.2 |
| 56 | + */ |
| 57 | + readonly use: <T>( |
| 58 | + f: (frame: Frame) => Promise<T>, |
| 59 | + ) => Effect.Effect<T, PlaywrightError>; |
| 60 | + /** |
| 61 | + * Returns a locator for the given selector. |
| 62 | + * |
| 63 | + * @see {@link Frame.locator} |
| 64 | + * @since 0.1.3 |
| 65 | + */ |
| 66 | + readonly locator: ( |
| 67 | + selector: string, |
| 68 | + options?: Parameters<Frame["locator"]>[1], |
| 69 | + ) => typeof PlaywrightLocator.Service; |
| 70 | + /** |
| 71 | + * Returns a locator that matches the given role. |
| 72 | + * |
| 73 | + * @see {@link Frame.getByRole} |
| 74 | + * @since 0.1.3 |
| 75 | + */ |
| 76 | + readonly getByRole: ( |
| 77 | + role: Parameters<Frame["getByRole"]>[0], |
| 78 | + options?: Parameters<Frame["getByRole"]>[1], |
| 79 | + ) => typeof PlaywrightLocator.Service; |
| 80 | + /** |
| 81 | + * Returns a locator that matches the given text. |
| 82 | + * |
| 83 | + * @see {@link Frame.getByText} |
| 84 | + * @since 0.1.3 |
| 85 | + */ |
| 86 | + readonly getByText: ( |
| 87 | + text: Parameters<Frame["getByText"]>[0], |
| 88 | + options?: Parameters<Frame["getByText"]>[1], |
| 89 | + ) => typeof PlaywrightLocator.Service; |
| 90 | + /** |
| 91 | + * Returns a locator that matches the given label. |
| 92 | + * |
| 93 | + * @see {@link Frame.getByLabel} |
| 94 | + * @since 0.1.3 |
| 95 | + */ |
| 96 | + readonly getByLabel: ( |
| 97 | + label: Parameters<Frame["getByLabel"]>[0], |
| 98 | + options?: Parameters<Frame["getByLabel"]>[1], |
| 99 | + ) => typeof PlaywrightLocator.Service; |
| 100 | + /** |
| 101 | + * Returns a locator that matches the given test id. |
| 102 | + * |
| 103 | + * @see {@link Frame.getByTestId} |
| 104 | + * @since 0.1.3 |
| 105 | + */ |
| 106 | + readonly getByTestId: ( |
| 107 | + testId: Parameters<Frame["getByTestId"]>[0], |
| 108 | + ) => typeof PlaywrightLocator.Service; |
| 109 | + |
| 110 | + /** |
| 111 | + * Returns the current URL of the frame. |
| 112 | + * |
| 113 | + * @see {@link Frame.url} |
| 114 | + * @since 0.1.3 |
| 115 | + */ |
| 116 | + readonly url: Effect.Effect<string, PlaywrightError>; |
| 117 | + |
| 118 | + /** |
| 119 | + * Returns the full HTML contents of the frame, including the doctype. |
| 120 | + * |
| 121 | + * @see {@link Frame.content} |
| 122 | + * @since 0.1.3 |
| 123 | + */ |
| 124 | + readonly content: Effect.Effect<string, PlaywrightError>; |
| 125 | + |
| 126 | + /** |
| 127 | + * Returns the frame name. |
| 128 | + * |
| 129 | + * @see {@link Frame.name} |
| 130 | + * @since 0.1.3 |
| 131 | + */ |
| 132 | + readonly name: Effect.Effect<string>; |
| 133 | + |
| 134 | + /** |
| 135 | + * Clicks an element matching the given selector. |
| 136 | + * |
| 137 | + * @deprecated Use {@link PlaywrightFrameService.locator} to create a locator and then call `click` on it instead. |
| 138 | + * @see {@link Frame.click} |
| 139 | + * @since 0.1.3 |
| 140 | + * @category deprecated |
| 141 | + */ |
| 142 | + readonly click: ( |
| 143 | + selector: string, |
| 144 | + options?: Parameters<Frame["click"]>[1], |
| 145 | + ) => Effect.Effect<void, PlaywrightError>; |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * @category tag |
| 150 | + * @since 0.1.2 |
| 151 | + */ |
| 152 | +export class PlaywrightFrame extends Context.Tag( |
| 153 | + "effect-playwright/PlaywrightFrame", |
| 154 | +)<PlaywrightFrame, PlaywrightFrameService>() { |
| 155 | + /** |
| 156 | + * Creates a `PlaywrightFrame` from a Playwright `Frame` instance. |
| 157 | + * |
| 158 | + * @param frame - The Playwright `Frame` instance to wrap. |
| 159 | + * @since 0.1.2 |
| 160 | + */ |
| 161 | + static make(frame: Frame): PlaywrightFrameService { |
| 162 | + const use = useHelper(frame); |
| 163 | + |
| 164 | + return PlaywrightFrame.of({ |
| 165 | + goto: (url, options) => use((f) => f.goto(url, options)), |
| 166 | + waitForURL: (url, options) => use((f) => f.waitForURL(url, options)), |
| 167 | + evaluate: <R, Arg>(f: PageFunction<Arg, R>, arg?: Arg) => |
| 168 | + use((frame) => frame.evaluate<R, Arg>(f, arg as Arg)), |
| 169 | + title: use((f) => f.title()), |
| 170 | + use, |
| 171 | + locator: (selector, options) => |
| 172 | + PlaywrightLocator.make(frame.locator(selector, options)), |
| 173 | + getByRole: (role, options) => |
| 174 | + PlaywrightLocator.make(frame.getByRole(role, options)), |
| 175 | + getByText: (text, options) => |
| 176 | + PlaywrightLocator.make(frame.getByText(text, options)), |
| 177 | + getByLabel: (label, options) => |
| 178 | + PlaywrightLocator.make(frame.getByLabel(label, options)), |
| 179 | + getByTestId: (testId) => |
| 180 | + PlaywrightLocator.make(frame.getByTestId(testId)), |
| 181 | + url: Effect.sync(() => frame.url()), |
| 182 | + content: use((f) => f.content()), |
| 183 | + name: Effect.sync(() => frame.name()), |
| 184 | + click: (selector, options) => use((f) => f.click(selector, options)), |
| 185 | + }); |
| 186 | + } |
| 187 | +} |
0 commit comments