Best JavaScript code snippet using playwright-internal
frames.js
Source:frames.js
...857 async rafrafTimeoutScreenshotElementWithProgress(progress, selector, timeout, options) {858 return await this._retryWithProgressIfNotConnected(progress, selector, true859 /* strict */860 , async handle => {861 await handle._frame.rafrafTimeout(timeout);862 return await this._page._screenshotter.screenshotElement(progress, handle, options);863 });864 }865 async click(metadata, selector, options) {866 const controller = new _progress.ProgressController(metadata, this);867 return controller.run(async progress => {868 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._click(progress, options)));869 }, this._page._timeoutSettings.timeout(options));870 }871 async dblclick(metadata, selector, options = {}) {872 const controller = new _progress.ProgressController(metadata, this);873 return controller.run(async progress => {874 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._dblclick(progress, options)));875 }, this._page._timeoutSettings.timeout(options));876 }877 async dragAndDrop(metadata, source, target, options = {}) {878 const controller = new _progress.ProgressController(metadata, this);879 await controller.run(async progress => {880 dom.assertDone(await this._retryWithProgressIfNotConnected(progress, source, options.strict, async handle => {881 return handle._retryPointerAction(progress, 'move and down', false, async point => {882 await this._page.mouse.move(point.x, point.y);883 await this._page.mouse.down();884 }, { ...options,885 position: options.sourcePosition,886 timeout: progress.timeUntilDeadline()887 });888 }));889 dom.assertDone(await this._retryWithProgressIfNotConnected(progress, target, options.strict, async handle => {890 return handle._retryPointerAction(progress, 'move and up', false, async point => {891 await this._page.mouse.move(point.x, point.y);892 await this._page.mouse.up();893 }, { ...options,894 position: options.targetPosition,895 timeout: progress.timeUntilDeadline()896 });897 }));898 }, this._page._timeoutSettings.timeout(options));899 }900 async tap(metadata, selector, options) {901 const controller = new _progress.ProgressController(metadata, this);902 return controller.run(async progress => {903 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._tap(progress, options)));904 }, this._page._timeoutSettings.timeout(options));905 }906 async fill(metadata, selector, value, options) {907 const controller = new _progress.ProgressController(metadata, this);908 return controller.run(async progress => {909 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._fill(progress, value, options)));910 }, this._page._timeoutSettings.timeout(options));911 }912 async focus(metadata, selector, options = {}) {913 const controller = new _progress.ProgressController(metadata, this);914 await controller.run(async progress => {915 dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._focus(progress)));916 await this._page._doSlowMo();917 }, this._page._timeoutSettings.timeout(options));918 }919 async textContent(metadata, selector, options = {}) {920 return this._scheduleRerunnableTask(metadata, selector, (progress, element) => element.textContent, undefined, options);921 }922 async innerText(metadata, selector, options = {}) {923 return this._scheduleRerunnableTask(metadata, selector, (progress, element) => {924 if (element.namespaceURI !== 'http://www.w3.org/1999/xhtml') throw progress.injectedScript.createStacklessError('Node is not an HTMLElement');925 return element.innerText;926 }, undefined, options);927 }928 async innerHTML(metadata, selector, options = {}) {929 return this._scheduleRerunnableTask(metadata, selector, (progress, element) => element.innerHTML, undefined, options);930 }931 async getAttribute(metadata, selector, name, options = {}) {932 return this._scheduleRerunnableTask(metadata, selector, (progress, element, data) => element.getAttribute(data.name), {933 name934 }, options);935 }936 async inputValue(metadata, selector, options = {}) {937 return this._scheduleRerunnableTask(metadata, selector, (progress, node) => {938 const element = progress.injectedScript.retarget(node, 'follow-label');939 if (!element || element.nodeName !== 'INPUT' && element.nodeName !== 'TEXTAREA' && element.nodeName !== 'SELECT') throw progress.injectedScript.createStacklessError('Node is not an <input>, <textarea> or <select> element');940 return element.value;941 }, undefined, options);942 }943 async highlight(selector) {944 const pair = await this.resolveFrameForSelectorNoWait(selector);945 if (!pair) return;946 const context = await pair.frame._utilityContext();947 const injectedScript = await context.injectedScript();948 return await injectedScript.evaluate((injected, {949 parsed950 }) => {951 return injected.highlight(parsed);952 }, {953 parsed: pair.info.parsed954 });955 }956 async hideHighlight() {957 return this.raceAgainstEvaluationStallingEvents(async () => {958 const context = await this._utilityContext();959 const injectedScript = await context.injectedScript();960 return await injectedScript.evaluate(injected => {961 return injected.hideHighlight();962 });963 });964 }965 async _elementState(metadata, selector, state, options = {}) {966 const result = await this._scheduleRerunnableTask(metadata, selector, (progress, element, data) => {967 const injected = progress.injectedScript;968 return injected.elementState(element, data.state);969 }, {970 state971 }, options);972 return dom.throwRetargetableDOMError(result);973 }974 async isVisible(metadata, selector, options = {}) {975 const controller = new _progress.ProgressController(metadata, this);976 return controller.run(async progress => {977 progress.log(` checking visibility of "${selector}"`);978 const pair = await this.resolveFrameForSelectorNoWait(selector, options);979 if (!pair) return false;980 const element = await this._page.selectors.query(pair.frame, pair.info);981 return element ? await element.isVisible() : false;982 }, this._page._timeoutSettings.timeout({}));983 }984 async isHidden(metadata, selector, options = {}) {985 return !(await this.isVisible(metadata, selector, options));986 }987 async isDisabled(metadata, selector, options = {}) {988 return this._elementState(metadata, selector, 'disabled', options);989 }990 async isEnabled(metadata, selector, options = {}) {991 return this._elementState(metadata, selector, 'enabled', options);992 }993 async isEditable(metadata, selector, options = {}) {994 return this._elementState(metadata, selector, 'editable', options);995 }996 async isChecked(metadata, selector, options = {}) {997 return this._elementState(metadata, selector, 'checked', options);998 }999 async hover(metadata, selector, 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._hover(progress, options)));1003 }, this._page._timeoutSettings.timeout(options));1004 }1005 async selectOption(metadata, selector, elements, values, options = {}) {1006 const controller = new _progress.ProgressController(metadata, this);1007 return controller.run(async progress => {1008 return await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._selectOption(progress, elements, values, options));1009 }, this._page._timeoutSettings.timeout(options));1010 }1011 async setInputFiles(metadata, selector, files, 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._setInputFiles(progress, files, options)));1015 }, this._page._timeoutSettings.timeout(options));1016 }1017 async type(metadata, selector, text, 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._type(progress, text, options)));1021 }, this._page._timeoutSettings.timeout(options));1022 }1023 async press(metadata, selector, key, options = {}) {1024 const controller = new _progress.ProgressController(metadata, this);1025 return controller.run(async progress => {1026 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._press(progress, key, options)));1027 }, this._page._timeoutSettings.timeout(options));1028 }1029 async check(metadata, selector, options = {}) {1030 const controller = new _progress.ProgressController(metadata, this);1031 return controller.run(async progress => {1032 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._setChecked(progress, true, options)));1033 }, this._page._timeoutSettings.timeout(options));1034 }1035 async uncheck(metadata, selector, options = {}) {1036 const controller = new _progress.ProgressController(metadata, this);1037 return controller.run(async progress => {1038 return dom.assertDone(await this._retryWithProgressIfNotConnected(progress, selector, options.strict, handle => handle._setChecked(progress, false, options)));1039 }, this._page._timeoutSettings.timeout(options));1040 }1041 async waitForTimeout(metadata, timeout) {1042 const controller = new _progress.ProgressController(metadata, this);1043 return controller.run(async () => {1044 await new Promise(resolve => setTimeout(resolve, timeout));1045 });1046 }1047 async expect(metadata, selector, options) {1048 const controller = new _progress.ProgressController(metadata, this);1049 const isArray = options.expression === 'to.have.count' || options.expression.endsWith('.array');1050 const mainWorld = options.expression === 'to.have.property';1051 const timeout = this._page._timeoutSettings.timeout(options); // List all combinations that are satisfied with the detached node(s).1052 let omitAttached = false;1053 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;1054 return controller.run(async outerProgress => {1055 outerProgress.log(`${metadata.apiName}${timeout ? ` with timeout ${timeout}ms` : ''}`);1056 return await this._scheduleRerunnableTaskWithProgress(outerProgress, selector, (progress, element, options, elements) => {1057 let result;1058 if (options.isArray) {1059 result = progress.injectedScript.expectArray(elements, options);1060 } else {1061 if (!element) {1062 // expect(locator).toBeHidden() passes when there is no element.1063 if (!options.isNot && options.expression === 'to.be.hidden') return {1064 matches: true1065 }; // expect(locator).not.toBeVisible() passes when there is no element.1066 if (options.isNot && options.expression === 'to.be.visible') return {1067 matches: false1068 }; // When none of the above applies, keep waiting for the element.1069 return progress.continuePolling;1070 }1071 result = progress.injectedScript.expectSingleElement(progress, element, options);1072 }1073 if (result.matches === options.isNot) {1074 // Keep waiting in these cases:1075 // expect(locator).conditionThatDoesNotMatch1076 // expect(locator).not.conditionThatDoesMatch1077 progress.setIntermediateResult(result.received);1078 if (!Array.isArray(result.received)) progress.log(` unexpected value "${result.received}"`);1079 return progress.continuePolling;1080 } // Reached the expected state!1081 return result;1082 }, { ...options,1083 isArray1084 }, {1085 strict: true,1086 querySelectorAll: isArray,1087 mainWorld,1088 omitAttached,1089 logScale: true,1090 ...options1091 });1092 }, timeout).catch(e => {1093 // Q: Why not throw upon isSessionClosedError(e) as in other places?1094 // A: We want user to receive a friendly message containing the last intermediate result.1095 if (js.isJavaScriptErrorInEvaluate(e) || (0, _selectorParser.isInvalidSelectorError)(e)) throw e;1096 return {1097 received: controller.lastIntermediateResult(),1098 matches: options.isNot,1099 log: metadata.log1100 };1101 });1102 }1103 async _waitForFunctionExpression(metadata, expression, isFunction, arg, options, world = 'main') {1104 const controller = new _progress.ProgressController(metadata, this);1105 if (typeof options.pollingInterval === 'number') (0, _utils.assert)(options.pollingInterval > 0, 'Cannot poll with non-positive interval: ' + options.pollingInterval);1106 expression = js.normalizeEvaluationExpression(expression, isFunction);1107 const task = injectedScript => injectedScript.evaluateHandle((injectedScript, {1108 expression,1109 isFunction,1110 polling,1111 arg1112 }) => {1113 const predicate = arg => {1114 let result = self.eval(expression);1115 if (isFunction === true) {1116 result = result(arg);1117 } else if (isFunction === false) {1118 result = result;1119 } else {1120 // auto detect.1121 if (typeof result === 'function') result = result(arg);1122 }1123 return result;1124 };1125 if (typeof polling !== 'number') return injectedScript.pollRaf(progress => predicate(arg) || progress.continuePolling);1126 return injectedScript.pollInterval(polling, progress => predicate(arg) || progress.continuePolling);1127 }, {1128 expression,1129 isFunction,1130 polling: options.pollingInterval,1131 arg1132 });1133 return controller.run(progress => this._scheduleRerunnableHandleTask(progress, world, task), this._page._timeoutSettings.timeout(options));1134 }1135 async waitForFunctionValueInUtility(progress, pageFunction) {1136 const expression = `() => {1137 const result = (${pageFunction})();1138 if (!result)1139 return result;1140 return JSON.stringify(result);1141 }`;1142 const handle = await this._waitForFunctionExpression((0, _instrumentation.internalCallMetadata)(), expression, true, undefined, {1143 timeout: progress.timeUntilDeadline()1144 }, 'utility');1145 return JSON.parse(handle.rawValue());1146 }1147 async title() {1148 const context = await this._utilityContext();1149 return context.evaluate(() => document.title);1150 }1151 async rafrafTimeout(timeout) {1152 if (timeout === 0) return;1153 const context = await this._utilityContext();1154 await Promise.all([// wait for double raf1155 context.evaluate(() => new Promise(x => {1156 requestAnimationFrame(() => {1157 requestAnimationFrame(x);1158 });1159 })), new Promise(fulfill => setTimeout(fulfill, timeout))]);1160 }1161 _onDetached() {1162 this._stopNetworkIdleTimer();1163 this._detached = true;1164 this._detachedCallback();1165 const error = new Error('Frame was detached');...
page.js
Source:page.js
...305 const locator = options.locator;306 const rafrafScreenshot = locator ? async (progress, timeout) => {307 return await locator.frame.rafrafTimeoutScreenshotElementWithProgress(progress, locator.selector, timeout, options.screenshotOptions || {});308 } : async (progress, timeout) => {309 await this.mainFrame().rafrafTimeout(timeout);310 return await this._screenshotter.screenshotPage(progress, options.screenshotOptions || {});311 };312 const comparator = (0, _comparators.getComparator)('image/png');313 const controller = new _progress.ProgressController(metadata, this);314 const isGeneratingNewScreenshot = !options.expected;315 if (isGeneratingNewScreenshot && options.isNot) return {316 errorMessage: '"not" matcher requires expected result'317 };318 let intermediateResult = undefined;319 return controller.run(async progress => {320 let actual;321 let previous;322 const pollIntervals = [0, 100, 250, 500];323 while (true) {...
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.waitForTimeout(1000);7 await page.screenshot({ path: `example.png` });8 await browser.close();9})();10const { chromium } = require('playwright');11(async () => {12 const browser = await chromium.launch();13 const context = await browser.newContext();14 const page = await context.newPage();15 await page.rafrafTimeout(1000);16 await page.screenshot({ path: `example.png` });17 await browser.close();18})();19const { chromium } = require('playwright');20(async () => {21 const browser = await chromium.launch();22 const context = await browser.newContext();23 const page = await context.newPage();24 await page.rafrafTimeout(1000);25 await page.screenshot({ path: `example.png` });26 await browser.close();27})();28const { chromium } = require('playwright');29(async () => {30 const browser = await chromium.launch();31 const context = await browser.newContext();32 const page = await context.newPage();33 await page.rafrafTimeout(1000);34 await page.screenshot({ path: `example.png` });35 await browser.close();36})();37const { chromium } = require('playwright');38(async () => {39 const browser = await chromium.launch();40 const context = await browser.newContext();41 const page = await context.newPage();42 await page.rafrafTimeout(1000);43 await page.screenshot({ path: `example.png` });44 await browser.close();45})();
Using AI Code Generation
1const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');2const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');3await rafrafTimeout();4await rafrafTimeout(5000);5await rafrafTimeout(5000);6await page.click('text=Next Page');7await rafrafTimeout(5000);8await page.click('text=Click Here');9await rafrafTimeout(5000);10await page.click('text=Click Here');11await rafrafTimeout(5000);12await page.click('text=Click Here');13await rafrafTimeout(5000);14await page.click('text=Click Here');15await rafrafTimeout(5000);16await page.click('text=Click Here');17await rafrafTimeout(5000);
Using AI Code Generation
1const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');2rafrafTimeout(1000);3const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');4rafrafTimeout(1000);5const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');6(async () => {7 await rafrafTimeout(1000);8})();9const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');10(async () => {11 await rafrafTimeout(1000);12 console.log('Hello World');13})();14const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');15(async () => {16 await rafrafTimeout(1000);17 await asyncFunction();18})();19const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');20(async () => {21 await rafrafTimeout(1000);22 await asyncFunction();23})();24const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');25(async () => {
Using AI Code Generation
1const { rafrafTimeout } = require('@playwright/test');2test('rafrafTimeout', async ({ page }) => {3 await rafrafTimeout(2000);4});5const { rafrafTimeout } = require('@playwright/test');6test('rafrafTimeout', async ({ page }) => {7 await rafrafTimeout(2000);8});9const { rafrafTimeout } = require('@playwright/test');10test('rafrafTimeout', async ({ page }) => {11 await rafrafTimeout(2000);12});13const { rafrafTimeout } = require('@playwright/test');14test('rafrafTimeout', async ({ page }) => {15 await rafrafTimeout(2000);16});17const { rafrafTimeout } = require('@playwright/test');18test('rafrafTimeout', async ({ page }) => {19 await rafrafTimeout(2000);20});21const { rafrafTimeout } = require('@playwright/test');22test('rafrafTimeout', async ({ page }) => {23 await rafrafTimeout(2000);24});25const { rafrafTimeout } = require('@playwright/test');26test('rafrafTimeout', async ({ page }) => {
Using AI Code Generation
1const { rafrafTimeout } = require('playwright');2(async () => {3 await rafrafTimeout(1000);4})();5module.exports = {6};7{8 "scripts": {9 },10 "devDependencies": {11 }12}13(node:19132) UnhandledPromiseRejectionWarning: Error: Test timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/username/Projects/ProjectName/test.js)14(async () => {15 await rafrafTimeout(1000);16})();17(async () => {18 await rafrafTimeout(1000);19})();
Using AI Code Generation
1const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');2await rafrafTimeout(5000);3await page.evaluate(async () => {4 const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');5 await rafrafTimeout(5000);6});7await page.evaluateHandle(async () => {8 const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');9 await rafrafTimeout(5000);10});11await page.exposeFunction('rafrafTimeout', async () => {12 const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');13 await rafrafTimeout(5000);14});15await page.route('**/*', async route => {16 const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');17 await rafrafTimeout(5000);18});19await page.waitForFunction(async () => {20 const { rafrafTimeout } = require('playwright/lib/utils/rafrafTimeout');21 await rafrafTimeout(5000);22});23await page.waitForTimeout(5000);24await page.waitForURL('**/*', { timeout: 5000 });25await page.waitForRequest('**/*', { timeout: 5000 });26await page.waitForResponse('**/*', { timeout: 5000 });27await page.waitForSelector('**/*', { timeout: 5000 });28await page.waitForXPath('**/*', { timeout: 5000 });
Using AI Code Generation
1const { rafrafTimeout } = require('playwright');2(async () => {3 await rafrafTimeout(1000);4 console.log("I'm executed after 1 second");5})();6const { rafrafInterval } = require('playwright');7(async () => {8 const interval = rafrafInterval(() => {9 console.log("I'm executed every 1 second");10 }, 1000);11 await rafrafTimeout(5000);12 interval.clear();13})();14const { rafrafUntil } = require('playwright');15(async () => {16 let count = 0;17 await rafrafUntil(() => {18 count++;19 console.log("I'm executed every 1 second");20 return count === 5;21 }, 1000);22})();23const { rafraf } = require('playwright');24(async () => {25 await rafraf(() => {26 console.log("I'm executed in the next rAF");27 });28})();
Using AI Code Generation
1const { rafrafTimeout } = require('playwright');2(async () => {3 await rafrafTimeout(1000);4})();5 at Object.rafrafTimeout (C:\Users\user\Documents\Playwright\test.js:3:3)6 at ModuleJob.run (internal/modules/esm/module_job.js:152:23)7 at async Loader.import (internal/modules/esm/loader.js:166:24)8 at async Object.loadESM (internal/process/esm_loader.js:68:5)9const { waitForTimeout } = require('playwright');10(async () => {11 await waitForTimeout(1000);12})();
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!!