-
Notifications
You must be signed in to change notification settings - Fork 2
implement frame service & methods #6
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { assert, layer } from "@effect/vitest"; | ||
| import { Effect } from "effect"; | ||
| import { chromium } from "playwright-core"; | ||
| import { PlaywrightBrowser } from "./browser"; | ||
| import { PlaywrightEnvironment } from "./experimental"; | ||
| import { PlaywrightFrame } from "./frame"; | ||
|
|
||
| layer(PlaywrightEnvironment.layer(chromium))("PlaywrightFrame", (it) => { | ||
| it.scoped("should wrap frame methods", () => | ||
| Effect.gen(function* () { | ||
| const browser = yield* PlaywrightBrowser; | ||
| const page = yield* browser.newPage(); | ||
|
|
||
| // Setup a page with an iframe | ||
| yield* page.evaluate(() => { | ||
| const iframe = document.createElement("iframe"); | ||
| iframe.name = "test-frame"; | ||
| iframe.srcdoc = | ||
| "<html><head><title>Frame Title</title></head><body><div id='target'>Hello from Frame</div></body></html>"; | ||
| document.body.appendChild(iframe); | ||
| }); | ||
|
|
||
| // Get the frame | ||
| const frameService = yield* page.use(async (p) => { | ||
| let frame = p.frames().find((f) => f.name() === "test-frame"); | ||
| if (!frame) { | ||
| // Wait a bit if not found immediately (though srcdoc is sync-ish, iframe loading is async) | ||
| await p.waitForLoadState("networkidle"); | ||
| frame = p.frames().find((f) => f.name() === "test-frame"); | ||
| } | ||
| if (!frame) throw new Error("Frame not found"); | ||
| return PlaywrightFrame.make(frame); | ||
| }); | ||
|
|
||
| // Test title | ||
| const title = yield* frameService.title; | ||
| assert.strictEqual(title, "Frame Title"); | ||
|
|
||
| // Test content | ||
| const content = yield* frameService.content; | ||
| assert.isTrue(content.includes("Hello from Frame")); | ||
|
|
||
| // Test evaluate | ||
| const result = yield* frameService.evaluate(() => 1 + 1); | ||
| assert.strictEqual(result, 2); | ||
|
|
||
| // Test locator | ||
| const text = yield* frameService.locator("#target").textContent(); | ||
| assert.strictEqual(text, "Hello from Frame"); | ||
|
|
||
| // Test getByText | ||
| const byText = yield* frameService.getByText("Hello from Frame").count; | ||
| assert.strictEqual(byText, 1); | ||
|
|
||
| // Test name | ||
| const name = yield* frameService.name; | ||
| assert.strictEqual(name, "test-frame"); | ||
| }).pipe(PlaywrightEnvironment.withBrowser), | ||
| ); | ||
| }); |
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,187 @@ | ||
| import { Context, Effect } from "effect"; | ||
| import type { Frame } from "playwright-core"; | ||
| import type { PlaywrightError } from "./errors"; | ||
| import { PlaywrightLocator } from "./locator"; | ||
| import type { PageFunction } from "./playwright-types"; | ||
| import { useHelper } from "./utils"; | ||
|
|
||
| /** | ||
| * @category model | ||
| * @since 0.1.2 | ||
| */ | ||
| export interface PlaywrightFrameService { | ||
| /** | ||
| * Navigates the frame to the given URL. | ||
| * | ||
| * @see {@link Frame.goto} | ||
| * @since 0.1.3 | ||
| */ | ||
| readonly goto: ( | ||
| url: string, | ||
| options?: Parameters<Frame["goto"]>[1], | ||
| ) => Effect.Effect<void, PlaywrightError>; | ||
| /** | ||
| * Waits for the frame to navigate to the given URL. | ||
| * | ||
| * @see {@link Frame.waitForURL} | ||
| * @since 0.1.3 | ||
| */ | ||
| readonly waitForURL: ( | ||
| url: Parameters<Frame["waitForURL"]>[0], | ||
| options?: Parameters<Frame["waitForURL"]>[1], | ||
| ) => Effect.Effect<void, PlaywrightError>; | ||
| /** | ||
| * Evaluates a function in the context of the frame. | ||
| * | ||
| * @see {@link Frame.evaluate} | ||
| * @since 0.1.3 | ||
| */ | ||
| readonly evaluate: <R, Arg = void>( | ||
| pageFunction: PageFunction<Arg, R>, | ||
| arg?: Arg, | ||
| ) => Effect.Effect<R, PlaywrightError>; | ||
| /** | ||
| * Returns the frame title. | ||
| * | ||
| * @see {@link Frame.title} | ||
| * @since 0.1.3 | ||
| */ | ||
| readonly title: Effect.Effect<string, PlaywrightError>; | ||
| /** | ||
| * A generic utility to execute any promise-based method on the underlying Playwright `Frame`. | ||
| * Can be used to access any Frame functionality not directly exposed by this service. | ||
| * | ||
| * @see {@link Frame} | ||
| * @since 0.1.2 | ||
| */ | ||
| readonly use: <T>( | ||
| f: (frame: Frame) => Promise<T>, | ||
| ) => Effect.Effect<T, PlaywrightError>; | ||
| /** | ||
| * Returns a locator for the given selector. | ||
| * | ||
| * @see {@link Frame.locator} | ||
| * @since 0.1.3 | ||
| */ | ||
| readonly locator: ( | ||
| selector: string, | ||
| options?: Parameters<Frame["locator"]>[1], | ||
| ) => typeof PlaywrightLocator.Service; | ||
| /** | ||
| * Returns a locator that matches the given role. | ||
| * | ||
| * @see {@link Frame.getByRole} | ||
| * @since 0.1.3 | ||
| */ | ||
| readonly getByRole: ( | ||
| role: Parameters<Frame["getByRole"]>[0], | ||
| options?: Parameters<Frame["getByRole"]>[1], | ||
| ) => typeof PlaywrightLocator.Service; | ||
| /** | ||
| * Returns a locator that matches the given text. | ||
| * | ||
| * @see {@link Frame.getByText} | ||
| * @since 0.1.3 | ||
| */ | ||
| readonly getByText: ( | ||
| text: Parameters<Frame["getByText"]>[0], | ||
| options?: Parameters<Frame["getByText"]>[1], | ||
| ) => typeof PlaywrightLocator.Service; | ||
| /** | ||
| * Returns a locator that matches the given label. | ||
| * | ||
| * @see {@link Frame.getByLabel} | ||
| * @since 0.1.3 | ||
| */ | ||
| readonly getByLabel: ( | ||
| label: Parameters<Frame["getByLabel"]>[0], | ||
| options?: Parameters<Frame["getByLabel"]>[1], | ||
| ) => typeof PlaywrightLocator.Service; | ||
| /** | ||
| * Returns a locator that matches the given test id. | ||
| * | ||
| * @see {@link Frame.getByTestId} | ||
| * @since 0.1.3 | ||
| */ | ||
| readonly getByTestId: ( | ||
| testId: Parameters<Frame["getByTestId"]>[0], | ||
| ) => typeof PlaywrightLocator.Service; | ||
|
|
||
| /** | ||
| * Returns the current URL of the frame. | ||
| * | ||
| * @see {@link Frame.url} | ||
| * @since 0.1.3 | ||
| */ | ||
| readonly url: Effect.Effect<string, PlaywrightError>; | ||
|
|
||
| /** | ||
| * Returns the full HTML contents of the frame, including the doctype. | ||
| * | ||
| * @see {@link Frame.content} | ||
| * @since 0.1.3 | ||
| */ | ||
| readonly content: Effect.Effect<string, PlaywrightError>; | ||
|
|
||
| /** | ||
| * Returns the frame name. | ||
| * | ||
| * @see {@link Frame.name} | ||
| * @since 0.1.3 | ||
| */ | ||
| readonly name: Effect.Effect<string>; | ||
|
|
||
| /** | ||
| * Clicks an element matching the given selector. | ||
| * | ||
| * @deprecated Use {@link PlaywrightFrameService.locator} to create a locator and then call `click` on it instead. | ||
| * @see {@link Frame.click} | ||
| * @since 0.1.3 | ||
| * @category deprecated | ||
| */ | ||
| readonly click: ( | ||
| selector: string, | ||
| options?: Parameters<Frame["click"]>[1], | ||
| ) => Effect.Effect<void, PlaywrightError>; | ||
| } | ||
|
|
||
| /** | ||
| * @category tag | ||
| * @since 0.1.2 | ||
| */ | ||
| export class PlaywrightFrame extends Context.Tag( | ||
| "effect-playwright/PlaywrightFrame", | ||
| )<PlaywrightFrame, PlaywrightFrameService>() { | ||
| /** | ||
| * Creates a `PlaywrightFrame` from a Playwright `Frame` instance. | ||
| * | ||
| * @param frame - The Playwright `Frame` instance to wrap. | ||
| * @since 0.1.2 | ||
| */ | ||
| static make(frame: Frame): PlaywrightFrameService { | ||
| const use = useHelper(frame); | ||
|
|
||
| return PlaywrightFrame.of({ | ||
| goto: (url, options) => use((f) => f.goto(url, options)), | ||
| waitForURL: (url, options) => use((f) => f.waitForURL(url, options)), | ||
| evaluate: <R, Arg>(f: PageFunction<Arg, R>, arg?: Arg) => | ||
| use((frame) => frame.evaluate<R, Arg>(f, arg as Arg)), | ||
| title: use((f) => f.title()), | ||
| use, | ||
| locator: (selector, options) => | ||
| PlaywrightLocator.make(frame.locator(selector, options)), | ||
| getByRole: (role, options) => | ||
| PlaywrightLocator.make(frame.getByRole(role, options)), | ||
| getByText: (text, options) => | ||
| PlaywrightLocator.make(frame.getByText(text, options)), | ||
| getByLabel: (label, options) => | ||
| PlaywrightLocator.make(frame.getByLabel(label, options)), | ||
| getByTestId: (testId) => | ||
| PlaywrightLocator.make(frame.getByTestId(testId)), | ||
| url: Effect.sync(() => frame.url()), | ||
| content: use((f) => f.content()), | ||
| name: Effect.sync(() => frame.name()), | ||
| click: (selector, options) => use((f) => f.click(selector, options)), | ||
| }); | ||
| } | ||
| } | ||
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.