Best JavaScript code snippet using playwright-internal
frames.js
Source:frames.js
...551 return value;552 }553 async querySelector(selector, options) {554 _debugLogger.debugLogger.log('api', ` finding element using the selector "${selector}"`);555 const result = await this.resolveFrameForSelectorNoWait(selector, options);556 if (!result) return null;557 return this._page.selectors.query(result.frame, result.info);558 }559 async waitForSelector(metadata, selector, options, scope) {560 const controller = new _progress.ProgressController(metadata, this);561 if (options.visibility) throw new Error('options.visibility is not supported, did you mean options.state?');562 if (options.waitFor && options.waitFor !== 'visible') throw new Error('options.waitFor is not supported, did you mean options.state?');563 const {564 state = 'visible'565 } = options;566 if (!['attached', 'detached', 'visible', 'hidden'].includes(state)) throw new Error(`state: expected one of (attached|detached|visible|hidden)`);567 return controller.run(async progress => {568 progress.log(`waiting for selector "${selector}"${state === 'attached' ? '' : ' to be ' + state}`);569 return this.retryWithProgress(progress, selector, options, async (selectorInFrame, continuePolling) => {570 // Be careful, |this| can be different from |frame|.571 // We did not pass omitAttached, so it is non-null.572 const {573 frame,574 info575 } = selectorInFrame;576 const actualScope = this === frame ? scope : undefined;577 const task = dom.waitForSelectorTask(info, state, options.omitReturnValue, actualScope);578 const result = actualScope ? await frame._runWaitForSelectorTaskOnce(progress, (0, _selectorParser.stringifySelector)(info.parsed), info.world, task) : await frame._scheduleRerunnableHandleTask(progress, info.world, task);579 if (!result.asElement()) {580 result.dispose();581 return null;582 }583 if (options.__testHookBeforeAdoptNode) await options.__testHookBeforeAdoptNode();584 const handle = result.asElement();585 try {586 return await handle._adoptTo(await frame._mainContext());587 } catch (e) {588 return continuePolling;589 }590 }, scope);591 }, this._page._timeoutSettings.timeout(options));592 }593 async dispatchEvent(metadata, selector, type, eventInit = {}, options = {}) {594 await this._scheduleRerunnableTask(metadata, selector, (progress, element, data) => {595 progress.injectedScript.dispatchEvent(element, data.type, data.eventInit);596 }, {597 type,598 eventInit599 }, {600 mainWorld: true,601 ...options602 });603 await this._page._doSlowMo();604 }605 async evalOnSelectorAndWaitForSignals(selector, strict, expression, isFunction, arg) {606 const pair = await this.resolveFrameForSelectorNoWait(selector, {607 strict608 });609 const handle = pair ? await this._page.selectors.query(pair.frame, pair.info) : null;610 if (!handle) throw new Error(`Error: failed to find element matching selector "${selector}"`);611 const result = await handle.evaluateExpressionAndWaitForSignals(expression, isFunction, true, arg);612 handle.dispose();613 return result;614 }615 async evalOnSelectorAllAndWaitForSignals(selector, expression, isFunction, arg) {616 const pair = await this.resolveFrameForSelectorNoWait(selector, {});617 if (!pair) throw new Error(`Error: failed to find frame for selector "${selector}"`);618 const arrayHandle = await this._page.selectors._queryArrayInMainWorld(pair.frame, pair.info);619 const result = await arrayHandle.evaluateExpressionAndWaitForSignals(expression, isFunction, true, arg);620 arrayHandle.dispose();621 return result;622 }623 async querySelectorAll(selector) {624 const pair = await this.resolveFrameForSelectorNoWait(selector, {});625 if (!pair) return [];626 return this._page.selectors._queryAll(pair.frame, pair.info, undefined, true627 /* adoptToMain */628 );629 }630 async queryCount(selector) {631 const pair = await this.resolveFrameForSelectorNoWait(selector);632 if (!pair) throw new Error(`Error: failed to find frame for selector "${selector}"`);633 return await this._page.selectors._queryCount(pair.frame, pair.info);634 }635 async content() {636 try {637 const context = await this._utilityContext();638 return await context.evaluate(() => {639 let retVal = '';640 if (document.doctype) retVal = new XMLSerializer().serializeToString(document.doctype);641 if (document.documentElement) retVal += document.documentElement.outerHTML;642 return retVal;643 });644 } catch (e) {645 if (js.isJavaScriptErrorInEvaluate(e) || (0, _protocolError.isSessionClosedError)(e)) throw e;646 throw new Error(`Unable to retrieve content because the page is navigating and changing the content.`);647 }648 }649 async setContent(metadata, html, options = {}) {650 const controller = new _progress.ProgressController(metadata, this);651 return controller.run(progress => this.raceNavigationAction(async () => {652 const waitUntil = options.waitUntil === undefined ? 'load' : options.waitUntil;653 progress.log(`setting frame content, waiting until "${waitUntil}"`);654 const tag = `--playwright--set--content--${this._id}--${++this._setContentCounter}--`;655 const context = await this._utilityContext();656 const lifecyclePromise = new Promise((resolve, reject) => {657 this._page._frameManager._consoleMessageTags.set(tag, () => {658 // Clear lifecycle right after document.open() - see 'tag' below.659 this._onClearLifecycle();660 this._waitForLoadState(progress, waitUntil).then(resolve).catch(reject);661 });662 });663 const contentPromise = context.evaluate(({664 html,665 tag666 }) => {667 window.stop();668 document.open();669 console.debug(tag); // eslint-disable-line no-console670 document.write(html);671 document.close();672 }, {673 html,674 tag675 });676 await Promise.all([contentPromise, lifecyclePromise]);677 await this._page._doSlowMo();678 }), this._page._timeoutSettings.navigationTimeout(options));679 }680 name() {681 return this._name || '';682 }683 url() {684 return this._url;685 }686 parentFrame() {687 return this._parentFrame;688 }689 childFrames() {690 return Array.from(this._childFrames);691 }692 async addScriptTag(params) {693 const {694 url = null,695 content = null,696 type = ''697 } = params;698 if (!url && !content) throw new Error('Provide an object with a `url`, `path` or `content` property');699 const context = await this._mainContext();700 return this._raceWithCSPError(async () => {701 if (url !== null) return (await context.evaluateHandle(addScriptUrl, {702 url,703 type704 })).asElement();705 const result = (await context.evaluateHandle(addScriptContent, {706 content: content,707 type708 })).asElement(); // Another round trip to the browser to ensure that we receive CSP error messages709 // (if any) logged asynchronously in a separate task on the content main thread.710 if (this._page._delegate.cspErrorsAsynchronousForInlineScipts) await context.evaluate(() => true);711 return result;712 });713 async function addScriptUrl(params) {714 const script = document.createElement('script');715 script.src = params.url;716 if (params.type) script.type = params.type;717 const promise = new Promise((res, rej) => {718 script.onload = res;719 script.onerror = e => rej(typeof e === 'string' ? new Error(e) : new Error(`Failed to load script at ${script.src}`));720 });721 document.head.appendChild(script);722 await promise;723 return script;724 }725 function addScriptContent(params) {726 const script = document.createElement('script');727 script.type = params.type || 'text/javascript';728 script.text = params.content;729 let error = null;730 script.onerror = e => error = e;731 document.head.appendChild(script);732 if (error) throw error;733 return script;734 }735 }736 async addStyleTag(params) {737 const {738 url = null,739 content = null740 } = params;741 if (!url && !content) throw new Error('Provide an object with a `url`, `path` or `content` property');742 const context = await this._mainContext();743 return this._raceWithCSPError(async () => {744 if (url !== null) return (await context.evaluateHandle(addStyleUrl, url)).asElement();745 return (await context.evaluateHandle(addStyleContent, content)).asElement();746 });747 async function addStyleUrl(url) {748 const link = document.createElement('link');749 link.rel = 'stylesheet';750 link.href = url;751 const promise = new Promise((res, rej) => {752 link.onload = res;753 link.onerror = rej;754 });755 document.head.appendChild(link);756 await promise;757 return link;758 }759 async function addStyleContent(content) {760 const style = document.createElement('style');761 style.type = 'text/css';762 style.appendChild(document.createTextNode(content));763 const promise = new Promise((res, rej) => {764 style.onload = res;765 style.onerror = rej;766 });767 document.head.appendChild(style);768 await promise;769 return style;770 }771 }772 async _raceWithCSPError(func) {773 const listeners = [];774 let result;775 let error;776 let cspMessage;777 const actionPromise = func().then(r => result = r).catch(e => error = e);778 const errorPromise = new Promise(resolve => {779 listeners.push(_eventsHelper.eventsHelper.addEventListener(this._page, _page.Page.Events.Console, message => {780 if (message.type() === 'error' && message.text().includes('Content Security Policy')) {781 cspMessage = message;782 resolve();783 }784 }));785 });786 await Promise.race([actionPromise, errorPromise]);787 _eventsHelper.eventsHelper.removeEventListeners(listeners);788 if (cspMessage) throw new Error(cspMessage.text());789 if (error) throw error;790 return result;791 }792 async retryWithProgress(progress, selector, options, action, scope) {793 const continuePolling = Symbol('continuePolling');794 while (progress.isRunning()) {795 let selectorInFrame;796 if (options.omitAttached) {797 selectorInFrame = await this.resolveFrameForSelectorNoWait(selector, options, scope);798 } else {799 selectorInFrame = await this._resolveFrameForSelector(progress, selector, options, scope);800 if (!selectorInFrame) {801 // Missing content frame.802 await new Promise(f => setTimeout(f, 100));803 continue;804 }805 }806 try {807 const result = await action(selectorInFrame, continuePolling);808 if (result === continuePolling) continue;809 return result;810 } catch (e) {811 var _selectorInFrame;812 // Always fail on JavaScript errors or when the main connection is closed.813 if (js.isJavaScriptErrorInEvaluate(e) || (0, _protocolError.isSessionClosedError)(e)) throw e; // Certain error opt-out of the retries, throw.814 if (dom.isNonRecoverableDOMError(e)) throw e; // If the call is made on the detached frame - throw.815 if (this.isDetached()) throw e; // If there is scope, and scope is within the frame we use to select, assume context is destroyed and816 // operation is not recoverable.817 if (scope && scope._context.frame === ((_selectorInFrame = selectorInFrame) === null || _selectorInFrame === void 0 ? void 0 : _selectorInFrame.frame)) throw e; // Retry upon all other errors.818 continue;819 }820 }821 progress.throwIfAborted();822 return undefined;823 }824 async _retryWithProgressIfNotConnected(progress, selector, strict, action) {825 return this.retryWithProgress(progress, selector, {826 strict827 }, async (selectorInFrame, continuePolling) => {828 // We did not pass omitAttached, so selectorInFrame is not null.829 const {830 frame,831 info832 } = selectorInFrame; // Be careful, |this| can be different from |frame|.833 const task = dom.waitForSelectorTask(info, 'attached');834 progress.log(`waiting for selector "${selector}"`);835 const handle = await frame._scheduleRerunnableHandleTask(progress, info.world, task);836 const element = handle.asElement();837 try {838 const result = await action(element);839 if (result === 'error:notconnected') {840 progress.log('element was detached from the DOM, retrying');841 return continuePolling;842 }843 return result;844 } finally {845 element === null || element === void 0 ? void 0 : element.dispose();846 }847 });848 }849 async click(metadata, selector, options) {850 const controller = new _progress.ProgressController(metadata, this);851 return controller.run(async progress => {852 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._click(progress, options)));853 }, this._page._timeoutSettings.timeout(options));854 }855 async dblclick(metadata, selector, options = {}) {856 const controller = new _progress.ProgressController(metadata, this);857 return controller.run(async progress => {858 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._dblclick(progress, options)));859 }, this._page._timeoutSettings.timeout(options));860 }861 async dragAndDrop(metadata, source, target, options = {}) {862 const controller = new _progress.ProgressController(metadata, this);863 await controller.run(async progress => {864 dom.assertDone(await this._retryWithProgressIfNotConnected(progress, source, options.strict, async handle => {865 return handle._retryPointerAction(progress, 'move and down', false, async point => {866 await this._page.mouse.move(point.x, point.y);867 await this._page.mouse.down();868 }, { ...options,869 position: options.sourcePosition,870 timeout: progress.timeUntilDeadline()871 });872 }));873 dom.assertDone(await this._retryWithProgressIfNotConnected(progress, target, options.strict, async handle => {874 return handle._retryPointerAction(progress, 'move and up', false, async point => {875 await this._page.mouse.move(point.x, point.y);876 await this._page.mouse.up();877 }, { ...options,878 position: options.targetPosition,879 timeout: progress.timeUntilDeadline()880 });881 }));882 }, this._page._timeoutSettings.timeout(options));883 }884 async tap(metadata, selector, options) {885 const controller = new _progress.ProgressController(metadata, this);886 return controller.run(async progress => {887 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._tap(progress, options)));888 }, this._page._timeoutSettings.timeout(options));889 }890 async fill(metadata, selector, value, options) {891 const controller = new _progress.ProgressController(metadata, this);892 return controller.run(async progress => {893 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._fill(progress, value, options)));894 }, this._page._timeoutSettings.timeout(options));895 }896 async focus(metadata, selector, options = {}) {897 const controller = new _progress.ProgressController(metadata, this);898 await controller.run(async progress => {899 dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._focus(progress)));900 await this._page._doSlowMo();901 }, this._page._timeoutSettings.timeout(options));902 }903 async textContent(metadata, selector, options = {}) {904 return this._scheduleRerunnableTask(metadata, selector, (progress, element) => element.textContent, undefined, options);905 }906 async innerText(metadata, selector, options = {}) {907 return this._scheduleRerunnableTask(metadata, selector, (progress, element) => {908 if (element.namespaceURI !== 'http://www.w3.org/1999/xhtml') throw progress.injectedScript.createStacklessError('Node is not an HTMLElement');909 return element.innerText;910 }, undefined, options);911 }912 async innerHTML(metadata, selector, options = {}) {913 return this._scheduleRerunnableTask(metadata, selector, (progress, element) => element.innerHTML, undefined, options);914 }915 async getAttribute(metadata, selector, name, options = {}) {916 return this._scheduleRerunnableTask(metadata, selector, (progress, element, data) => element.getAttribute(data.name), {917 name918 }, options);919 }920 async inputValue(metadata, selector, options = {}) {921 return this._scheduleRerunnableTask(metadata, selector, (progress, node) => {922 const element = progress.injectedScript.retarget(node, 'follow-label');923 if (!element || element.nodeName !== 'INPUT' && element.nodeName !== 'TEXTAREA' && element.nodeName !== 'SELECT') throw progress.injectedScript.createStacklessError('Node is not an <input>, <textarea> or <select> element');924 return element.value;925 }, undefined, options);926 }927 async highlight(selector) {928 const pair = await this.resolveFrameForSelectorNoWait(selector);929 if (!pair) return;930 const context = await this._utilityContext();931 const injectedScript = await context.injectedScript();932 return await injectedScript.evaluate((injected, {933 parsed934 }) => {935 return injected.highlight(parsed);936 }, {937 parsed: pair.info.parsed938 });939 }940 async hideHighlight() {941 const context = await this._utilityContext();942 const injectedScript = await context.injectedScript();943 return await injectedScript.evaluate(injected => {944 return injected.hideHighlight();945 });946 }947 async _elementState(metadata, selector, state, options = {}) {948 const result = await this._scheduleRerunnableTask(metadata, selector, (progress, element, data) => {949 const injected = progress.injectedScript;950 return injected.elementState(element, data.state);951 }, {952 state953 }, options);954 return dom.throwRetargetableDOMError(result);955 }956 async isVisible(metadata, selector, options = {}) {957 const controller = new _progress.ProgressController(metadata, this);958 return controller.run(async progress => {959 progress.log(` checking visibility of "${selector}"`);960 const pair = await this.resolveFrameForSelectorNoWait(selector, options);961 if (!pair) return false;962 const element = await this._page.selectors.query(pair.frame, pair.info);963 return element ? await element.isVisible() : false;964 }, this._page._timeoutSettings.timeout({}));965 }966 async isHidden(metadata, selector, options = {}) {967 return !(await this.isVisible(metadata, selector, options));968 }969 async isDisabled(metadata, selector, options = {}) {970 return this._elementState(metadata, selector, 'disabled', options);971 }972 async isEnabled(metadata, selector, options = {}) {973 return this._elementState(metadata, selector, 'enabled', options);974 }975 async isEditable(metadata, selector, options = {}) {976 return this._elementState(metadata, selector, 'editable', options);977 }978 async isChecked(metadata, selector, options = {}) {979 return this._elementState(metadata, selector, 'checked', options);980 }981 async hover(metadata, selector, options = {}) {982 const controller = new _progress.ProgressController(metadata, this);983 return controller.run(async progress => {984 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._hover(progress, options)));985 }, this._page._timeoutSettings.timeout(options));986 }987 async selectOption(metadata, selector, elements, values, options = {}) {988 const controller = new _progress.ProgressController(metadata, this);989 return controller.run(async progress => {990 return await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._selectOption(progress, elements, values, options));991 }, this._page._timeoutSettings.timeout(options));992 }993 async setInputFiles(metadata, selector, files, options = {}) {994 const controller = new _progress.ProgressController(metadata, this);995 return controller.run(async progress => {996 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._setInputFiles(progress, files, options)));997 }, this._page._timeoutSettings.timeout(options));998 }999 async type(metadata, selector, text, options = {}) {1000 const controller = new _progress.ProgressController(metadata, this);1001 return controller.run(async progress => {1002 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._type(progress, text, options)));1003 }, this._page._timeoutSettings.timeout(options));1004 }1005 async press(metadata, selector, key, options = {}) {1006 const controller = new _progress.ProgressController(metadata, this);1007 return controller.run(async progress => {1008 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._press(progress, key, options)));1009 }, this._page._timeoutSettings.timeout(options));1010 }1011 async check(metadata, selector, options = {}) {1012 const controller = new _progress.ProgressController(metadata, this);1013 return controller.run(async progress => {1014 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._setChecked(progress, true, options)));1015 }, this._page._timeoutSettings.timeout(options));1016 }1017 async uncheck(metadata, selector, options = {}) {1018 const controller = new _progress.ProgressController(metadata, this);1019 return controller.run(async progress => {1020 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._setChecked(progress, false, options)));1021 }, this._page._timeoutSettings.timeout(options));1022 }1023 async waitForTimeout(metadata, timeout) {1024 const controller = new _progress.ProgressController(metadata, this);1025 return controller.run(async () => {1026 await new Promise(resolve => setTimeout(resolve, timeout));1027 });1028 }1029 async expect(metadata, selector, options) {1030 const controller = new _progress.ProgressController(metadata, this);1031 const isArray = options.expression === 'to.have.count' || options.expression.endsWith('.array');1032 const mainWorld = options.expression === 'to.have.property';1033 const timeout = this._page._timeoutSettings.timeout(options); // List all combinations that are satisfied with the detached node(s).1034 let omitAttached = false;1035 if (!options.isNot && options.expression === 'to.be.hidden') omitAttached = true;else if (options.isNot && options.expression === 'to.be.visible') omitAttached = true;else if (!options.isNot && options.expression === 'to.have.count' && options.expectedNumber === 0) omitAttached = true;else if (options.isNot && options.expression === 'to.have.count' && options.expectedNumber !== 0) omitAttached = true;else if (!options.isNot && options.expression.endsWith('.array') && options.expectedText.length === 0) omitAttached = true;else if (options.isNot && options.expression.endsWith('.array') && options.expectedText.length > 0) omitAttached = true;1036 return controller.run(async outerProgress => {1037 outerProgress.log(`${metadata.apiName}${timeout ? ` with timeout ${timeout}ms` : ''}`);1038 return await this._scheduleRerunnableTaskWithProgress(outerProgress, selector, (progress, element, options, elements) => {1039 let result;1040 if (options.isArray) {1041 result = progress.injectedScript.expectArray(elements, options);1042 } else {1043 if (!element) {1044 // expect(locator).toBeHidden() passes when there is no element.1045 if (!options.isNot && options.expression === 'to.be.hidden') return {1046 matches: true1047 }; // expect(locator).not.toBeVisible() passes when there is no element.1048 if (options.isNot && options.expression === 'to.be.visible') return {1049 matches: false1050 }; // When none of the above applies, keep waiting for the element.1051 return progress.continuePolling;1052 }1053 result = progress.injectedScript.expectSingleElement(progress, element, options);1054 }1055 if (result.matches === options.isNot) {1056 // Keep waiting in these cases:1057 // expect(locator).conditionThatDoesNotMatch1058 // expect(locator).not.conditionThatDoesMatch1059 progress.setIntermediateResult(result.received);1060 if (!Array.isArray(result.received)) progress.log(` unexpected value "${result.received}"`);1061 return progress.continuePolling;1062 } // Reached the expected state!1063 return result;1064 }, { ...options,1065 isArray1066 }, {1067 strict: true,1068 querySelectorAll: isArray,1069 mainWorld,1070 omitAttached,1071 logScale: true,1072 ...options1073 });1074 }, timeout).catch(e => {1075 // Q: Why not throw upon isSessionClosedError(e) as in other places?1076 // A: We want user to receive a friendly message containing the last intermediate result.1077 if (js.isJavaScriptErrorInEvaluate(e) || (0, _selectorErrors.isInvalidSelectorError)(e)) throw e;1078 return {1079 received: controller.lastIntermediateResult(),1080 matches: options.isNot,1081 log: metadata.log1082 };1083 });1084 }1085 async _waitForFunctionExpression(metadata, expression, isFunction, arg, options, world = 'main') {1086 const controller = new _progress.ProgressController(metadata, this);1087 if (typeof options.pollingInterval === 'number') (0, _utils.assert)(options.pollingInterval > 0, 'Cannot poll with non-positive interval: ' + options.pollingInterval);1088 expression = js.normalizeEvaluationExpression(expression, isFunction);1089 const task = injectedScript => injectedScript.evaluateHandle((injectedScript, {1090 expression,1091 isFunction,1092 polling,1093 arg1094 }) => {1095 const predicate = arg => {1096 let result = self.eval(expression);1097 if (isFunction === true) {1098 result = result(arg);1099 } else if (isFunction === false) {1100 result = result;1101 } else {1102 // auto detect.1103 if (typeof result === 'function') result = result(arg);1104 }1105 return result;1106 };1107 if (typeof polling !== 'number') return injectedScript.pollRaf(progress => predicate(arg) || progress.continuePolling);1108 return injectedScript.pollInterval(polling, progress => predicate(arg) || progress.continuePolling);1109 }, {1110 expression,1111 isFunction,1112 polling: options.pollingInterval,1113 arg1114 });1115 return controller.run(progress => this._scheduleRerunnableHandleTask(progress, world, task), this._page._timeoutSettings.timeout(options));1116 }1117 async waitForFunctionValueInUtility(progress, pageFunction) {1118 const expression = `() => {1119 const result = (${pageFunction})();1120 if (!result)1121 return result;1122 return JSON.stringify(result);1123 }`;1124 const handle = await this._waitForFunctionExpression((0, _instrumentation.internalCallMetadata)(), expression, true, undefined, {1125 timeout: progress.timeUntilDeadline()1126 }, 'utility');1127 return JSON.parse(handle.rawValue());1128 }1129 async title() {1130 const context = await this._utilityContext();1131 return context.evaluate(() => document.title);1132 }1133 _onDetached() {1134 this._stopNetworkIdleTimer();1135 this._detached = true;1136 this._detachedCallback();1137 const error = new Error('Frame was detached');1138 for (const data of this._contextData.values()) {1139 if (data.context) data.context.contextDestroyed(error);1140 data.contextPromise.resolve(error);1141 for (const rerunnableTask of data.rerunnableTasks) rerunnableTask.terminate(error);1142 }1143 if (this._parentFrame) this._parentFrame._childFrames.delete(this);1144 this._parentFrame = null;1145 }1146 async _scheduleRerunnableTask(metadata, selector, body, taskData, options = {}) {1147 const controller = new _progress.ProgressController(metadata, this);1148 return controller.run(async progress => {1149 return await this._scheduleRerunnableTaskWithProgress(progress, selector, body, taskData, options);1150 }, this._page._timeoutSettings.timeout(options));1151 }1152 async _scheduleRerunnableTaskWithProgress(progress, selector, body, taskData, options = {}) {1153 const callbackText = body.toString();1154 return this.retryWithProgress(progress, selector, options, async selectorInFrame => {1155 // Be careful, |this| can be different from |frame|.1156 progress.log(`waiting for selector "${selector}"`);1157 const {1158 frame,1159 info1160 } = selectorInFrame || {1161 frame: this,1162 info: {1163 parsed: {1164 parts: [{1165 name: 'control',1166 body: 'return-empty',1167 source: 'control=return-empty'1168 }]1169 },1170 world: 'utility',1171 strict: !!options.strict1172 }1173 };1174 return await frame._scheduleRerunnableTaskInFrame(progress, info, callbackText, taskData, options);1175 });1176 }1177 async _scheduleRerunnableTaskInFrame(progress, info, callbackText, taskData, options) {1178 progress.throwIfAborted();1179 const data = this._contextData.get(options.mainWorld ? 'main' : info.world); // This potentially runs in a sub-frame.1180 {1181 const rerunnableTask = new RerunnableTask(data, progress, injectedScript => {1182 return injectedScript.evaluateHandle((injected, {1183 info,1184 taskData,1185 callbackText,1186 querySelectorAll,1187 logScale,1188 omitAttached,1189 snapshotName1190 }) => {1191 const callback = injected.eval(callbackText);1192 const poller = logScale ? injected.pollLogScale.bind(injected) : injected.pollRaf.bind(injected);1193 let markedElements = new Set();1194 return poller(progress => {1195 let element;1196 let elements = [];1197 if (querySelectorAll) {1198 elements = injected.querySelectorAll(info.parsed, document);1199 element = elements[0];1200 progress.logRepeating(` selector resolved to ${elements.length} element${elements.length === 1 ? '' : 's'}`);1201 } else {1202 element = injected.querySelector(info.parsed, document, info.strict);1203 elements = element ? [element] : [];1204 if (element) progress.logRepeating(` selector resolved to ${injected.previewNode(element)}`);1205 }1206 if (!element && !omitAttached) return progress.continuePolling;1207 if (snapshotName) {1208 const previouslyMarkedElements = markedElements;1209 markedElements = new Set(elements);1210 for (const e of previouslyMarkedElements) {1211 if (!markedElements.has(e)) e.removeAttribute('__playwright_target__');1212 }1213 for (const e of markedElements) {1214 if (!previouslyMarkedElements.has(e)) e.setAttribute('__playwright_target__', snapshotName);1215 }1216 }1217 return callback(progress, element, taskData, elements);1218 });1219 }, {1220 info,1221 taskData,1222 callbackText,1223 querySelectorAll: options.querySelectorAll,1224 logScale: options.logScale,1225 omitAttached: options.omitAttached,1226 snapshotName: progress.metadata.afterSnapshot1227 });1228 }, true);1229 if (this._detached) rerunnableTask.terminate(new Error('Frame got detached.'));1230 if (data.context) rerunnableTask.rerun(data.context);1231 return await rerunnableTask.promise;1232 }1233 }1234 _scheduleRerunnableHandleTask(progress, world, task) {1235 const data = this._contextData.get(world);1236 const rerunnableTask = new RerunnableTask(data, progress, task, false1237 /* returnByValue */1238 );1239 if (this._detached) rerunnableTask.terminate(new Error('waitForFunction failed: frame got detached.'));1240 if (data.context) rerunnableTask.rerun(data.context);1241 return rerunnableTask.handlePromise;1242 }1243 _setContext(world, context) {1244 const data = this._contextData.get(world);1245 data.context = context;1246 if (context) {1247 data.contextPromise.resolve(context);1248 for (const rerunnableTask of data.rerunnableTasks) rerunnableTask.rerun(context);1249 } else {1250 data.contextPromise = new _async.ManualPromise();1251 }1252 }1253 _contextCreated(world, context) {1254 const data = this._contextData.get(world); // In case of multiple sessions to the same target, there's a race between1255 // connections so we might end up creating multiple isolated worlds.1256 // We can use either.1257 if (data.context) {1258 data.context.contextDestroyed(new Error('Execution context was destroyed, most likely because of a navigation'));1259 this._setContext(world, null);1260 }1261 this._setContext(world, context);1262 }1263 _contextDestroyed(context) {1264 // Sometimes we get this after detach, in which case we should not reset1265 // our already destroyed contexts to something that will never resolve.1266 if (this._detached) return;1267 context.contextDestroyed(new Error('Execution context was destroyed, most likely because of a navigation'));1268 for (const [world, data] of this._contextData) {1269 if (data.context === context) this._setContext(world, null);1270 }1271 }1272 _startNetworkIdleTimer() {1273 (0, _utils.assert)(!this._networkIdleTimer); // We should not start a timer and report networkidle in detached frames.1274 // This happens at least in Firefox for child frames, where we may get requestFinished1275 // after the frame was detached - probably a race in the Firefox itself.1276 if (this._firedLifecycleEvents.has('networkidle') || this._detached) return;1277 this._networkIdleTimer = setTimeout(() => this._onLifecycleEvent('networkidle'), 500);1278 }1279 _stopNetworkIdleTimer() {1280 if (this._networkIdleTimer) clearTimeout(this._networkIdleTimer);1281 this._networkIdleTimer = undefined;1282 }1283 async extendInjectedScript(source, arg) {1284 const context = await this._context('main');1285 const injectedScriptHandle = await context.injectedScript();1286 return injectedScriptHandle.evaluateHandle((injectedScript, {1287 source,1288 arg1289 }) => {1290 return injectedScript.extend(source, arg);1291 }, {1292 source,1293 arg1294 });1295 }1296 async _resolveFrameForSelector(progress, selector, options, scope) {1297 const elementPath = [];1298 progress.cleanupWhenAborted(() => {1299 // Do not await here to avoid being blocked, either by stalled1300 // page (e.g. alert) or unresolved navigation in Chromium.1301 for (const element of elementPath) element.dispose();1302 });1303 let frame = this;1304 const frameChunks = (0, _selectorParser.splitSelectorByFrame)(selector);1305 for (let i = 0; i < frameChunks.length - 1 && progress.isRunning(); ++i) {1306 const info = this._page.parseSelector(frameChunks[i], options);1307 const task = dom.waitForSelectorTask(info, 'attached', false, i === 0 ? scope : undefined);1308 progress.log(` waiting for frame "${(0, _selectorParser.stringifySelector)(frameChunks[i])}"`);1309 const handle = i === 0 && scope ? await frame._runWaitForSelectorTaskOnce(progress, (0, _selectorParser.stringifySelector)(info.parsed), info.world, task) : await frame._scheduleRerunnableHandleTask(progress, info.world, task);1310 const element = handle.asElement();1311 const isIframe = await element.isIframeElement();1312 if (isIframe === 'error:notconnected') return null; // retry1313 if (!isIframe) throw new Error(`Selector "${(0, _selectorParser.stringifySelector)(info.parsed)}" resolved to ${element.preview()}, <iframe> was expected`);1314 frame = await element.contentFrame();1315 element.dispose();1316 if (!frame) return null; // retry1317 }1318 return {1319 frame,1320 info: this._page.parseSelector(frameChunks[frameChunks.length - 1], options)1321 };1322 }1323 async resolveFrameForSelectorNoWait(selector, options = {}, scope) {1324 let frame = this;1325 const frameChunks = (0, _selectorParser.splitSelectorByFrame)(selector);1326 for (let i = 0; i < frameChunks.length - 1; ++i) {1327 const info = this._page.parseSelector(frameChunks[i], options);1328 const element = await this._page.selectors.query(frame, info, i === 0 ? scope : undefined);1329 if (!element) return null;1330 frame = await element.contentFrame();1331 element.dispose();1332 if (!frame) throw new Error(`Selector "${(0, _selectorParser.stringifySelector)(info.parsed)}" resolved to ${element.preview()}, <iframe> was expected`);1333 }1334 return {1335 frame,1336 info: this._page.parseSelector(frameChunks[frameChunks.length - 1], options)1337 };...
dom.js
Source:dom.js
...689 const controller = new _progress.ProgressController(metadata, this);690 return controller.run(progress => this._page._screenshotter.screenshotElement(progress, this, options), this._page._timeoutSettings.timeout(options));691 }692 async querySelector(selector, options) {693 const pair = await this._frame.resolveFrameForSelectorNoWait(selector, options, this);694 if (!pair) return null;695 const {696 frame,697 info698 } = pair; // If we end up in the same frame => use the scope again, line above was noop.699 return this._page.selectors.query(frame, info, this._frame === frame ? this : undefined);700 }701 async querySelectorAll(selector) {702 const pair = await this._frame.resolveFrameForSelectorNoWait(selector, {}, this);703 if (!pair) return [];704 const {705 frame,706 info707 } = pair; // If we end up in the same frame => use the scope again, line above was noop.708 return this._page.selectors._queryAll(frame, info, this._frame === frame ? this : undefined, true709 /* adoptToMain */710 );711 }712 async evalOnSelectorAndWaitForSignals(selector, strict, expression, isFunction, arg) {713 const pair = await this._frame.resolveFrameForSelectorNoWait(selector, {714 strict715 }, this); // If we end up in the same frame => use the scope again, line above was noop.716 const handle = pair ? await this._page.selectors.query(pair.frame, pair.info, this._frame === pair.frame ? this : undefined) : null;717 if (!handle) throw new Error(`Error: failed to find element matching selector "${selector}"`);718 const result = await handle.evaluateExpressionAndWaitForSignals(expression, isFunction, true, arg);719 handle.dispose();720 return result;721 }722 async evalOnSelectorAllAndWaitForSignals(selector, expression, isFunction, arg) {723 const pair = await this._frame.resolveFrameForSelectorNoWait(selector, {}, this);724 if (!pair) throw new Error(`Error: failed to find frame for selector "${selector}"`);725 const {726 frame,727 info728 } = pair; // If we end up in the same frame => use the scope again, line above was noop.729 const arrayHandle = await this._page.selectors._queryArrayInMainWorld(frame, info, this._frame === frame ? this : undefined);730 const result = await arrayHandle.evaluateExpressionAndWaitForSignals(expression, isFunction, true, arg);731 arrayHandle.dispose();732 return result;733 }734 async isVisible() {735 const result = await this.evaluateInUtility(([injected, node]) => injected.elementState(node, 'visible'), {});736 if (result === 'error:notconnected') return false;737 return result;...
screenshotter.js
Source:screenshotter.js
...198 await Promise.all((options.mask || []).map(async ({199 frame,200 selector201 }) => {202 const pair = await frame.resolveFrameForSelectorNoWait(selector);203 if (pair) framesToParsedSelectors.set(pair.frame, pair.info.parsed);204 }));205 progress.throwIfAborted(); // Avoid extra work.206 await Promise.all([...framesToParsedSelectors.keys()].map(async frame => {207 await frame.maskSelectors(framesToParsedSelectors.get(frame));208 }));209 progress.cleanupWhenAborted(() => this._page.hideHighlight());210 return true;211 }212 async _screenshot(progress, format, documentRect, viewportRect, fitsViewport, options) {213 if (options.__testHookBeforeScreenshot) await options.__testHookBeforeScreenshot();214 progress.throwIfAborted(); // Screenshotting is expensive - avoid extra work.215 const shouldSetDefaultBackground = options.omitBackground && format === 'png';216 if (shouldSetDefaultBackground) {...
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch({ headless: false });4 const context = await browser.newContext();5 const page = await context.newPage();6 const frame = await page.resolveFrameForSelectorNoWait('iframe');7 await frame.click('text=Login');8 await page.waitForSelector('text=Sign in with Google');9 await page.click('text=Sign in with Google');10 await page.waitForSelector('text=Create account');11 await page.click('text=Create account');12 await page.waitForSelector('input[name="firstName"]');13 await page.fill('input[name="firstName"]', 'John');14 await page.fill('input[name="lastName"]', 'Doe');15 await page.fill('input[name="Username"]', 'johndoe');16 await page.fill('input[name="Passwd"]', 'test');17 await page.fill('input[name="ConfirmPasswd"]', 'test');18 await page.click('text=Next');19 await page.waitForSelector('input[name="phoneNumberId"]');20 await page.fill('input[name="phoneNumberId"]', '1234567890');21 await page.click('text=Next');22 await page.waitForTimeout(5000);23 await browser.close();24})();
Using AI Code Generation
1const { resolveFrameForSelectorNoWait } = require('playwright/lib/helper');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 const frame = await resolveFrameForSelectorNoWait(page, 'iframe');8 console.log(frame);9 await browser.close();10})();
Using AI Code Generation
1const { Playwright } = require('playwright');2const path = require('path');3(async () => {4 const browser = await Playwright.chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 const frame = await page.mainFrame().childFrames()[0];8 const selector = await frame.$('#docs-content');9 const frameForSelector = await frame.resolveFrameForSelectorNoWait(selector);10 const element = await frameForSelector.$('div[class="docs-nav"]');11 const text = await element.textContent();12 console.log(text);13 await browser.close();14})();
Using AI Code Generation
1const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/dom');2const { Page } = require('playwright/lib/server/page');3const { ElementHandle } = require('playwright/lib/server/dom');4const { Frame } = require('playwright/lib/server/frame');5const { JSHandle } = require('playwright/lib/server/jsHandle');6const { frame } = await page.$('iframe')7const selector = 'div';8const elementHandle = await resolveFrameForSelectorNoWait(page.mainFrame(), selector);9const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/dom');10const { Page } = require('playwright/lib/server/page');11const { ElementHandle } = require('playwright/lib/server/dom');12const { Frame } = require('playwright/lib/server/frame');13const { JSHandle } = require('playwright/lib/server/jsHandle');14const { frame } = await page.$('iframe')15const selector = 'div';16const elementHandle = await resolveFrameForSelectorNoWait(page.mainFrame(), selector);17const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/dom');18const { Page } = require('playwright/lib/server/page');19const { ElementHandle } = require('playwright/lib/server/dom');20const { Frame } = require('playwright/lib/server/frame');21const { JSHandle } = require('playwright/lib/server/jsHandle');22const { frame } = await page.$('iframe')23const selector = 'div';24const elementHandle = await resolveFrameForSelectorNoWait(page.mainFrame(), selector);25const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/dom');26const { Page } = require('playwright/lib/server/page');27const { ElementHandle } = require('playwright/lib/server/dom');28const { Frame } = require('playwright/lib/server/frame');29const { JSHandle } = require('playwright/lib/server/jsHandle');30const { frame }
Using AI Code Generation
1const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/chromium/crPage');2const { Frame } = require('playwright/lib/server/chromium/crFrameManager');3const { ElementHandle } = require('playwright/lib/server/dom');4const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/chromium/crPage');5const { Frame } = require('playwright/lib/server/chromium/crFrameManager');6const { ElementHandle } = require('playwright/lib/server/dom');7const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/chromium/crPage');8const { Frame } = require('playwright/lib/server/chromium/crFrameManager');9const { ElementHandle } = require('playwright/lib/server/dom');10const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/chromium/crPage');11const { Frame } = require('playwright/lib/server/chromium/crFrameManager');12const { ElementHandle } = require('playwright/lib/server/dom');13const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/chromium/crPage');14const { Frame } = require('playwright/lib/server/chromium/crFrameManager');15const { ElementHandle } = require('playwright/lib/server/dom');16const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/chromium/crPage');17const { Frame } = require('playwright/lib/server/chromium/crFrameManager');18const { ElementHandle } = require('playwright/lib/server/dom');19const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/chromium/crPage');20const { Frame } = require('playwright/lib/server/chromium/crFrameManager');21const { ElementHandle } = require('playwright/lib/server/dom');
Using AI Code Generation
1const {chromium} = require('playwright');2const {resolveFrameForSelectorNoWait} = require('playwright/lib/internal/frames');3const expect = require('expect');4(async () => {5 const browser = await chromium.launch();6 const context = await browser.newContext();7 const page = await context.newPage();8 expect(frame).not.toBeNull();9 await browser.close();10})();11const {chromium} = require('playwright');12const expect = require('expect');13(async () => {14 const browser = await chromium.launch();15 const context = await browser.newContext();16 const page = await context.newPage();17 const [popup] = await Promise.all([18 page.waitForEvent('popup'),19 page.click('text=Sign in'),20 ]);21 expect(popup).not.toBeNull();22 await browser.close();23})();24const {chromium} = require('playwright');25const expect = require('expect');26(async () => {27 const browser = await chromium.launch();28 const context = await browser.newContext();29 const page = await context.newPage();30 await page.waitForTimeout(5000);31 const [popup] = await Promise.all([32 page.waitForEvent('popup'),
Using AI Code Generation
1const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/dom');2const { selectors } = require('playwright/lib/server/selectors');3const { Page } = require('playwright/lib/server/page');4const { Frame } = require('playwright/lib/server/frames');5const { ElementHandle } = require('playwright/lib/server/dom');6const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/dom');7const { selectors } = require('playwright/lib/server/selectors');8const { Page } = require('playwright/lib/server/page');9const { Frame } = require('playwright/lib/server/frames');10const { ElementHandle } = require('playwright/lib/server/dom');11const { selectors } = require('playwright/lib/server/selectors');12const { Page } = require('playwright/lib/server/page');13const { Frame } = require('playwright/lib/server/frames');14const { ElementHandle } = require('playwright/lib/server/dom');15const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/dom');16const { selectors } = require('playwright/lib/server/selectors');17const { Page } = require('playwright/lib/server/page');18const { Frame } = require('playwright/lib/server/frames');19const { ElementHandle } = require('playwright/lib/server/dom');20const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/dom');21const { selectors } = require('playwright/lib/server/selectors');22const { Page } = require('playwright/lib/server/page');23const { Frame } = require('playwright/lib/server/frames');24const { ElementHandle } = require('playwright/lib/server/dom');25const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/dom');26const { selectors } = require('playwright/lib/server/selectors');27const { Page } = require('playwright/lib/server/page');28const { Frame } = require('playwright/lib/server/frames');29const { ElementHandle } = require('playwright/lib/server/dom');30const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/dom');31const { selectors } = require('playwright/lib/server/selectors');32const { Page } = require('playwright/lib/server/page');33const { Frame } = require('playwright/lib/server/frames');
Using AI Code Generation
1const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/dom');2const { selectors } = require('playwright/lib/server/selectors');3const { ElementHandle } = require('playwright/lib/server/dom');4const { Frame } = require('playwright/lib/server/dom');5const { Page } = require('playwright/lib/server/dom');6const { JSHandle } = require('playwright/lib/server/dom');7const { ElementHandleChannel } = require('playwright/lib/server/channels');8const { FrameChannel } = require('playwright/lib/server/channels');9const { PageChannel } = require('playwright/lib/server/channels');10const { JSHandleChannel } = require('playwright/lib/server/channels');11async function main() {12 const page = await browser.newPage();13 const frame = await page.mainFrame();14 const handle = await frame.evaluateHandle(() => document.body);15 const elementHandle = handle.asElement();16 const selector = 'css=body';17 const info = await resolveFrameForSelectorNoWait(18 );19 console.log(info);20}21main();
Using AI Code Generation
1const { resolveFrameForSelectorNoWait } = require('playwright/lib/helper');2const { selectors } = require('playwright/lib/server/selectors');3const { ElementHandle } = require('playwright/lib/server/dom');4(async () => {5 const page = await browser.newPage();6 const element = await resolveFrameForSelectorNoWait(page.mainFrame(), selectors._engines['css'], 'input[title="Search"]');7 await element.type('Hello World');8})();9(async () => {10 const page = await browser.newPage();11 const element = await resolveFrameForSelectorNoWait(page.mainFrame(), selectors._engines['css'], 'input[title="Search"]');12 await element.type('Hello World');13})();14(async () => {15 const page = await browser.newPage();16 const element = await resolveFrameForSelectorNoWait(page.mainFrame(), selectors._engines['css'], 'input[title="Search"]');17 await element.type('Hello World');18})();19(async () => {20 const page = await browser.newPage();21 const element = await resolveFrameForSelectorNoWait(page.mainFrame(), selectors._engines['css'], 'input[title="Search"]');22 await element.type('Hello World');23})();24(async () => {25 const page = await browser.newPage();26 const element = await resolveFrameForSelectorNoWait(page.mainFrame(), selectors._engines['css'], 'input[title="Search"]');27 await element.type('Hello World');28})();29(async () => {30 const page = await browser.newPage();31 const element = await resolveFrameForSelectorNoWait(page.mainFrame(), selectors
Using AI Code Generation
1const {chromium} = require('playwright');2const { resolveFrameForSelectorNoWait } = require('playwright/lib/server/supplements/recorder/recorderSupplement');3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 const frame = await resolveFrameForSelectorNoWait(page, 'a.text-link');8 await frame.click('a.text-link');9 await browser.close();10})();11{ _page: Page { _closed: false, _closedCallback: [Function], _initialLoaderId: '0A1E1F2B2E2F2E2F2F2E2F2E2F2E2F2E', _pageBindings: {}, _pageBindingsReverse: {}, _timeoutSettings: TimeoutSettings { _defaultNavigationTimeout: 30000, _defaultTimeout: 30000 }, _delegate: [PageDelegate], _channel: Channel, _eventEmitter: EventEmitter, _browserContext: BrowserContext { _closed: false, _closedCallback: [Function], _options: [Object], _timeoutSettings: [TimeoutSettings], _delegate: [BrowserContextDelegate], _channel: [Channel], _eventEmitter: [EventEmitter], _browser: [Browser], _permissions: [Map], _isChromium: true, _isWebKit: false, _isFirefox: false }, _browser: Browser { _closed: false, _closedCallback: [Function], _options: [Object], _timeoutSettings: [TimeoutSettings], _delegate: [BrowserDelegate], _channel: [Channel], _eventEmitter: [EventEmitter], _isChromium: true, _isWebKit: false, _isFirefox: false, _contexts: [Set], _defaultContext: [BrowserContext] }, _workers: Set {}, _routes: [], _workersById: Map {}, _frameManager: FrameManager { _page: [Circular], _mainFrame: [Frame], _frames: [Map], _detachedFrames: [], _eventListeners: [], _contextIdToContext: [Map], _contextIdToAuxData: [Map], _isolatedWorlds: [Map], _isolatedWorldNameToId: [Map], _isol
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!