frame.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 frame_exports = {};
  30. __export(frame_exports, {
  31. Frame: () => Frame,
  32. verifyLoadState: () => verifyLoadState
  33. });
  34. module.exports = __toCommonJS(frame_exports);
  35. var import_eventEmitter = require("./eventEmitter");
  36. var import_channelOwner = require("./channelOwner");
  37. var import_clientHelper = require("./clientHelper");
  38. var import_elementHandle = require("./elementHandle");
  39. var import_events = require("./events");
  40. var import_jsHandle = require("./jsHandle");
  41. var import_locator = require("./locator");
  42. var network = __toESM(require("./network"));
  43. var import_types = require("./types");
  44. var import_waiter = require("./waiter");
  45. var import_assert = require("../utils/isomorphic/assert");
  46. var import_locatorUtils = require("../utils/isomorphic/locatorUtils");
  47. var import_urlMatch = require("../utils/isomorphic/urlMatch");
  48. var import_timeoutSettings = require("./timeoutSettings");
  49. class Frame extends import_channelOwner.ChannelOwner {
  50. constructor(parent, type, guid, initializer) {
  51. super(parent, type, guid, initializer);
  52. this._parentFrame = null;
  53. this._url = "";
  54. this._name = "";
  55. this._detached = false;
  56. this._childFrames = /* @__PURE__ */ new Set();
  57. this._eventEmitter = new import_eventEmitter.EventEmitter(parent._platform);
  58. this._eventEmitter.setMaxListeners(0);
  59. this._parentFrame = Frame.fromNullable(initializer.parentFrame);
  60. if (this._parentFrame)
  61. this._parentFrame._childFrames.add(this);
  62. this._name = initializer.name;
  63. this._url = initializer.url;
  64. this._loadStates = new Set(initializer.loadStates);
  65. this._channel.on("loadstate", (event) => {
  66. if (event.add) {
  67. this._loadStates.add(event.add);
  68. this._eventEmitter.emit("loadstate", event.add);
  69. }
  70. if (event.remove)
  71. this._loadStates.delete(event.remove);
  72. if (!this._parentFrame && event.add === "load" && this._page)
  73. this._page.emit(import_events.Events.Page.Load, this._page);
  74. if (!this._parentFrame && event.add === "domcontentloaded" && this._page)
  75. this._page.emit(import_events.Events.Page.DOMContentLoaded, this._page);
  76. });
  77. this._channel.on("navigated", (event) => {
  78. this._url = event.url;
  79. this._name = event.name;
  80. this._eventEmitter.emit("navigated", event);
  81. if (!event.error && this._page)
  82. this._page.emit(import_events.Events.Page.FrameNavigated, this);
  83. });
  84. }
  85. static from(frame) {
  86. return frame._object;
  87. }
  88. static fromNullable(frame) {
  89. return frame ? Frame.from(frame) : null;
  90. }
  91. page() {
  92. return this._page;
  93. }
  94. _timeout(options) {
  95. const timeoutSettings = this._page?._timeoutSettings || new import_timeoutSettings.TimeoutSettings(this._platform);
  96. return timeoutSettings.timeout(options || {});
  97. }
  98. _navigationTimeout(options) {
  99. const timeoutSettings = this._page?._timeoutSettings || new import_timeoutSettings.TimeoutSettings(this._platform);
  100. return timeoutSettings.navigationTimeout(options || {});
  101. }
  102. async goto(url, options = {}) {
  103. const waitUntil = verifyLoadState("waitUntil", options.waitUntil === void 0 ? "load" : options.waitUntil);
  104. this.page().context()._checkUrlAllowed(url);
  105. return network.Response.fromNullable((await this._channel.goto({ url, ...options, waitUntil, timeout: this._navigationTimeout(options) })).response);
  106. }
  107. _setupNavigationWaiter(options) {
  108. const waiter = new import_waiter.Waiter(this._page, "");
  109. if (this._page.isClosed())
  110. waiter.rejectImmediately(this._page._closeErrorWithReason());
  111. waiter.rejectOnEvent(this._page, import_events.Events.Page.Close, () => this._page._closeErrorWithReason());
  112. waiter.rejectOnEvent(this._page, import_events.Events.Page.Crash, new Error("Navigation failed because page crashed!"));
  113. waiter.rejectOnEvent(this._page, import_events.Events.Page.FrameDetached, new Error("Navigating frame was detached!"), (frame) => frame === this);
  114. const timeout = this._page._timeoutSettings.navigationTimeout(options);
  115. waiter.rejectOnTimeout(timeout, `Timeout ${timeout}ms exceeded.`);
  116. return waiter;
  117. }
  118. async waitForNavigation(options = {}) {
  119. return await this._page._wrapApiCall(async () => {
  120. const waitUntil = verifyLoadState("waitUntil", options.waitUntil === void 0 ? "load" : options.waitUntil);
  121. const waiter = this._setupNavigationWaiter(options);
  122. const toUrl = typeof options.url === "string" ? ` to "${options.url}"` : "";
  123. waiter.log(`waiting for navigation${toUrl} until "${waitUntil}"`);
  124. const navigatedEvent = await waiter.waitForEvent(this._eventEmitter, "navigated", (event) => {
  125. if (event.error)
  126. return true;
  127. waiter.log(` navigated to "${event.url}"`);
  128. return (0, import_urlMatch.urlMatches)(this._page?.context()._options.baseURL, event.url, options.url);
  129. });
  130. if (navigatedEvent.error) {
  131. const e = new Error(navigatedEvent.error);
  132. e.stack = "";
  133. await waiter.waitForPromise(Promise.reject(e));
  134. }
  135. if (!this._loadStates.has(waitUntil)) {
  136. await waiter.waitForEvent(this._eventEmitter, "loadstate", (s) => {
  137. waiter.log(` "${s}" event fired`);
  138. return s === waitUntil;
  139. });
  140. }
  141. const request = navigatedEvent.newDocument ? network.Request.fromNullable(navigatedEvent.newDocument.request) : null;
  142. const response = request ? await waiter.waitForPromise(request._finalRequest()._internalResponse()) : null;
  143. waiter.dispose();
  144. return response;
  145. }, { title: "Wait for navigation" });
  146. }
  147. async waitForLoadState(state = "load", options = {}) {
  148. state = verifyLoadState("state", state);
  149. return await this._page._wrapApiCall(async () => {
  150. const waiter = this._setupNavigationWaiter(options);
  151. if (this._loadStates.has(state)) {
  152. waiter.log(` not waiting, "${state}" event already fired`);
  153. } else {
  154. await waiter.waitForEvent(this._eventEmitter, "loadstate", (s) => {
  155. waiter.log(` "${s}" event fired`);
  156. return s === state;
  157. });
  158. }
  159. waiter.dispose();
  160. }, { title: `Wait for load state "${state}"` });
  161. }
  162. async waitForURL(url, options = {}) {
  163. if ((0, import_urlMatch.urlMatches)(this._page?.context()._options.baseURL, this.url(), url))
  164. return await this.waitForLoadState(options.waitUntil, options);
  165. await this.waitForNavigation({ url, ...options });
  166. }
  167. async frameElement() {
  168. return import_elementHandle.ElementHandle.from((await this._channel.frameElement()).element);
  169. }
  170. async evaluateHandle(pageFunction, arg) {
  171. (0, import_jsHandle.assertMaxArguments)(arguments.length, 2);
  172. const result = await this._channel.evaluateExpressionHandle({ expression: String(pageFunction), isFunction: typeof pageFunction === "function", arg: (0, import_jsHandle.serializeArgument)(arg) });
  173. return import_jsHandle.JSHandle.from(result.handle);
  174. }
  175. async evaluate(pageFunction, arg) {
  176. (0, import_jsHandle.assertMaxArguments)(arguments.length, 2);
  177. const result = await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === "function", arg: (0, import_jsHandle.serializeArgument)(arg) });
  178. return (0, import_jsHandle.parseResult)(result.value);
  179. }
  180. async _evaluateFunction(functionDeclaration) {
  181. const result = await this._channel.evaluateExpression({ expression: functionDeclaration, isFunction: true, arg: (0, import_jsHandle.serializeArgument)(void 0) });
  182. return (0, import_jsHandle.parseResult)(result.value);
  183. }
  184. async _evaluateExposeUtilityScript(pageFunction, arg) {
  185. (0, import_jsHandle.assertMaxArguments)(arguments.length, 2);
  186. const result = await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === "function", arg: (0, import_jsHandle.serializeArgument)(arg) });
  187. return (0, import_jsHandle.parseResult)(result.value);
  188. }
  189. async $(selector, options) {
  190. const result = await this._channel.querySelector({ selector, ...options });
  191. return import_elementHandle.ElementHandle.fromNullable(result.element);
  192. }
  193. async waitForSelector(selector, options = {}) {
  194. if (options.visibility)
  195. throw new Error("options.visibility is not supported, did you mean options.state?");
  196. if (options.waitFor && options.waitFor !== "visible")
  197. throw new Error("options.waitFor is not supported, did you mean options.state?");
  198. const result = await this._channel.waitForSelector({ selector, ...options, timeout: this._timeout(options) });
  199. return import_elementHandle.ElementHandle.fromNullable(result.element);
  200. }
  201. async dispatchEvent(selector, type, eventInit, options = {}) {
  202. await this._channel.dispatchEvent({ selector, type, eventInit: (0, import_jsHandle.serializeArgument)(eventInit), ...options, timeout: this._timeout(options) });
  203. }
  204. async $eval(selector, pageFunction, arg) {
  205. (0, import_jsHandle.assertMaxArguments)(arguments.length, 3);
  206. const result = await this._channel.evalOnSelector({ selector, expression: String(pageFunction), isFunction: typeof pageFunction === "function", arg: (0, import_jsHandle.serializeArgument)(arg) });
  207. return (0, import_jsHandle.parseResult)(result.value);
  208. }
  209. async $$eval(selector, pageFunction, arg) {
  210. (0, import_jsHandle.assertMaxArguments)(arguments.length, 3);
  211. const result = await this._channel.evalOnSelectorAll({ selector, expression: String(pageFunction), isFunction: typeof pageFunction === "function", arg: (0, import_jsHandle.serializeArgument)(arg) });
  212. return (0, import_jsHandle.parseResult)(result.value);
  213. }
  214. async $$(selector) {
  215. const result = await this._channel.querySelectorAll({ selector });
  216. return result.elements.map((e) => import_elementHandle.ElementHandle.from(e));
  217. }
  218. async _queryCount(selector, options) {
  219. return (await this._channel.queryCount({ selector, ...options })).value;
  220. }
  221. async content() {
  222. return (await this._channel.content()).value;
  223. }
  224. async setContent(html, options = {}) {
  225. const waitUntil = verifyLoadState("waitUntil", options.waitUntil === void 0 ? "load" : options.waitUntil);
  226. await this._channel.setContent({ html, ...options, waitUntil, timeout: this._navigationTimeout(options) });
  227. }
  228. name() {
  229. return this._name || "";
  230. }
  231. url() {
  232. return this._url;
  233. }
  234. parentFrame() {
  235. return this._parentFrame;
  236. }
  237. childFrames() {
  238. return Array.from(this._childFrames);
  239. }
  240. isDetached() {
  241. return this._detached;
  242. }
  243. async addScriptTag(options = {}) {
  244. const copy = { ...options };
  245. if (copy.path) {
  246. copy.content = (await this._platform.fs().promises.readFile(copy.path)).toString();
  247. copy.content = (0, import_clientHelper.addSourceUrlToScript)(copy.content, copy.path);
  248. }
  249. return import_elementHandle.ElementHandle.from((await this._channel.addScriptTag({ ...copy })).element);
  250. }
  251. async addStyleTag(options = {}) {
  252. const copy = { ...options };
  253. if (copy.path) {
  254. copy.content = (await this._platform.fs().promises.readFile(copy.path)).toString();
  255. copy.content += "/*# sourceURL=" + copy.path.replace(/\n/g, "") + "*/";
  256. }
  257. return import_elementHandle.ElementHandle.from((await this._channel.addStyleTag({ ...copy })).element);
  258. }
  259. async click(selector, options = {}) {
  260. return await this._channel.click({ selector, ...options, timeout: this._timeout(options) });
  261. }
  262. async dblclick(selector, options = {}) {
  263. return await this._channel.dblclick({ selector, ...options, timeout: this._timeout(options) });
  264. }
  265. async dragAndDrop(source, target, options = {}) {
  266. return await this._channel.dragAndDrop({ source, target, ...options, timeout: this._timeout(options) });
  267. }
  268. async tap(selector, options = {}) {
  269. return await this._channel.tap({ selector, ...options, timeout: this._timeout(options) });
  270. }
  271. async fill(selector, value, options = {}) {
  272. return await this._channel.fill({ selector, value, ...options, timeout: this._timeout(options) });
  273. }
  274. async _highlight(selector) {
  275. return await this._channel.highlight({ selector });
  276. }
  277. locator(selector, options) {
  278. return new import_locator.Locator(this, selector, options);
  279. }
  280. getByTestId(testId) {
  281. return this.locator((0, import_locatorUtils.getByTestIdSelector)((0, import_locator.testIdAttributeName)(), testId));
  282. }
  283. getByAltText(text, options) {
  284. return this.locator((0, import_locatorUtils.getByAltTextSelector)(text, options));
  285. }
  286. getByLabel(text, options) {
  287. return this.locator((0, import_locatorUtils.getByLabelSelector)(text, options));
  288. }
  289. getByPlaceholder(text, options) {
  290. return this.locator((0, import_locatorUtils.getByPlaceholderSelector)(text, options));
  291. }
  292. getByText(text, options) {
  293. return this.locator((0, import_locatorUtils.getByTextSelector)(text, options));
  294. }
  295. getByTitle(text, options) {
  296. return this.locator((0, import_locatorUtils.getByTitleSelector)(text, options));
  297. }
  298. getByRole(role, options = {}) {
  299. return this.locator((0, import_locatorUtils.getByRoleSelector)(role, options));
  300. }
  301. frameLocator(selector) {
  302. return new import_locator.FrameLocator(this, selector);
  303. }
  304. async focus(selector, options = {}) {
  305. await this._channel.focus({ selector, ...options, timeout: this._timeout(options) });
  306. }
  307. async textContent(selector, options = {}) {
  308. const value = (await this._channel.textContent({ selector, ...options, timeout: this._timeout(options) })).value;
  309. return value === void 0 ? null : value;
  310. }
  311. async innerText(selector, options = {}) {
  312. return (await this._channel.innerText({ selector, ...options, timeout: this._timeout(options) })).value;
  313. }
  314. async innerHTML(selector, options = {}) {
  315. return (await this._channel.innerHTML({ selector, ...options, timeout: this._timeout(options) })).value;
  316. }
  317. async getAttribute(selector, name, options = {}) {
  318. const value = (await this._channel.getAttribute({ selector, name, ...options, timeout: this._timeout(options) })).value;
  319. return value === void 0 ? null : value;
  320. }
  321. async inputValue(selector, options = {}) {
  322. return (await this._channel.inputValue({ selector, ...options, timeout: this._timeout(options) })).value;
  323. }
  324. async isChecked(selector, options = {}) {
  325. return (await this._channel.isChecked({ selector, ...options, timeout: this._timeout(options) })).value;
  326. }
  327. async isDisabled(selector, options = {}) {
  328. return (await this._channel.isDisabled({ selector, ...options, timeout: this._timeout(options) })).value;
  329. }
  330. async isEditable(selector, options = {}) {
  331. return (await this._channel.isEditable({ selector, ...options, timeout: this._timeout(options) })).value;
  332. }
  333. async isEnabled(selector, options = {}) {
  334. return (await this._channel.isEnabled({ selector, ...options, timeout: this._timeout(options) })).value;
  335. }
  336. async isHidden(selector, options = {}) {
  337. return (await this._channel.isHidden({ selector, ...options })).value;
  338. }
  339. async isVisible(selector, options = {}) {
  340. return (await this._channel.isVisible({ selector, ...options })).value;
  341. }
  342. async hover(selector, options = {}) {
  343. await this._channel.hover({ selector, ...options, timeout: this._timeout(options) });
  344. }
  345. async selectOption(selector, values, options = {}) {
  346. return (await this._channel.selectOption({ selector, ...(0, import_elementHandle.convertSelectOptionValues)(values), ...options, timeout: this._timeout(options) })).values;
  347. }
  348. async setInputFiles(selector, files, options = {}) {
  349. const converted = await (0, import_elementHandle.convertInputFiles)(this._platform, files, this.page().context());
  350. await this._channel.setInputFiles({ selector, ...converted, ...options, timeout: this._timeout(options) });
  351. }
  352. async type(selector, text, options = {}) {
  353. await this._channel.type({ selector, text, ...options, timeout: this._timeout(options) });
  354. }
  355. async press(selector, key, options = {}) {
  356. await this._channel.press({ selector, key, ...options, timeout: this._timeout(options) });
  357. }
  358. async check(selector, options = {}) {
  359. await this._channel.check({ selector, ...options, timeout: this._timeout(options) });
  360. }
  361. async uncheck(selector, options = {}) {
  362. await this._channel.uncheck({ selector, ...options, timeout: this._timeout(options) });
  363. }
  364. async setChecked(selector, checked, options) {
  365. if (checked)
  366. await this.check(selector, options);
  367. else
  368. await this.uncheck(selector, options);
  369. }
  370. async waitForTimeout(timeout) {
  371. await this._channel.waitForTimeout({ waitTimeout: timeout });
  372. }
  373. async waitForFunction(pageFunction, arg, options = {}) {
  374. if (typeof options.polling === "string")
  375. (0, import_assert.assert)(options.polling === "raf", "Unknown polling option: " + options.polling);
  376. const result = await this._channel.waitForFunction({
  377. ...options,
  378. pollingInterval: options.polling === "raf" ? void 0 : options.polling,
  379. expression: String(pageFunction),
  380. isFunction: typeof pageFunction === "function",
  381. arg: (0, import_jsHandle.serializeArgument)(arg),
  382. timeout: this._timeout(options)
  383. });
  384. return import_jsHandle.JSHandle.from(result.handle);
  385. }
  386. async title() {
  387. return (await this._channel.title()).value;
  388. }
  389. async _expect(expression, options) {
  390. const params = { expression, ...options, isNot: !!options.isNot };
  391. params.expectedValue = (0, import_jsHandle.serializeArgument)(options.expectedValue);
  392. const result = await this._channel.expect(params);
  393. if (result.received !== void 0)
  394. result.received = (0, import_jsHandle.parseResult)(result.received);
  395. return result;
  396. }
  397. }
  398. function verifyLoadState(name, waitUntil) {
  399. if (waitUntil === "networkidle0")
  400. waitUntil = "networkidle";
  401. if (!import_types.kLifecycleEvents.has(waitUntil))
  402. throw new Error(`${name}: expected one of (load|domcontentloaded|networkidle|commit)`);
  403. return waitUntil;
  404. }
  405. // Annotate the CommonJS export names for ESM import in node:
  406. 0 && (module.exports = {
  407. Frame,
  408. verifyLoadState
  409. });