page.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var page_exports = {};
  30. __export(page_exports, {
  31. InitScript: () => InitScript,
  32. Page: () => Page,
  33. PageBinding: () => PageBinding,
  34. Worker: () => Worker
  35. });
  36. module.exports = __toCommonJS(page_exports);
  37. var accessibility = __toESM(require("./accessibility"));
  38. var import_browserContext = require("./browserContext");
  39. var import_console = require("./console");
  40. var import_errors = require("./errors");
  41. var import_fileChooser = require("./fileChooser");
  42. var frames = __toESM(require("./frames"));
  43. var import_helper = require("./helper");
  44. var input = __toESM(require("./input"));
  45. var import_instrumentation = require("./instrumentation");
  46. var js = __toESM(require("./javascript"));
  47. var import_screenshotter = require("./screenshotter");
  48. var import_utils = require("../utils");
  49. var import_utils2 = require("../utils");
  50. var import_comparators = require("./utils/comparators");
  51. var import_debugLogger = require("./utils/debugLogger");
  52. var import_selectorParser = require("../utils/isomorphic/selectorParser");
  53. var import_manualPromise = require("../utils/isomorphic/manualPromise");
  54. var import_utilityScriptSerializers = require("../utils/isomorphic/utilityScriptSerializers");
  55. var import_callLog = require("./callLog");
  56. var rawBindingsControllerSource = __toESM(require("../generated/bindingsControllerSource"));
  57. class Page extends import_instrumentation.SdkObject {
  58. constructor(delegate, browserContext) {
  59. super(browserContext, "page");
  60. this._closedState = "open";
  61. this._closedPromise = new import_manualPromise.ManualPromise();
  62. this._initializedPromise = new import_manualPromise.ManualPromise();
  63. this._eventsToEmitAfterInitialized = [];
  64. this._crashed = false;
  65. this.openScope = new import_utils.LongStandingScope();
  66. this._emulatedMedia = {};
  67. this._fileChooserInterceptedBy = /* @__PURE__ */ new Set();
  68. this._pageBindings = /* @__PURE__ */ new Map();
  69. this.initScripts = [];
  70. this._workers = /* @__PURE__ */ new Map();
  71. this.requestInterceptors = [];
  72. this.video = null;
  73. this._locatorHandlers = /* @__PURE__ */ new Map();
  74. this._lastLocatorHandlerUid = 0;
  75. this._locatorHandlerRunningCounter = 0;
  76. // Aiming at 25 fps by default - each frame is 40ms, but we give some slack with 35ms.
  77. // When throttling for tracing, 200ms between frames, except for 10 frames around the action.
  78. this._frameThrottler = new FrameThrottler(10, 35, 200);
  79. this.lastSnapshotFrameIds = [];
  80. this.attribution.page = this;
  81. this.delegate = delegate;
  82. this.browserContext = browserContext;
  83. this.accessibility = new accessibility.Accessibility(delegate.getAccessibilityTree.bind(delegate));
  84. this.keyboard = new input.Keyboard(delegate.rawKeyboard, this);
  85. this.mouse = new input.Mouse(delegate.rawMouse, this);
  86. this.touchscreen = new input.Touchscreen(delegate.rawTouchscreen, this);
  87. this.screenshotter = new import_screenshotter.Screenshotter(this);
  88. this.frameManager = new frames.FrameManager(this);
  89. if (delegate.pdf)
  90. this.pdf = delegate.pdf.bind(delegate);
  91. this.coverage = delegate.coverage ? delegate.coverage() : null;
  92. this.isStorageStatePage = browserContext.isCreatingStorageStatePage();
  93. }
  94. static {
  95. this.Events = {
  96. Close: "close",
  97. Crash: "crash",
  98. Download: "download",
  99. EmulatedSizeChanged: "emulatedsizechanged",
  100. FileChooser: "filechooser",
  101. FrameAttached: "frameattached",
  102. FrameDetached: "framedetached",
  103. InternalFrameNavigatedToNewDocument: "internalframenavigatedtonewdocument",
  104. LocatorHandlerTriggered: "locatorhandlertriggered",
  105. ScreencastFrame: "screencastframe",
  106. Video: "video",
  107. WebSocket: "websocket",
  108. Worker: "worker"
  109. };
  110. }
  111. async reportAsNew(opener, error = void 0, contextEvent = import_browserContext.BrowserContext.Events.Page) {
  112. if (opener) {
  113. const openerPageOrError = await opener.waitForInitializedOrError();
  114. if (openerPageOrError instanceof Page && !openerPageOrError.isClosed())
  115. this._opener = openerPageOrError;
  116. }
  117. this._markInitialized(error, contextEvent);
  118. }
  119. _markInitialized(error = void 0, contextEvent = import_browserContext.BrowserContext.Events.Page) {
  120. if (error) {
  121. if (this.browserContext.isClosingOrClosed())
  122. return;
  123. this.frameManager.createDummyMainFrameIfNeeded();
  124. }
  125. this._initialized = error || this;
  126. this.emitOnContext(contextEvent, this);
  127. for (const { event, args } of this._eventsToEmitAfterInitialized)
  128. this.browserContext.emit(event, ...args);
  129. this._eventsToEmitAfterInitialized = [];
  130. if (this.isClosed())
  131. this.emit(Page.Events.Close);
  132. else
  133. this.instrumentation.onPageOpen(this);
  134. this._initializedPromise.resolve(this._initialized);
  135. }
  136. initializedOrUndefined() {
  137. return this._initialized ? this : void 0;
  138. }
  139. waitForInitializedOrError() {
  140. return this._initializedPromise;
  141. }
  142. emitOnContext(event, ...args) {
  143. if (this.isStorageStatePage)
  144. return;
  145. this.browserContext.emit(event, ...args);
  146. }
  147. emitOnContextOnceInitialized(event, ...args) {
  148. if (this.isStorageStatePage)
  149. return;
  150. if (this._initialized)
  151. this.browserContext.emit(event, ...args);
  152. else
  153. this._eventsToEmitAfterInitialized.push({ event, args });
  154. }
  155. async resetForReuse(progress) {
  156. await this.mainFrame().gotoImpl(progress, "about:blank", {});
  157. this._emulatedSize = void 0;
  158. this._emulatedMedia = {};
  159. this._extraHTTPHeaders = void 0;
  160. await Promise.all([
  161. this.delegate.updateEmulatedViewportSize(),
  162. this.delegate.updateEmulateMedia(),
  163. this.delegate.updateExtraHTTPHeaders()
  164. ]);
  165. await this.delegate.resetForReuse(progress);
  166. }
  167. _didClose() {
  168. this.frameManager.dispose();
  169. this._frameThrottler.dispose();
  170. (0, import_utils.assert)(this._closedState !== "closed", "Page closed twice");
  171. this._closedState = "closed";
  172. this.emit(Page.Events.Close);
  173. this._closedPromise.resolve();
  174. this.instrumentation.onPageClose(this);
  175. this.openScope.close(new import_errors.TargetClosedError());
  176. }
  177. _didCrash() {
  178. this.frameManager.dispose();
  179. this._frameThrottler.dispose();
  180. this.emit(Page.Events.Crash);
  181. this._crashed = true;
  182. this.instrumentation.onPageClose(this);
  183. this.openScope.close(new Error("Page crashed"));
  184. }
  185. async _onFileChooserOpened(handle) {
  186. let multiple;
  187. try {
  188. multiple = await handle.evaluate((element) => !!element.multiple);
  189. } catch (e) {
  190. return;
  191. }
  192. if (!this.listenerCount(Page.Events.FileChooser)) {
  193. handle.dispose();
  194. return;
  195. }
  196. const fileChooser = new import_fileChooser.FileChooser(this, handle, multiple);
  197. this.emit(Page.Events.FileChooser, fileChooser);
  198. }
  199. opener() {
  200. return this._opener;
  201. }
  202. mainFrame() {
  203. return this.frameManager.mainFrame();
  204. }
  205. frames() {
  206. return this.frameManager.frames();
  207. }
  208. async exposeBinding(progress, name, needsHandle, playwrightBinding) {
  209. if (this._pageBindings.has(name))
  210. throw new Error(`Function "${name}" has been already registered`);
  211. if (this.browserContext._pageBindings.has(name))
  212. throw new Error(`Function "${name}" has been already registered in the browser context`);
  213. await progress.race(this.browserContext.exposePlaywrightBindingIfNeeded());
  214. const binding = new PageBinding(name, playwrightBinding, needsHandle);
  215. this._pageBindings.set(name, binding);
  216. try {
  217. await progress.race(this.delegate.addInitScript(binding.initScript));
  218. await progress.race(this.safeNonStallingEvaluateInAllFrames(binding.initScript.source, "main"));
  219. return binding;
  220. } catch (error) {
  221. this._pageBindings.delete(name);
  222. throw error;
  223. }
  224. }
  225. async removeExposedBindings(bindings) {
  226. bindings = bindings.filter((binding) => this._pageBindings.get(binding.name) === binding);
  227. for (const binding of bindings)
  228. this._pageBindings.delete(binding.name);
  229. await this.delegate.removeInitScripts(bindings.map((binding) => binding.initScript));
  230. const cleanup = bindings.map((binding) => `{ ${binding.cleanupScript} };
  231. `).join("");
  232. await this.safeNonStallingEvaluateInAllFrames(cleanup, "main");
  233. }
  234. async setExtraHTTPHeaders(progress, headers) {
  235. const oldHeaders = this._extraHTTPHeaders;
  236. try {
  237. this._extraHTTPHeaders = headers;
  238. await progress.race(this.delegate.updateExtraHTTPHeaders());
  239. } catch (error) {
  240. this._extraHTTPHeaders = oldHeaders;
  241. this.delegate.updateExtraHTTPHeaders().catch(() => {
  242. });
  243. throw error;
  244. }
  245. }
  246. extraHTTPHeaders() {
  247. return this._extraHTTPHeaders;
  248. }
  249. async onBindingCalled(payload, context) {
  250. if (this._closedState === "closed")
  251. return;
  252. await PageBinding.dispatch(this, payload, context);
  253. }
  254. addConsoleMessage(type, args, location, text) {
  255. const message = new import_console.ConsoleMessage(this, type, text, args, location);
  256. const intercepted = this.frameManager.interceptConsoleMessage(message);
  257. if (intercepted) {
  258. args.forEach((arg) => arg.dispose());
  259. return;
  260. }
  261. this.emitOnContextOnceInitialized(import_browserContext.BrowserContext.Events.Console, message);
  262. }
  263. async reload(progress, options) {
  264. return this.mainFrame().raceNavigationAction(progress, async () => {
  265. const [response] = await Promise.all([
  266. // Reload must be a new document, and should not be confused with a stray pushState.
  267. this.mainFrame()._waitForNavigation(progress, true, options),
  268. progress.race(this.delegate.reload())
  269. ]);
  270. return response;
  271. });
  272. }
  273. async goBack(progress, options) {
  274. return this.mainFrame().raceNavigationAction(progress, async () => {
  275. let error;
  276. const waitPromise = this.mainFrame()._waitForNavigation(progress, false, options).catch((e) => {
  277. error = e;
  278. return null;
  279. });
  280. const result = await progress.race(this.delegate.goBack());
  281. if (!result) {
  282. waitPromise.catch(() => {
  283. });
  284. return null;
  285. }
  286. const response = await waitPromise;
  287. if (error)
  288. throw error;
  289. return response;
  290. });
  291. }
  292. async goForward(progress, options) {
  293. return this.mainFrame().raceNavigationAction(progress, async () => {
  294. let error;
  295. const waitPromise = this.mainFrame()._waitForNavigation(progress, false, options).catch((e) => {
  296. error = e;
  297. return null;
  298. });
  299. const result = await progress.race(this.delegate.goForward());
  300. if (!result) {
  301. waitPromise.catch(() => {
  302. });
  303. return null;
  304. }
  305. const response = await waitPromise;
  306. if (error)
  307. throw error;
  308. return response;
  309. });
  310. }
  311. requestGC() {
  312. return this.delegate.requestGC();
  313. }
  314. registerLocatorHandler(selector, noWaitAfter) {
  315. const uid = ++this._lastLocatorHandlerUid;
  316. this._locatorHandlers.set(uid, { selector, noWaitAfter });
  317. return uid;
  318. }
  319. resolveLocatorHandler(uid, remove) {
  320. const handler = this._locatorHandlers.get(uid);
  321. if (remove)
  322. this._locatorHandlers.delete(uid);
  323. if (handler) {
  324. handler.resolved?.resolve();
  325. handler.resolved = void 0;
  326. }
  327. }
  328. unregisterLocatorHandler(uid) {
  329. this._locatorHandlers.delete(uid);
  330. }
  331. async performActionPreChecks(progress) {
  332. await this._performWaitForNavigationCheck(progress);
  333. await this._performLocatorHandlersCheckpoint(progress);
  334. await this._performWaitForNavigationCheck(progress);
  335. }
  336. async _performWaitForNavigationCheck(progress) {
  337. const mainFrame = this.frameManager.mainFrame();
  338. if (!mainFrame || !mainFrame.pendingDocument())
  339. return;
  340. const url = mainFrame.pendingDocument()?.request?.url();
  341. const toUrl = url ? `" ${(0, import_utils.trimStringWithEllipsis)(url, 200)}"` : "";
  342. progress.log(` waiting for${toUrl} navigation to finish...`);
  343. await import_helper.helper.waitForEvent(progress, mainFrame, frames.Frame.Events.InternalNavigation, (e) => {
  344. if (!e.isPublic)
  345. return false;
  346. if (!e.error)
  347. progress.log(` navigated to "${(0, import_utils.trimStringWithEllipsis)(mainFrame.url(), 200)}"`);
  348. return true;
  349. }).promise;
  350. }
  351. async _performLocatorHandlersCheckpoint(progress) {
  352. if (this._locatorHandlerRunningCounter)
  353. return;
  354. for (const [uid, handler] of this._locatorHandlers) {
  355. if (!handler.resolved) {
  356. if (await this.mainFrame().isVisibleInternal(progress, handler.selector, { strict: true })) {
  357. handler.resolved = new import_manualPromise.ManualPromise();
  358. this.emit(Page.Events.LocatorHandlerTriggered, uid);
  359. }
  360. }
  361. if (handler.resolved) {
  362. ++this._locatorHandlerRunningCounter;
  363. progress.log(` found ${(0, import_utils2.asLocator)(this.browserContext._browser.sdkLanguage(), handler.selector)}, intercepting action to run the handler`);
  364. const promise = handler.resolved.then(async () => {
  365. if (!handler.noWaitAfter) {
  366. progress.log(` locator handler has finished, waiting for ${(0, import_utils2.asLocator)(this.browserContext._browser.sdkLanguage(), handler.selector)} to be hidden`);
  367. await this.mainFrame().waitForSelector(progress, handler.selector, false, { state: "hidden" });
  368. } else {
  369. progress.log(` locator handler has finished`);
  370. }
  371. });
  372. await progress.race(this.openScope.race(promise)).finally(() => --this._locatorHandlerRunningCounter);
  373. progress.log(` interception handler has finished, continuing`);
  374. }
  375. }
  376. }
  377. async emulateMedia(progress, options) {
  378. const oldEmulatedMedia = { ...this._emulatedMedia };
  379. if (options.media !== void 0)
  380. this._emulatedMedia.media = options.media;
  381. if (options.colorScheme !== void 0)
  382. this._emulatedMedia.colorScheme = options.colorScheme;
  383. if (options.reducedMotion !== void 0)
  384. this._emulatedMedia.reducedMotion = options.reducedMotion;
  385. if (options.forcedColors !== void 0)
  386. this._emulatedMedia.forcedColors = options.forcedColors;
  387. if (options.contrast !== void 0)
  388. this._emulatedMedia.contrast = options.contrast;
  389. try {
  390. await progress.race(this.delegate.updateEmulateMedia());
  391. } catch (error) {
  392. this._emulatedMedia = oldEmulatedMedia;
  393. this.delegate.updateEmulateMedia().catch(() => {
  394. });
  395. throw error;
  396. }
  397. }
  398. emulatedMedia() {
  399. const contextOptions = this.browserContext._options;
  400. return {
  401. media: this._emulatedMedia.media || "no-override",
  402. colorScheme: this._emulatedMedia.colorScheme !== void 0 ? this._emulatedMedia.colorScheme : contextOptions.colorScheme ?? "light",
  403. reducedMotion: this._emulatedMedia.reducedMotion !== void 0 ? this._emulatedMedia.reducedMotion : contextOptions.reducedMotion ?? "no-preference",
  404. forcedColors: this._emulatedMedia.forcedColors !== void 0 ? this._emulatedMedia.forcedColors : contextOptions.forcedColors ?? "none",
  405. contrast: this._emulatedMedia.contrast !== void 0 ? this._emulatedMedia.contrast : contextOptions.contrast ?? "no-preference"
  406. };
  407. }
  408. async setViewportSize(progress, viewportSize) {
  409. const oldEmulatedSize = this._emulatedSize;
  410. try {
  411. this._setEmulatedSize({ viewport: { ...viewportSize }, screen: { ...viewportSize } });
  412. await progress.race(this.delegate.updateEmulatedViewportSize());
  413. } catch (error) {
  414. this._emulatedSize = oldEmulatedSize;
  415. this.delegate.updateEmulatedViewportSize().catch(() => {
  416. });
  417. throw error;
  418. }
  419. }
  420. setEmulatedSizeFromWindowOpen(emulatedSize) {
  421. this._setEmulatedSize(emulatedSize);
  422. }
  423. _setEmulatedSize(emulatedSize) {
  424. this._emulatedSize = emulatedSize;
  425. this.emit(Page.Events.EmulatedSizeChanged);
  426. }
  427. emulatedSize() {
  428. if (this._emulatedSize)
  429. return this._emulatedSize;
  430. const contextOptions = this.browserContext._options;
  431. return contextOptions.viewport ? { viewport: contextOptions.viewport, screen: contextOptions.screen || contextOptions.viewport } : void 0;
  432. }
  433. async bringToFront() {
  434. await this.delegate.bringToFront();
  435. }
  436. async addInitScript(progress, source) {
  437. const initScript = new InitScript(source);
  438. this.initScripts.push(initScript);
  439. try {
  440. await progress.race(this.delegate.addInitScript(initScript));
  441. } catch (error) {
  442. this.removeInitScripts([initScript]).catch(() => {
  443. });
  444. throw error;
  445. }
  446. return initScript;
  447. }
  448. async removeInitScripts(initScripts) {
  449. const set = new Set(initScripts);
  450. this.initScripts = this.initScripts.filter((script) => !set.has(script));
  451. await this.delegate.removeInitScripts(initScripts);
  452. }
  453. needsRequestInterception() {
  454. return this.requestInterceptors.length > 0 || this.browserContext.requestInterceptors.length > 0;
  455. }
  456. async addRequestInterceptor(progress, handler, prepend) {
  457. if (prepend)
  458. this.requestInterceptors.unshift(handler);
  459. else
  460. this.requestInterceptors.push(handler);
  461. await this.delegate.updateRequestInterception();
  462. }
  463. async removeRequestInterceptor(handler) {
  464. const index = this.requestInterceptors.indexOf(handler);
  465. if (index === -1)
  466. return;
  467. this.requestInterceptors.splice(index, 1);
  468. await this.browserContext.notifyRoutesInFlightAboutRemovedHandler(handler);
  469. await this.delegate.updateRequestInterception();
  470. }
  471. async expectScreenshot(progress, options) {
  472. const locator = options.locator;
  473. const rafrafScreenshot = locator ? async (timeout) => {
  474. return await locator.frame.rafrafTimeoutScreenshotElementWithProgress(progress, locator.selector, timeout, options || {});
  475. } : async (timeout) => {
  476. await this.performActionPreChecks(progress);
  477. await this.mainFrame().rafrafTimeout(progress, timeout);
  478. return await this.screenshotter.screenshotPage(progress, options || {});
  479. };
  480. const comparator = (0, import_comparators.getComparator)("image/png");
  481. if (!options.expected && options.isNot)
  482. return { errorMessage: '"not" matcher requires expected result' };
  483. try {
  484. const format = (0, import_screenshotter.validateScreenshotOptions)(options || {});
  485. if (format !== "png")
  486. throw new Error("Only PNG screenshots are supported");
  487. } catch (error) {
  488. return { errorMessage: error.message };
  489. }
  490. let intermediateResult;
  491. const areEqualScreenshots = (actual, expected, previous) => {
  492. const comparatorResult = actual && expected ? comparator(actual, expected, options) : void 0;
  493. if (comparatorResult !== void 0 && !!comparatorResult === !!options.isNot)
  494. return true;
  495. if (comparatorResult)
  496. intermediateResult = { errorMessage: comparatorResult.errorMessage, diff: comparatorResult.diff, actual, previous };
  497. return false;
  498. };
  499. try {
  500. let actual;
  501. let previous;
  502. const pollIntervals = [0, 100, 250, 500];
  503. progress.log(`${(0, import_utils.renderTitleForCall)(progress.metadata)}${options.timeout ? ` with timeout ${options.timeout}ms` : ""}`);
  504. if (options.expected)
  505. progress.log(` verifying given screenshot expectation`);
  506. else
  507. progress.log(` generating new stable screenshot expectation`);
  508. let isFirstIteration = true;
  509. while (true) {
  510. if (this.isClosed())
  511. throw new Error("The page has closed");
  512. const screenshotTimeout = pollIntervals.shift() ?? 1e3;
  513. if (screenshotTimeout)
  514. progress.log(`waiting ${screenshotTimeout}ms before taking screenshot`);
  515. previous = actual;
  516. actual = await rafrafScreenshot(screenshotTimeout).catch((e) => {
  517. if (this.mainFrame().isNonRetriableError(e))
  518. throw e;
  519. progress.log(`failed to take screenshot - ` + e.message);
  520. return void 0;
  521. });
  522. if (!actual)
  523. continue;
  524. const expectation = options.expected && isFirstIteration ? options.expected : previous;
  525. if (areEqualScreenshots(actual, expectation, previous))
  526. break;
  527. if (intermediateResult)
  528. progress.log(intermediateResult.errorMessage);
  529. isFirstIteration = false;
  530. }
  531. if (!isFirstIteration)
  532. progress.log(`captured a stable screenshot`);
  533. if (!options.expected)
  534. return { actual };
  535. if (isFirstIteration) {
  536. progress.log(`screenshot matched expectation`);
  537. return {};
  538. }
  539. if (areEqualScreenshots(actual, options.expected, void 0)) {
  540. progress.log(`screenshot matched expectation`);
  541. return {};
  542. }
  543. throw new Error(intermediateResult.errorMessage);
  544. } catch (e) {
  545. if (js.isJavaScriptErrorInEvaluate(e) || (0, import_selectorParser.isInvalidSelectorError)(e))
  546. throw e;
  547. let errorMessage = e.message;
  548. if (e instanceof import_errors.TimeoutError && intermediateResult?.previous)
  549. errorMessage = `Failed to take two consecutive stable screenshots.`;
  550. return {
  551. log: (0, import_callLog.compressCallLog)(e.message ? [...progress.metadata.log, e.message] : progress.metadata.log),
  552. ...intermediateResult,
  553. errorMessage,
  554. timedOut: e instanceof import_errors.TimeoutError
  555. };
  556. }
  557. }
  558. async screenshot(progress, options) {
  559. return await this.screenshotter.screenshotPage(progress, options);
  560. }
  561. async close(options = {}) {
  562. if (this._closedState === "closed")
  563. return;
  564. if (options.reason)
  565. this.closeReason = options.reason;
  566. const runBeforeUnload = !!options.runBeforeUnload;
  567. if (this._closedState !== "closing") {
  568. this._closedState = "closing";
  569. await this.delegate.closePage(runBeforeUnload).catch((e) => import_debugLogger.debugLogger.log("error", e));
  570. }
  571. if (!runBeforeUnload)
  572. await this._closedPromise;
  573. }
  574. isClosed() {
  575. return this._closedState === "closed";
  576. }
  577. hasCrashed() {
  578. return this._crashed;
  579. }
  580. isClosedOrClosingOrCrashed() {
  581. return this._closedState !== "open" || this._crashed;
  582. }
  583. addWorker(workerId, worker) {
  584. this._workers.set(workerId, worker);
  585. this.emit(Page.Events.Worker, worker);
  586. }
  587. removeWorker(workerId) {
  588. const worker = this._workers.get(workerId);
  589. if (!worker)
  590. return;
  591. worker.didClose();
  592. this._workers.delete(workerId);
  593. }
  594. clearWorkers() {
  595. for (const [workerId, worker] of this._workers) {
  596. worker.didClose();
  597. this._workers.delete(workerId);
  598. }
  599. }
  600. async setFileChooserInterceptedBy(enabled, by) {
  601. const wasIntercepted = this.fileChooserIntercepted();
  602. if (enabled)
  603. this._fileChooserInterceptedBy.add(by);
  604. else
  605. this._fileChooserInterceptedBy.delete(by);
  606. if (wasIntercepted !== this.fileChooserIntercepted())
  607. await this.delegate.updateFileChooserInterception();
  608. }
  609. fileChooserIntercepted() {
  610. return this._fileChooserInterceptedBy.size > 0;
  611. }
  612. frameNavigatedToNewDocument(frame) {
  613. this.emit(Page.Events.InternalFrameNavigatedToNewDocument, frame);
  614. const origin = frame.origin();
  615. if (origin)
  616. this.browserContext.addVisitedOrigin(origin);
  617. }
  618. allInitScripts() {
  619. const bindings = [...this.browserContext._pageBindings.values(), ...this._pageBindings.values()].map((binding) => binding.initScript);
  620. if (this.browserContext.bindingsInitScript)
  621. bindings.unshift(this.browserContext.bindingsInitScript);
  622. return [...bindings, ...this.browserContext.initScripts, ...this.initScripts];
  623. }
  624. getBinding(name) {
  625. return this._pageBindings.get(name) || this.browserContext._pageBindings.get(name);
  626. }
  627. setScreencastOptions(options) {
  628. this.delegate.setScreencastOptions(options).catch((e) => import_debugLogger.debugLogger.log("error", e));
  629. this._frameThrottler.setThrottlingEnabled(!!options);
  630. }
  631. throttleScreencastFrameAck(ack) {
  632. this._frameThrottler.ack(ack);
  633. }
  634. temporarilyDisableTracingScreencastThrottling() {
  635. this._frameThrottler.recharge();
  636. }
  637. async safeNonStallingEvaluateInAllFrames(expression, world, options = {}) {
  638. await Promise.all(this.frames().map(async (frame) => {
  639. try {
  640. await frame.nonStallingEvaluateInExistingContext(expression, world);
  641. } catch (e) {
  642. if (options.throwOnJSErrors && js.isJavaScriptErrorInEvaluate(e))
  643. throw e;
  644. }
  645. }));
  646. }
  647. async hideHighlight() {
  648. await Promise.all(this.frames().map((frame) => frame.hideHighlight().catch(() => {
  649. })));
  650. }
  651. async snapshotForAI(progress) {
  652. this.lastSnapshotFrameIds = [];
  653. const snapshot = await snapshotFrameForAI(progress, this.mainFrame(), 0, this.lastSnapshotFrameIds);
  654. return snapshot.join("\n");
  655. }
  656. }
  657. class Worker extends import_instrumentation.SdkObject {
  658. constructor(parent, url) {
  659. super(parent, "worker");
  660. this.existingExecutionContext = null;
  661. this.openScope = new import_utils.LongStandingScope();
  662. this.url = url;
  663. this._executionContextCallback = () => {
  664. };
  665. this._executionContextPromise = new Promise((x) => this._executionContextCallback = x);
  666. }
  667. static {
  668. this.Events = {
  669. Close: "close"
  670. };
  671. }
  672. createExecutionContext(delegate) {
  673. this.existingExecutionContext = new js.ExecutionContext(this, delegate, "worker");
  674. this._executionContextCallback(this.existingExecutionContext);
  675. return this.existingExecutionContext;
  676. }
  677. didClose() {
  678. if (this.existingExecutionContext)
  679. this.existingExecutionContext.contextDestroyed("Worker was closed");
  680. this.emit(Worker.Events.Close, this);
  681. this.openScope.close(new Error("Worker closed"));
  682. }
  683. async evaluateExpression(expression, isFunction, arg) {
  684. return js.evaluateExpression(await this._executionContextPromise, expression, { returnByValue: true, isFunction }, arg);
  685. }
  686. async evaluateExpressionHandle(expression, isFunction, arg) {
  687. return js.evaluateExpression(await this._executionContextPromise, expression, { returnByValue: false, isFunction }, arg);
  688. }
  689. }
  690. class PageBinding {
  691. static {
  692. this.kController = "__playwright__binding__controller__";
  693. }
  694. static {
  695. this.kBindingName = "__playwright__binding__";
  696. }
  697. static createInitScript() {
  698. return new InitScript(`
  699. (() => {
  700. const module = {};
  701. ${rawBindingsControllerSource.source}
  702. const property = '${PageBinding.kController}';
  703. if (!globalThis[property])
  704. globalThis[property] = new (module.exports.BindingsController())(globalThis, '${PageBinding.kBindingName}');
  705. })();
  706. `);
  707. }
  708. constructor(name, playwrightFunction, needsHandle) {
  709. this.name = name;
  710. this.playwrightFunction = playwrightFunction;
  711. this.initScript = new InitScript(`globalThis['${PageBinding.kController}'].addBinding(${JSON.stringify(name)}, ${needsHandle})`);
  712. this.needsHandle = needsHandle;
  713. this.cleanupScript = `globalThis['${PageBinding.kController}'].removeBinding(${JSON.stringify(name)})`;
  714. }
  715. static async dispatch(page, payload, context) {
  716. const { name, seq, serializedArgs } = JSON.parse(payload);
  717. try {
  718. (0, import_utils.assert)(context.world);
  719. const binding = page.getBinding(name);
  720. if (!binding)
  721. throw new Error(`Function "${name}" is not exposed`);
  722. let result;
  723. if (binding.needsHandle) {
  724. const handle = await context.evaluateExpressionHandle(`arg => globalThis['${PageBinding.kController}'].takeBindingHandle(arg)`, { isFunction: true }, { name, seq }).catch((e) => null);
  725. result = await binding.playwrightFunction({ frame: context.frame, page, context: page.browserContext }, handle);
  726. } else {
  727. if (!Array.isArray(serializedArgs))
  728. throw new Error(`serializedArgs is not an array. This can happen when Array.prototype.toJSON is defined incorrectly`);
  729. const args = serializedArgs.map((a) => (0, import_utilityScriptSerializers.parseEvaluationResultValue)(a));
  730. result = await binding.playwrightFunction({ frame: context.frame, page, context: page.browserContext }, ...args);
  731. }
  732. context.evaluateExpressionHandle(`arg => globalThis['${PageBinding.kController}'].deliverBindingResult(arg)`, { isFunction: true }, { name, seq, result }).catch((e) => import_debugLogger.debugLogger.log("error", e));
  733. } catch (error) {
  734. context.evaluateExpressionHandle(`arg => globalThis['${PageBinding.kController}'].deliverBindingResult(arg)`, { isFunction: true }, { name, seq, error }).catch((e) => import_debugLogger.debugLogger.log("error", e));
  735. }
  736. }
  737. }
  738. class InitScript {
  739. constructor(source) {
  740. this.source = `(() => {
  741. ${source}
  742. })();`;
  743. }
  744. }
  745. class FrameThrottler {
  746. constructor(nonThrottledFrames, defaultInterval, throttlingInterval) {
  747. this._acks = [];
  748. this._throttlingEnabled = false;
  749. this._nonThrottledFrames = nonThrottledFrames;
  750. this._budget = nonThrottledFrames;
  751. this._defaultInterval = defaultInterval;
  752. this._throttlingInterval = throttlingInterval;
  753. this._tick();
  754. }
  755. dispose() {
  756. if (this._timeoutId) {
  757. clearTimeout(this._timeoutId);
  758. this._timeoutId = void 0;
  759. }
  760. }
  761. setThrottlingEnabled(enabled) {
  762. this._throttlingEnabled = enabled;
  763. }
  764. recharge() {
  765. for (const ack of this._acks)
  766. ack();
  767. this._acks = [];
  768. this._budget = this._nonThrottledFrames;
  769. if (this._timeoutId) {
  770. clearTimeout(this._timeoutId);
  771. this._tick();
  772. }
  773. }
  774. ack(ack) {
  775. if (!this._timeoutId) {
  776. ack();
  777. return;
  778. }
  779. this._acks.push(ack);
  780. }
  781. _tick() {
  782. const ack = this._acks.shift();
  783. if (ack) {
  784. --this._budget;
  785. ack();
  786. }
  787. if (this._throttlingEnabled && this._budget <= 0) {
  788. this._timeoutId = setTimeout(() => this._tick(), this._throttlingInterval);
  789. } else {
  790. this._timeoutId = setTimeout(() => this._tick(), this._defaultInterval);
  791. }
  792. }
  793. }
  794. async function snapshotFrameForAI(progress, frame, frameOrdinal, frameIds) {
  795. const snapshot = await frame.retryWithProgressAndTimeouts(progress, [1e3, 2e3, 4e3, 8e3], async (continuePolling) => {
  796. try {
  797. const context = await progress.race(frame._utilityContext());
  798. const injectedScript = await progress.race(context.injectedScript());
  799. const snapshotOrRetry = await progress.race(injectedScript.evaluate((injected, refPrefix) => {
  800. const node = injected.document.body;
  801. if (!node)
  802. return true;
  803. return injected.ariaSnapshot(node, { mode: "ai", refPrefix });
  804. }, frameOrdinal ? "f" + frameOrdinal : ""));
  805. if (snapshotOrRetry === true)
  806. return continuePolling;
  807. return snapshotOrRetry;
  808. } catch (e) {
  809. if (frame.isNonRetriableError(e))
  810. throw e;
  811. return continuePolling;
  812. }
  813. });
  814. const lines = snapshot.split("\n");
  815. const result = [];
  816. for (const line of lines) {
  817. const match = line.match(/^(\s*)- iframe (?:\[active\] )?\[ref=([^\]]*)\]/);
  818. if (!match) {
  819. result.push(line);
  820. continue;
  821. }
  822. const leadingSpace = match[1];
  823. const ref = match[2];
  824. const frameSelector = `aria-ref=${ref} >> internal:control=enter-frame`;
  825. const frameBodySelector = `${frameSelector} >> body`;
  826. const child = await progress.race(frame.selectors.resolveFrameForSelector(frameBodySelector, { strict: true }));
  827. if (!child) {
  828. result.push(line);
  829. continue;
  830. }
  831. const frameOrdinal2 = frameIds.length + 1;
  832. frameIds.push(child.frame._id);
  833. try {
  834. const childSnapshot = await snapshotFrameForAI(progress, child.frame, frameOrdinal2, frameIds);
  835. result.push(line + ":", ...childSnapshot.map((l) => leadingSpace + " " + l));
  836. } catch {
  837. result.push(line);
  838. }
  839. }
  840. return result;
  841. }
  842. // Annotate the CommonJS export names for ESM import in node:
  843. 0 && (module.exports = {
  844. InitScript,
  845. Page,
  846. PageBinding,
  847. Worker
  848. });