@itmetsam/samurai-framework 0.22.10 → 0.22.11
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.
- package/dist/browser/page-actions.d.ts +151 -0
- package/dist/browser/page-actions.js +197 -0
- package/dist/browser/page-core.d.ts +80 -0
- package/dist/browser/page-core.js +150 -0
- package/dist/browser/page-locating.d.ts +37 -0
- package/dist/browser/page-locating.js +58 -0
- package/dist/browser/page-observing.d.ts +120 -0
- package/dist/browser/page-observing.js +194 -0
- package/dist/browser/page.d.ts +7 -358
- package/dist/browser/page.js +2 -582
- package/dist/locator/locator-element-methods.d.ts +169 -0
- package/dist/locator/locator-element-methods.js +228 -0
- package/dist/locator/locator.d.ts +7 -161
- package/dist/locator/locator.js +4 -223
- package/package.json +1 -1
package/dist/browser/page.js
CHANGED
|
@@ -1,143 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import EventEmitter from "node:events";
|
|
4
|
-
import logger from "../logger/index.js";
|
|
5
|
-
import { ContextTree } from "./context-tree.js";
|
|
6
|
-
import { printPdf } from "./pdf.js";
|
|
7
|
-
import { applyUse } from "./use-commands.js";
|
|
8
|
-
import { screenshotWithEvidence, } from "./screenshot.js";
|
|
9
|
-
import { PageLogging } from "./page-logging.js";
|
|
10
|
-
import { Lazy } from "./lazy.js";
|
|
11
|
-
import { Response } from "../network/response.js";
|
|
12
|
-
import { traverseHistory } from "./history.js";
|
|
13
|
-
import { DialogHandling } from "./dialog-handling.js";
|
|
14
|
-
import { DocumentWatcher } from "./document-watcher.js";
|
|
15
|
-
import { PageNavigation } from "./page-navigation.js";
|
|
16
|
-
import { PageNetwork } from "./page-network.js";
|
|
17
|
-
import { PageTransfers } from "./page-transfers.js";
|
|
18
|
-
import { InitScripts } from "./init-script.js";
|
|
19
|
-
import { grantPermission } from "./page-permissions.js";
|
|
20
|
-
import { PageLocators } from "./page-locators.js";
|
|
21
|
-
import Keyboard from "./keyboard.js";
|
|
22
|
-
import Mouse from "./mouse.js";
|
|
23
|
-
import { startSources, waitForEventOn, waitForPageEvent, } from "./page-events.js";
|
|
24
|
-
import { PageConfig, } from "./page-settings.js";
|
|
25
|
-
import { PageViewport } from "./page-viewport.js";
|
|
26
|
-
import { HelperRealm } from "../locator/helper-realm.js";
|
|
27
|
-
import { BiDiError } from "../transport/bidi-error.js";
|
|
28
|
-
import { readContent, readTitle, readUrl } from "./page-content.js";
|
|
29
|
-
import { evaluateInPage } from "./page-evaluate.js";
|
|
30
|
-
export default class Page {
|
|
31
|
-
/** `settings` override what `samurai.config.ts` says; without them the config is read when needed */
|
|
32
|
-
constructor(biDiConnector, contextId, settings) {
|
|
33
|
-
this.lazyTree = new Lazy();
|
|
34
|
-
this.initScripts = new InitScripts();
|
|
35
|
-
this.pageEvents = new EventEmitter();
|
|
36
|
-
this.isClosed = false;
|
|
37
|
-
this.biDiConnector = biDiConnector;
|
|
38
|
-
this.id = contextId;
|
|
39
|
-
this.pageConfig = new PageConfig(settings);
|
|
40
|
-
this.helperRealm = new HelperRealm(biDiConnector, contextId);
|
|
41
|
-
this.locators = new PageLocators(biDiConnector, contextId, this.helperRealm, this.pageConfig, () => this.assertOpen());
|
|
42
|
-
this.keyboard = new Keyboard(biDiConnector, contextId, () => this.assertOpen());
|
|
43
|
-
this.mouse = new Mouse(biDiConnector, contextId, () => this.assertOpen());
|
|
44
|
-
this.documents = new DocumentWatcher(biDiConnector, contextId);
|
|
45
|
-
this.viewport = new PageViewport(biDiConnector, contextId, this.pageConfig);
|
|
46
|
-
this.logging = new PageLogging(biDiConnector, contextId, () => this.tree(), this.pageEvents);
|
|
47
|
-
this.net = new PageNetwork(biDiConnector, contextId, {
|
|
48
|
-
config: this.pageConfig,
|
|
49
|
-
tree: { ensure: () => this.tree(), peek: () => this.lazyTree.peek() },
|
|
50
|
-
emitter: this.pageEvents,
|
|
51
|
-
waitForEvent: (event, matches, options, onTimeout) => this.waitForPageEvent(event, matches, options, onTimeout),
|
|
52
|
-
});
|
|
53
|
-
this.nav = new PageNavigation(biDiConnector, contextId, {
|
|
54
|
-
config: this.pageConfig,
|
|
55
|
-
documents: this.documents,
|
|
56
|
-
assertOpen: () => this.assertOpen(),
|
|
57
|
-
url: () => this.url(),
|
|
58
|
-
waitForNetworkIdle: (options) => this.net.waitForIdle(options),
|
|
59
|
-
});
|
|
60
|
-
this.transfers = new PageTransfers(biDiConnector, contextId, {
|
|
61
|
-
config: this.pageConfig,
|
|
62
|
-
tree: { ensure: () => this.tree(), peek: () => this.lazyTree.peek() },
|
|
63
|
-
emitter: this.pageEvents,
|
|
64
|
-
waitForEvent: (event, matches, options, onTimeout) => this.waitForPageEvent(event, matches, options, onTimeout),
|
|
65
|
-
});
|
|
66
|
-
this.dialogs = new DialogHandling(biDiConnector, contextId, { ensure: () => this.tree(), peek: () => this.lazyTree.peek() }, this.pageEvents);
|
|
67
|
-
}
|
|
68
|
-
/** The BiDi browsing context id of this page's top-level context */
|
|
69
|
-
get contextId() {
|
|
70
|
-
return this.id;
|
|
71
|
-
}
|
|
72
|
-
/** The context tree, created on first use and shared by everything that needs to attribute frame events to this page */
|
|
73
|
-
tree() {
|
|
74
|
-
return this.lazyTree.get(() => ContextTree.create(this.biDiConnector));
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Locates elements on the page. An xpath (`//x`, `/html/body`, `(//x)[2]`, `./x`, `xpath=…`, or an older
|
|
78
|
-
* relative one such as `a[@id='x']`) or a css selector (`input[name='x']`, `a, button`, `css=…`).
|
|
79
|
-
* `options` filter the matches, as `locator.filter()` does
|
|
80
|
-
*/
|
|
81
|
-
locator(selector, options) {
|
|
82
|
-
const found = this.locators.locate(selector);
|
|
83
|
-
return options === undefined ? found : found.filter(options);
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Makes this page's locators strict (or not): an action or read on a locator matching several elements then
|
|
87
|
-
* throws `name(): strict mode violation, N elements match ...` instead of using the first. `undefined` goes back
|
|
88
|
-
* to config `use.strictSelectors` (off unless set). Applies to locators made afterwards and to earlier ones that have not
|
|
89
|
-
* run an action or read yet: a locator keeps the answer it got at its first one
|
|
90
|
-
*/
|
|
91
|
-
setStrictSelectors(enabled) {
|
|
92
|
-
this.locators.setStrictSelectors(enabled);
|
|
93
|
-
}
|
|
94
|
-
/** Locates elements with a CSS selector */
|
|
95
|
-
getByCss(css) {
|
|
96
|
-
return this.locators.locate([cssSelector(css)]);
|
|
97
|
-
}
|
|
98
|
-
/** Locates elements by their text: an exact match unless `match: "partial"` or `exact: false`; a RegExp is tested as written (see `TextOptions`) */
|
|
99
|
-
getByText(text, options) {
|
|
100
|
-
return this.locators.locate([textSelector(text, options)]);
|
|
101
|
-
}
|
|
102
|
-
/** Locates form controls by their label text: an exact match unless `match: "partial"` or `exact: false`; a RegExp is tested as written */
|
|
103
|
-
getByLabel(text, options) {
|
|
104
|
-
return this.locators.locate([labelSelector(text, options)]);
|
|
105
|
-
}
|
|
106
|
-
/** Locates elements by their `placeholder` (see `TextOptions` for `exact` and RegExp) */
|
|
107
|
-
getByPlaceholder(text, options) {
|
|
108
|
-
return this.locators.locate([
|
|
109
|
-
attributeSelector("placeholder", text, options),
|
|
110
|
-
]);
|
|
111
|
-
}
|
|
112
|
-
/** Locates elements by their `alt` text (images, areas, image inputs) */
|
|
113
|
-
getByAltText(text, options) {
|
|
114
|
-
return this.locators.locate([attributeSelector("alt", text, options)]);
|
|
115
|
-
}
|
|
116
|
-
/** Locates elements by their `title` attribute */
|
|
117
|
-
getByTitle(text, options) {
|
|
118
|
-
return this.locators.locate([attributeSelector("title", text, options)]);
|
|
119
|
-
}
|
|
120
|
-
/** Locates elements by their `data-testid` attribute */
|
|
121
|
-
getByTestId(testId) {
|
|
122
|
-
return this.locators.locate([testIdSelector(testId)]);
|
|
123
|
-
}
|
|
124
|
-
/** Locates elements by ARIA role and, optionally, accessible name and state (see `RoleOptions`) */
|
|
125
|
-
getByRole(role, options) {
|
|
126
|
-
return this.locators.locate([roleSelector(role, options)]);
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Runs `script` before any page script in every document this page loads from now on, and once in
|
|
130
|
-
* the document that is loaded now. It runs in the page's own realm, so the page sees what it sets.
|
|
131
|
-
* A function receives `arg` (which must be JSON-serializable) as its only argument;
|
|
132
|
-
* a string is script source (statements) and takes no argument.
|
|
133
|
-
* @example
|
|
134
|
-
* await page.addInitScript(() => { window.__testMode = true; });
|
|
135
|
-
* await page.addInitScript((flags) => { window.flags = flags; }, { beta: true });
|
|
136
|
-
*/
|
|
137
|
-
async addInitScript(script, arg) {
|
|
138
|
-
this.assertOpen();
|
|
139
|
-
return this.initScripts.add(this.biDiConnector, this.id, script, arg);
|
|
140
|
-
}
|
|
1
|
+
import { PageObserving } from "./page-observing.js";
|
|
2
|
+
export default class Page extends PageObserving {
|
|
141
3
|
/**
|
|
142
4
|
* Starts recording what a person does in the page as steps (see `Recorder`). The page must be driven by
|
|
143
5
|
* a person; stop with `recorder.stop()`.
|
|
@@ -148,151 +10,6 @@ export default class Page {
|
|
|
148
10
|
const { Recorder } = await import("../recorder/recorder.js");
|
|
149
11
|
return Recorder.start({ page: this, connector: this.biDiConnector, context: this.id }, options);
|
|
150
12
|
}
|
|
151
|
-
/** Removes everything the page registered in the browser: init scripts, framework helpers and event subscriptions */
|
|
152
|
-
async dispose() {
|
|
153
|
-
const scripts = this.initScripts.all();
|
|
154
|
-
const results = await Promise.allSettled([
|
|
155
|
-
...scripts.map((handle) => handle.dispose()),
|
|
156
|
-
this.helperRealm.dispose(),
|
|
157
|
-
// The tracker goes before the tree it reads
|
|
158
|
-
this.net.disposeTracker(),
|
|
159
|
-
]);
|
|
160
|
-
const tree = this.lazyTree.take();
|
|
161
|
-
const pageLogs = this.logging.take();
|
|
162
|
-
results.push(...(await Promise.allSettled([
|
|
163
|
-
tree?.then((resolved) => resolved.dispose()),
|
|
164
|
-
this.documents.dispose(),
|
|
165
|
-
this.dialogs.dispose(),
|
|
166
|
-
pageLogs?.then((logs) => logs.dispose()),
|
|
167
|
-
...this.net.disposeRest(),
|
|
168
|
-
...this.transfers.dispose(),
|
|
169
|
-
])));
|
|
170
|
-
const failed = results.find((result) => result.status === "rejected");
|
|
171
|
-
if (failed)
|
|
172
|
-
throw failed.reason;
|
|
173
|
-
}
|
|
174
|
-
/** Sets the base that relative `goto` URLs resolve against; `undefined` falls back to the run's baseURL */
|
|
175
|
-
setBaseURL(url) {
|
|
176
|
-
this.nav.setBaseURL(url);
|
|
177
|
-
}
|
|
178
|
-
/** @internal The full URL `goto` would navigate to: a URL starting with "/", "./" or "../" resolves against the base URL, others stay as given. `method` names the caller in the error */
|
|
179
|
-
resolveURL(url, method) {
|
|
180
|
-
return this.nav.resolveURL(url, method);
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Navigates like `navigateTo`, but a URL starting with "/", "./" or "../" resolves against the base URL
|
|
184
|
-
* (`setBaseURL`, else the environment's `baseURL`).
|
|
185
|
-
* @example
|
|
186
|
-
* await page.goto("/products");
|
|
187
|
-
* await page.goto("https://example.com", { wait: "interactive" });
|
|
188
|
-
*/
|
|
189
|
-
goto(url, options) {
|
|
190
|
-
return this.nav.goto(url, options);
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Navigates to `url` and resolves when the page reached `wait` (default "complete", the load event).
|
|
194
|
-
* A URL without a scheme gets `https://`. Throws NavigationError when the navigation fails or takes too long.
|
|
195
|
-
* The positional form `navigateTo(url, wait, protocol)` still works.
|
|
196
|
-
* @example
|
|
197
|
-
* await page.navigateTo("example.com");
|
|
198
|
-
* await page.navigateTo("localhost:3000", { wait: "interactive", protocol: "http" });
|
|
199
|
-
*/
|
|
200
|
-
navigateTo(url, options, protocol) {
|
|
201
|
-
return this.nav.navigateTo(url, options, protocol);
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Reloads the page and resolves when it reached `wait` (default like `navigateTo`). Firefox does not support
|
|
205
|
-
* `ignoreCache`; asking for it throws UnsupportedOperationError.
|
|
206
|
-
*/
|
|
207
|
-
reload(options = {}) {
|
|
208
|
-
return this.nav.reload(options);
|
|
209
|
-
}
|
|
210
|
-
/** Goes back one entry in the history. False when there is none to go back to */
|
|
211
|
-
async goBack() {
|
|
212
|
-
this.assertOpen();
|
|
213
|
-
return traverseHistory(this.biDiConnector, this.id, -1);
|
|
214
|
-
}
|
|
215
|
-
/** Goes forward one entry in the history. False when there is none */
|
|
216
|
-
async goForward() {
|
|
217
|
-
this.assertOpen();
|
|
218
|
-
return traverseHistory(this.biDiConnector, this.id, 1);
|
|
219
|
-
}
|
|
220
|
-
/** The URL of the page now */
|
|
221
|
-
async url() {
|
|
222
|
-
this.assertOpen();
|
|
223
|
-
return readUrl(this.biDiConnector, this.id);
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Waits until the URL matches `url` (a string equals it, or with `*` / `**` is a glob; a RegExp is tested on it;
|
|
227
|
-
* a function gets it as a `URL`), then until the page reached `waitUntil` (default "load"). Also sees `pushState`.
|
|
228
|
-
* @example
|
|
229
|
-
* await page.getByRole("button", { name: "Sign in" }).click();
|
|
230
|
-
* await page.waitForURL("**\/dashboard");
|
|
231
|
-
*/
|
|
232
|
-
waitForURL(url, options) {
|
|
233
|
-
return this.nav.waitForURL(url, options);
|
|
234
|
-
}
|
|
235
|
-
/** The markup of the document now, doctype included */
|
|
236
|
-
async content() {
|
|
237
|
-
this.assertOpen();
|
|
238
|
-
return readContent(this.biDiConnector, this.id);
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Replaces the document with `html` and waits for `waitUntil` (default "load"). Scripts and styles in it run and apply;
|
|
242
|
-
* the page keeps its URL and origin (it is not a `data:` page).
|
|
243
|
-
* @example
|
|
244
|
-
* await page.setContent("<button>Go</button><script>document.title = 'x'</script>");
|
|
245
|
-
*/
|
|
246
|
-
async setContent(html, options) {
|
|
247
|
-
return this.nav.setContent(html, options);
|
|
248
|
-
}
|
|
249
|
-
/** The title of the document now */
|
|
250
|
-
async title() {
|
|
251
|
-
this.assertOpen();
|
|
252
|
-
return readTitle(this.biDiConnector, this.id);
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Runs `fn` in the page, with `arg` as its only argument, and returns the result (a promise is awaited). `fn` is a function,
|
|
256
|
-
* the source of one (`"async () => ..."`), or any other string, which is an expression (`"document.title"`; it takes no argument).
|
|
257
|
-
* Values come back as plain JavaScript: Date, Map, Set, RegExp, BigInt, NaN, undefined, nested objects and arrays;
|
|
258
|
-
* an element as an `ElementHandle`; what cannot be copied (a function, window, error, promise) as a `RemoteObject` with its `type`.
|
|
259
|
-
* The page's own error message is kept (`ScriptError`). A function written under tsx must not contain named inner functions
|
|
260
|
-
* (the transpiler adds a `__name` helper the page lacks); a string always works.
|
|
261
|
-
* @example
|
|
262
|
-
* const count = await page.evaluate((min) => document.querySelectorAll("li").length > min, 3);
|
|
263
|
-
*/
|
|
264
|
-
async evaluate(fn, arg) {
|
|
265
|
-
this.assertOpen();
|
|
266
|
-
return evaluateInPage(this.biDiConnector, this.id, fn, arg);
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Sets the size of the viewport (and, optionally, the device pixel ratio); `null` puts the browser's own size back.
|
|
270
|
-
* @example
|
|
271
|
-
* await page.setViewport({ width: 390, height: 844, devicePixelRatio: 3 });
|
|
272
|
-
*/
|
|
273
|
-
async setViewport(viewport) {
|
|
274
|
-
this.assertOpen();
|
|
275
|
-
await this.viewport.set(viewport);
|
|
276
|
-
}
|
|
277
|
-
/** @internal Chrome: lays this page out like a phone from now on (`use.isMobile`), including the size it has now */
|
|
278
|
-
async setMobileLayout() {
|
|
279
|
-
await this.viewport.setMobileLayout();
|
|
280
|
-
}
|
|
281
|
-
/** Playwright's name for `setViewport` */
|
|
282
|
-
setViewportSize(size) {
|
|
283
|
-
return this.setViewport(size);
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* The size this page was last given (`setViewport`, `setViewportSize`, or the default from config `use.viewport` when the
|
|
287
|
-
* browser opened it), read at once; null before one is known, after `setViewport(null)` and when config turns the viewport off.
|
|
288
|
-
*/
|
|
289
|
-
viewportSize() {
|
|
290
|
-
return this.viewport.size();
|
|
291
|
-
}
|
|
292
|
-
/** Gives the page the size from config `use.viewport` (1280x720 by default); the browser does this when it opens a page */
|
|
293
|
-
async applyDefaultViewport() {
|
|
294
|
-
await this.viewport.applyDefault();
|
|
295
|
-
}
|
|
296
13
|
/**
|
|
297
14
|
* What the browser sets up on every page it opens: the default size, then everything that has to be listening
|
|
298
15
|
* before the page does anything (requests, dialogs, logs, downloads, file pickers). Safe to call again.
|
|
@@ -305,301 +22,4 @@ export default class Page {
|
|
|
305
22
|
await this.startDownloadTracking();
|
|
306
23
|
await this.startFileChooserTracking();
|
|
307
24
|
}
|
|
308
|
-
/**
|
|
309
|
-
* Takes a screenshot and returns the image (also written to `options.path`).
|
|
310
|
-
* @example
|
|
311
|
-
* await page.screenshot({ path: "out/home.png", fullPage: true });
|
|
312
|
-
*/
|
|
313
|
-
async screenshot(options = {}) {
|
|
314
|
-
this.assertOpen();
|
|
315
|
-
return screenshotWithEvidence(this.biDiConnector, this.id, options, this.evidence);
|
|
316
|
-
}
|
|
317
|
-
/** @internal The runner gives each test its own evidence folder */
|
|
318
|
-
setEvidence(evidence) {
|
|
319
|
-
this.evidence = evidence;
|
|
320
|
-
}
|
|
321
|
-
/** Waits `ms` milliseconds. Prefer waiting for something (an assertion, `waitForNetworkIdle`) when you can */
|
|
322
|
-
async waitForTimeout(ms) {
|
|
323
|
-
if (!Number.isFinite(ms) || ms < 0)
|
|
324
|
-
throw new RangeError(`waitForTimeout(): ms must be 0 or more, got ${ms}`);
|
|
325
|
-
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
326
|
-
}
|
|
327
|
-
/** Renders the page as a PDF and returns it (also written to `options.path`) */
|
|
328
|
-
async pdf(options = {}) {
|
|
329
|
-
this.assertOpen();
|
|
330
|
-
return printPdf(this.biDiConnector, this.id, options);
|
|
331
|
-
}
|
|
332
|
-
/** The browser context (cookies, storage) this page belongs to */
|
|
333
|
-
context() {
|
|
334
|
-
if (!this.browserContext)
|
|
335
|
-
throw new Error("this page has no browser context: open it with browser.newPage() or context.newPage()");
|
|
336
|
-
return this.browserContext;
|
|
337
|
-
}
|
|
338
|
-
/** Makes HTTP requests from the test with this page's cookies; the same as `page.context().request` */
|
|
339
|
-
get request() {
|
|
340
|
-
return this.context().request;
|
|
341
|
-
}
|
|
342
|
-
/** @internal Called when the page is opened in a context */
|
|
343
|
-
setContext(context) {
|
|
344
|
-
this.browserContext = context;
|
|
345
|
-
}
|
|
346
|
-
/** @internal Marks the page closed after its tab is gone (its context was removed) and releases what it registered */
|
|
347
|
-
async detach() {
|
|
348
|
-
this.isClosed = true;
|
|
349
|
-
await this.dispose().catch((err) => logger.debug("Releasing a detached page failed", { err }));
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* Changes the `use` options that can change while the browser runs for this page only: locale, timezone, user
|
|
353
|
-
* agent, geolocation, offline, orientation, screen, hasTouch, javaScriptEnabled. It wins over what the page's
|
|
354
|
-
* context says; `null` puts the real (or the context's) value back. Same options as `test.use()`, applied now.
|
|
355
|
-
* @example
|
|
356
|
-
* await page.use({ locale: "nl-NL", offline: true });
|
|
357
|
-
*/
|
|
358
|
-
async use(options) {
|
|
359
|
-
this.assertOpen();
|
|
360
|
-
await applyUse(this.biDiConnector, { contexts: [this.id] }, options);
|
|
361
|
-
}
|
|
362
|
-
/**
|
|
363
|
-
* Grants a browser permission ("geolocation", "notifications", "camera", …) to the origin this page is on,
|
|
364
|
-
* or to `options.origin`. A geolocation override only answers once this is granted.
|
|
365
|
-
*/
|
|
366
|
-
async grantPermission(name, options = {}) {
|
|
367
|
-
this.assertOpen();
|
|
368
|
-
await grantPermission(this.biDiConnector, () => this.url(), () => this.browserContext, name, options);
|
|
369
|
-
}
|
|
370
|
-
/** True once `close()` has run */
|
|
371
|
-
get closed() {
|
|
372
|
-
return this.isClosed;
|
|
373
|
-
}
|
|
374
|
-
assertOpen() {
|
|
375
|
-
if (this.isClosed)
|
|
376
|
-
throw new Error("page closed");
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Closes the tab and releases everything the page registered. Later calls on the page throw "page closed".
|
|
380
|
-
* With `runBeforeUnload` the page's `beforeunload` handlers run first. Closing the browser's last tab may end the browser.
|
|
381
|
-
*/
|
|
382
|
-
async close(options = {}) {
|
|
383
|
-
if (this.isClosed)
|
|
384
|
-
return;
|
|
385
|
-
try {
|
|
386
|
-
await this.biDiConnector.send("browsingContext.close", {
|
|
387
|
-
context: this.id,
|
|
388
|
-
...(options.runBeforeUnload && { promptUnload: true }),
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
catch (err) {
|
|
392
|
-
// Already gone (closed by the page, the browser or another call): the goal is met
|
|
393
|
-
if (!(err instanceof BiDiError && err.code === "no such frame"))
|
|
394
|
-
throw err;
|
|
395
|
-
}
|
|
396
|
-
this.isClosed = true;
|
|
397
|
-
await this.dispose().catch((err) => logger.debug("Releasing a closed page failed", { err }));
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* Waits until the page's document reached `state` (default "load"): "domcontentloaded" and "load" follow the
|
|
401
|
-
* document, "networkidle" is "load" plus a quiet network (see `waitForNetworkIdle`). Resolves at once when it
|
|
402
|
-
* is already there. Call it once the navigation has started; a document that is still the old one counts.
|
|
403
|
-
* @example
|
|
404
|
-
* await page.locator("a[@id='next']").click();
|
|
405
|
-
* await page.waitForLoadState("domcontentloaded");
|
|
406
|
-
*/
|
|
407
|
-
waitForLoadState(state = "load", options = {}) {
|
|
408
|
-
return this.nav.waitForLoadState(state, options);
|
|
409
|
-
}
|
|
410
|
-
/**
|
|
411
|
-
* Starts following this page's requests now. Tracking only sees requests made after it started, so the
|
|
412
|
-
* browser calls this when it opens a page; without it, tracking starts on first use. Does nothing when
|
|
413
|
-
* config `network.track` is false.
|
|
414
|
-
*/
|
|
415
|
-
startNetworkTracking() {
|
|
416
|
-
return this.net.startTracking();
|
|
417
|
-
}
|
|
418
|
-
/**
|
|
419
|
-
* Waits until no request has been in flight for `idleTime` ms (default config `network.idleTime`, then 500).
|
|
420
|
-
* Long-lived requests (server-sent events, long polling) keep the page busy until `timeout`.
|
|
421
|
-
*/
|
|
422
|
-
waitForNetworkIdle(options = {}) {
|
|
423
|
-
return this.net.waitForIdle(options);
|
|
424
|
-
}
|
|
425
|
-
/**
|
|
426
|
-
* Listens for page events: `request`, `response`, `requestfailed` (network), `dialog` (a JavaScript dialog or
|
|
427
|
-
* `beforeunload` prompt opened; answer it with `dialog.accept()` / `dialog.dismiss()`), `console` (a `console.*` call)
|
|
428
|
-
* and `pageerror` (an uncaught exception). Starts what the event needs.
|
|
429
|
-
* A dialog nobody answers is dismissed automatically (a `beforeunload` prompt is accepted), so it cannot hang a test.
|
|
430
|
-
* @example
|
|
431
|
-
* page.on("dialog", (dialog) => dialog.accept());
|
|
432
|
-
*/
|
|
433
|
-
on(event, listener) {
|
|
434
|
-
this.pageEvents.on(event, listener);
|
|
435
|
-
this.trackInBackground(event);
|
|
436
|
-
return this;
|
|
437
|
-
}
|
|
438
|
-
once(event, listener) {
|
|
439
|
-
this.pageEvents.once(event, listener);
|
|
440
|
-
this.trackInBackground(event);
|
|
441
|
-
return this;
|
|
442
|
-
}
|
|
443
|
-
off(event, listener) {
|
|
444
|
-
this.pageEvents.off(event, listener);
|
|
445
|
-
return this;
|
|
446
|
-
}
|
|
447
|
-
trackInBackground(event) {
|
|
448
|
-
const start = {
|
|
449
|
-
dialogs: () => this.startDialogHandling(),
|
|
450
|
-
downloads: () => this.startDownloadTracking(),
|
|
451
|
-
filechoosers: () => this.startFileChooserTracking(),
|
|
452
|
-
logs: () => this.startLogging(),
|
|
453
|
-
network: () => this.startNetworkTracking(),
|
|
454
|
-
bodies: () => this.startBodyCollection(),
|
|
455
|
-
};
|
|
456
|
-
startSources(event, start, (err) => logger.error("Could not start listening for %s", event, { err }));
|
|
457
|
-
}
|
|
458
|
-
/** Starts following downloads; the browser does this when it opens a page, `page.on("download")` and `waitForDownload` when needed */
|
|
459
|
-
startDownloadTracking() {
|
|
460
|
-
return this.transfers.startDownloadTracking();
|
|
461
|
-
}
|
|
462
|
-
/** Starts following file pickers; the browser does this when it opens a page, `page.on("filechooser")` and `waitForFileChooser` when needed */
|
|
463
|
-
startFileChooserTracking() {
|
|
464
|
-
return this.transfers.startFileChooserTracking();
|
|
465
|
-
}
|
|
466
|
-
/**
|
|
467
|
-
* Resolves with the next download this page starts (when it starts, not when it ends: use `download.path()` for that).
|
|
468
|
-
* Call it before the download is triggered, or pass `trigger`, which runs once the wait is set up.
|
|
469
|
-
* @example
|
|
470
|
-
* const download = await page.waitForDownload({ trigger: () => link.click() });
|
|
471
|
-
* const file = await download.path();
|
|
472
|
-
*/
|
|
473
|
-
async waitForDownload(options = {}) {
|
|
474
|
-
this.assertOpen();
|
|
475
|
-
return this.transfers.waitForDownload(options);
|
|
476
|
-
}
|
|
477
|
-
/**
|
|
478
|
-
* Resolves with the next file picker the page opens (a click on an `<input type=file>` or its label). Nothing shows
|
|
479
|
-
* on screen: answer it with `chooser.setFiles(...)`.
|
|
480
|
-
* @example
|
|
481
|
-
* const chooser = await page.waitForFileChooser({ trigger: () => page.locator("label[@for='avatar']").click() });
|
|
482
|
-
* await chooser.setFiles("./fixtures/avatar.png");
|
|
483
|
-
*/
|
|
484
|
-
async waitForFileChooser(options = {}) {
|
|
485
|
-
this.assertOpen();
|
|
486
|
-
return this.transfers.waitForFileChooser(options);
|
|
487
|
-
}
|
|
488
|
-
/**
|
|
489
|
-
* Playwright-style wait for one page event: `download`, `filechooser`, `request` or `response`. Same as the named
|
|
490
|
-
* `waitFor…` method, with an optional predicate (a function, or `predicate` in the options). Call it before the event
|
|
491
|
-
* is caused, or pass `trigger`, which runs once the wait is set up.
|
|
492
|
-
* @example
|
|
493
|
-
* const chooser = await page.waitForEvent("filechooser", { trigger: () => page.getByText("Upload file").click() });
|
|
494
|
-
* await chooser.setFiles("./myfile.pdf");
|
|
495
|
-
*/
|
|
496
|
-
async waitForEvent(event, optionsOrPredicate = {}) {
|
|
497
|
-
return waitForEventOn({
|
|
498
|
-
request: (accepts, options) => this.waitForRequest(accepts, options),
|
|
499
|
-
response: (accepts, options) => this.waitForResponse(accepts, options),
|
|
500
|
-
download: (options, accepts) => {
|
|
501
|
-
this.assertOpen();
|
|
502
|
-
return this.transfers.waitForDownload(options, accepts);
|
|
503
|
-
},
|
|
504
|
-
filechooser: (options, accepts) => {
|
|
505
|
-
this.assertOpen();
|
|
506
|
-
return this.transfers.waitForFileChooser(options, accepts);
|
|
507
|
-
},
|
|
508
|
-
}, event, optionsOrPredicate);
|
|
509
|
-
}
|
|
510
|
-
/**
|
|
511
|
-
* Starts keeping response bodies, which is what makes `response.body()` possible: the browser only keeps a body
|
|
512
|
-
* when this ran before the request started. `page.on("response")` and `waitForResponse` call it; config
|
|
513
|
-
* `network.collectBodies` calls it when the browser opens the page.
|
|
514
|
-
*/
|
|
515
|
-
startBodyCollection() {
|
|
516
|
-
return this.net.startBodyCollection();
|
|
517
|
-
}
|
|
518
|
-
/**
|
|
519
|
-
* Resolves with the next response of this page whose URL matches (a pattern, see `route`) or that the function
|
|
520
|
-
* accepts. Call it before the request is made, or pass `trigger`, which runs once the wait is set up.
|
|
521
|
-
* @example
|
|
522
|
-
* const response = await page.waitForResponse("**\/api/users", { trigger: () => button.click() });
|
|
523
|
-
* const users = await response.json();
|
|
524
|
-
*/
|
|
525
|
-
waitForResponse(match, options = {}) {
|
|
526
|
-
return this.net.waitForResponse(match, options);
|
|
527
|
-
}
|
|
528
|
-
/** Like `waitForResponse`, for requests */
|
|
529
|
-
waitForRequest(match, options = {}) {
|
|
530
|
-
return this.net.waitForRequest(match, options);
|
|
531
|
-
}
|
|
532
|
-
/**
|
|
533
|
-
* Resolves with the first `event` `matches` accepts. Listens before `trigger` runs, so what the trigger causes cannot
|
|
534
|
-
* be missed; a trigger that throws rejects the wait with its error. `onTimeout` builds the error for a wait that ran out.
|
|
535
|
-
*/
|
|
536
|
-
async waitForPageEvent(event, matches, options, onTimeout) {
|
|
537
|
-
const timeout = await this.pageConfig.navigationTimeout(options.timeout);
|
|
538
|
-
return waitForPageEvent(this.pageEvents, event, matches, options, timeout, onTimeout);
|
|
539
|
-
}
|
|
540
|
-
/** Turns the browser's HTTP cache off (`true`) or back on for this page: every request then goes to the server */
|
|
541
|
-
setCacheDisabled(disabled) {
|
|
542
|
-
return this.net.setCacheDisabled(disabled);
|
|
543
|
-
}
|
|
544
|
-
/**
|
|
545
|
-
* Sends requests whose URL matches to `handler`, which must answer with `route.fulfill()`, `route.continue()`,
|
|
546
|
-
* `route.abort()` or `route.fallback()` (await the answer: a handler that returns first lets the request continue).
|
|
547
|
-
* A string without `*` is an exact URL, with `*` / `**` a glob (`*` stops at `/`), a RegExp is tested on the URL.
|
|
548
|
-
* Routes added later are asked first. Errors a handler throws fail the request and, in the runner, the test.
|
|
549
|
-
* @example
|
|
550
|
-
* await page.route("**\/api/users", (route) => route.fulfill({ json: [{ id: 1 }] }));
|
|
551
|
-
* await page.route(/\.(png|jpg)$/, (route) => route.abort());
|
|
552
|
-
*/
|
|
553
|
-
async route(url, handler) {
|
|
554
|
-
this.assertOpen();
|
|
555
|
-
await this.net.route(url, handler);
|
|
556
|
-
}
|
|
557
|
-
/** Removes the routes for `url` (only the one with `handler`, when given) */
|
|
558
|
-
unroute(url, handler) {
|
|
559
|
-
return this.net.unroute(url, handler);
|
|
560
|
-
}
|
|
561
|
-
unrouteAll() {
|
|
562
|
-
return this.net.unrouteAll();
|
|
563
|
-
}
|
|
564
|
-
/** Errors thrown by route handlers so far */
|
|
565
|
-
routeErrors() {
|
|
566
|
-
return this.net.routeErrors();
|
|
567
|
-
}
|
|
568
|
-
/**
|
|
569
|
-
* Starts collecting the page's console output and uncaught exceptions. The browser does this when it opens a page so
|
|
570
|
-
* early output is kept; `page.on("console" | "pageerror")` starts it when needed.
|
|
571
|
-
*/
|
|
572
|
-
async startLogging() {
|
|
573
|
-
await this.logging.start();
|
|
574
|
-
}
|
|
575
|
-
/** The newest console and error entries of this page (empty until logging started), oldest first */
|
|
576
|
-
getLogs() {
|
|
577
|
-
return this.logging.entries();
|
|
578
|
-
}
|
|
579
|
-
/** Uncaught exceptions of the page since logging started or `clearLogs()` */
|
|
580
|
-
pageErrors() {
|
|
581
|
-
return this.logging.errors();
|
|
582
|
-
}
|
|
583
|
-
/** How many entries fell out of the page's log buffer */
|
|
584
|
-
get logsDropped() {
|
|
585
|
-
return this.logging.dropped;
|
|
586
|
-
}
|
|
587
|
-
clearLogs() {
|
|
588
|
-
this.logging.clear();
|
|
589
|
-
}
|
|
590
|
-
/** Resolves once everything the page logged before the call has arrived: events and replies share one ordered connection */
|
|
591
|
-
async syncLogs() {
|
|
592
|
-
await this.biDiConnector.send("session.status", {});
|
|
593
|
-
}
|
|
594
|
-
/** Tells the runner not to fail this test on uncaught page exceptions (config `logs.failOnPageError`) */
|
|
595
|
-
allowPageErrors() {
|
|
596
|
-
this.logging.allowErrors();
|
|
597
|
-
}
|
|
598
|
-
get pageErrorsAllowed() {
|
|
599
|
-
return this.logging.errorsAreAllowed;
|
|
600
|
-
}
|
|
601
|
-
/** Starts watching for dialogs; the browser does this when it opens a page, `page.on("dialog")` when needed */
|
|
602
|
-
startDialogHandling() {
|
|
603
|
-
return this.dialogs.start();
|
|
604
|
-
}
|
|
605
25
|
}
|