effect-playwright 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,167 +0,0 @@
1
- import { Context, Data, Effect, Layer, Stream } from "effect";
2
- import { chromium, errors } from "playwright-core";
3
-
4
- //#region src/errors.ts
5
- var PlaywrightError = class extends Data.TaggedError("PlaywrightError") {};
6
- function wrapError(error) {
7
- if (error instanceof errors.TimeoutError) return new PlaywrightError({
8
- reason: "Timeout",
9
- cause: error
10
- });
11
- else return new PlaywrightError({
12
- reason: "Unknown",
13
- cause: error
14
- });
15
- }
16
-
17
- //#endregion
18
- //#region src/utils.ts
19
- /** @internal */
20
- const useHelper = (api) => (userFunction) => Effect.tryPromise(() => userFunction(api)).pipe(Effect.mapError(wrapError));
21
-
22
- //#endregion
23
- //#region src/locator.ts
24
- /**
25
- * A service that provides a `PlaywrightLocator` instance.
26
- *
27
- * @since 0.1.0
28
- * @category tag
29
- */
30
- var PlaywrightLocator = class PlaywrightLocator extends Context.Tag("effect-playwright/PlaywrightLocator")() {
31
- /**
32
- * Creates a `PlaywrightLocator` from a Playwright `Locator` instance. This is mostly for internal use.
33
- * But you could use this if you have used `use` or similar to wrap the locator.
34
- *
35
- * @example
36
- * ```ts
37
- * const playwrightNativeLocator = yield* page.use((p) => p.locator("button"));
38
- * const locator = PlaywrightLocator.make(playwrightNativeLocator);
39
- * ```
40
- *
41
- * @param locator - The Playwright `Locator` instance to wrap.
42
- * @since 0.1.0
43
- * @category constructor
44
- */
45
- static make(locator) {
46
- const use = useHelper(locator);
47
- return PlaywrightLocator.of({
48
- click: (options) => use((l) => l.click(options)),
49
- fill: (value, options) => use((l) => l.fill(value, options)),
50
- getAttribute: (name, options) => use((l) => l.getAttribute(name, options)),
51
- innerText: (options) => use((l) => l.innerText(options)),
52
- innerHTML: (options) => use((l) => l.innerHTML(options)),
53
- inputValue: (options) => use((l) => l.inputValue(options)),
54
- textContent: (options) => use((l) => l.textContent(options)),
55
- count: use((l) => l.count()),
56
- first: () => PlaywrightLocator.make(locator.first()),
57
- last: () => PlaywrightLocator.make(locator.last()),
58
- nth: (index) => PlaywrightLocator.make(locator.nth(index)),
59
- evaluate: (pageFunction, arg, options) => use((l) => l.evaluate(pageFunction, arg, options)),
60
- use
61
- });
62
- }
63
- };
64
-
65
- //#endregion
66
- //#region src/page.ts
67
- /**
68
- * @category tag
69
- */
70
- var PlaywrightPage = class PlaywrightPage extends Context.Tag("effect-playwright/PlaywrightPage")() {
71
- /**
72
- * Creates a `PlaywrightPage` from a Playwright `Page` instance.
73
- *
74
- * @param page - The Playwright `Page` instance to wrap.
75
- * @since 0.1.0
76
- */
77
- static make(page) {
78
- const use = useHelper(page);
79
- return PlaywrightPage.of({
80
- goto: (url, options) => use((p) => p.goto(url, options)),
81
- waitForURL: (url, options) => use((p) => p.waitForURL(url, options)),
82
- title: use((p) => p.title()),
83
- evaluate: (f, arg) => use((p) => p.evaluate(f, arg)),
84
- locator: (selector, options) => PlaywrightLocator.make(page.locator(selector, options)),
85
- getByRole: (role, options) => PlaywrightLocator.make(page.getByRole(role, options)),
86
- getByText: (text, options) => PlaywrightLocator.make(page.getByText(text, options)),
87
- getByLabel: (label, options) => PlaywrightLocator.make(page.getByLabel(label, options)),
88
- getByTestId: (testId) => PlaywrightLocator.make(page.getByTestId(testId)),
89
- url: Effect.sync(() => page.url()),
90
- reload: use((p) => p.reload()),
91
- close: use((p) => p.close()),
92
- click: (selector, options) => use((p) => p.click(selector, options)),
93
- eventStream: (event) => Stream.asyncPush((emit) => Effect.acquireRelease(Effect.sync(() => page.on(event, emit.single)), () => Effect.sync(() => page.off(event, emit.single)))),
94
- use
95
- });
96
- }
97
- };
98
-
99
- //#endregion
100
- //#region src/browser-context.ts
101
- var PlaywrightBrowserContext = class PlaywrightBrowserContext extends Context.Tag("cehs/backend/lib/playwright/PlaywrightBrowserContext")() {
102
- static make(context) {
103
- const use = useHelper(context);
104
- return PlaywrightBrowserContext.of({
105
- pages: Effect.sync(() => context.pages().map(PlaywrightPage.make)),
106
- newPage: use((c) => c.newPage().then(PlaywrightPage.make)),
107
- close: use((c) => c.close())
108
- });
109
- }
110
- };
111
-
112
- //#endregion
113
- //#region src/browser.ts
114
- /**
115
- * @category tag
116
- */
117
- var PlaywrightBrowser = class PlaywrightBrowser extends Context.Tag("cehs/backend/lib/playwright/PlaywrightBrowser")() {
118
- /**
119
- * @category constructor
120
- */
121
- static make(browser) {
122
- const use = useHelper(browser);
123
- return PlaywrightBrowser.of({
124
- newPage: (options) => use((browser$1) => browser$1.newPage(options).then(PlaywrightPage.make)),
125
- close: use((browser$1) => browser$1.close()),
126
- contexts: Effect.sync(() => browser.contexts().map(PlaywrightBrowserContext.make)),
127
- newContext: (options) => Effect.acquireRelease(use((browser$1) => browser$1.newContext(options).then(PlaywrightBrowserContext.make)), (context) => context.close.pipe(Effect.ignoreLogged)),
128
- browserType: Effect.sync(() => browser.browserType()),
129
- version: Effect.sync(() => browser.version()),
130
- isConnected: Effect.sync(() => browser.isConnected()),
131
- use
132
- });
133
- }
134
- };
135
-
136
- //#endregion
137
- //#region src/playwright.ts
138
- const launch = Effect.fn(function* (browserType, options) {
139
- const rawBrowser = yield* Effect.tryPromise({
140
- try: () => browserType.launch(options),
141
- catch: wrapError
142
- });
143
- return PlaywrightBrowser.make(rawBrowser);
144
- });
145
- var Playwright = class Playwright extends Context.Tag("effect-playwright/index/Playwright")() {
146
- /**
147
- * @category layer
148
- */
149
- static layer = Layer.succeed(Playwright, {
150
- launch,
151
- launchScoped: Effect.fn(function* (browserType, options) {
152
- const browser = yield* launch(browserType, options);
153
- yield* Effect.addFinalizer(() => browser.close.pipe(Effect.ignore));
154
- return browser;
155
- }),
156
- connectCDP: Effect.fn(function* (cdpUrl, options) {
157
- const browser = yield* Effect.tryPromise({
158
- try: () => chromium.connectOverCDP(cdpUrl, options),
159
- catch: wrapError
160
- });
161
- return PlaywrightBrowser.make(browser);
162
- })
163
- });
164
- };
165
-
166
- //#endregion
167
- export { PlaywrightLocator as i, PlaywrightBrowser as n, PlaywrightPage as r, Playwright as t };