Best JavaScript code snippet using wpt
commands.js
Source:commands.js
1// ***********************************************2// This example commands.js shows you how to3// create various custom commands and overwrite4// existing commands.5//6// For more comprehensive examples of custom7// commands please read more here:8// https://on.cypress.io/custom-commands9// ***********************************************10//11//12// -- This is a parent command --13// Cypress.Commands.add("login", (email, password) => { ... })14//15//16// -- This is a child command --17// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })18//19//20// -- This is a dual command --21// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })22//23//24// -- This will overwrite an existing command --25// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })26import 'cypress-wait-until';27import 'cypress-iframe'28Cypress.Commands.add('waitForStripeIdealIframe', callback => {29 const outerIframe = cy30 .get('iframe[name*="__privateStripeFrame"]')31 .its("0.contentDocument.body")32 .should("not.be.empty")33 .then(cy.wrap).click()34 35 // const innerIframe = outerIframe36 // .find('iframe[name*="__privateStripeFrame"]')37 // .its("0.contentDocument.body")38 // .should("not.be.empty")39 // .then(cy.wrap)40 // return innerIframe41});42Cypress.Commands.add('waitForStripe3dIframeMember', callback => {43 const outerIframe = cy44 .get('iframe[name*="__privateStripeFrame"]')45 .its("0.contentDocument.body")46 .should("not.be.empty")47 .then(cy.wrap)48 const innerIframe = outerIframe49 .find('iframe[id=challengeFrame]')50 .its("0.contentDocument.body")51 .should("not.be.empty")52 .and('include.text','Politicalnetworks')53 .then(cy.wrap)54 return innerIframe55});56Cypress.Commands.add('waitForStripe3dIframe', callback => {57 const outerIframe = cy58 .get('iframe[name*="__privateStripeFrame"]')59 .its("0.contentDocument.body")60 .should("not.be.empty")61 .then(cy.wrap)62 63 const innerIframe = outerIframe64 .find('iframe[id*="challengeFrame"]')65 .its("0.contentDocument.body")66 .should("not.be.empty")67 .then(cy.wrap)68 69 return innerIframe70 });71 Cypress.Commands.add('failForStripe3dIframe', callback => {72 const outerIframe = cy73 .get('iframe[name*="__privateStripeFrame"]')74 .its("0.contentDocument.body")75 .should("not.be.empty")76 .then(cy.wrap)77 78 const innerIframe = outerIframe79 .find('iframe[id=challengeFrame]')80 .its("0.contentDocument.body")81 .should("not.be.empty")82 .then(cy.wrap)83 84 return innerIframe85 });86 Cypress.Commands.add('waitForStripe3DIframe', callback => {87 const outerIframe = cy88 .get('iframe[name*="__privateStripeFrame"]')89 .its("0.contentDocument.body")90 .should("not.be.empty")91 .then(cy.wrap)92 93 const innerIframe = outerIframe94 .find('iframe[id=challengeFrame]')95 .its("0.contentDocument.body")96 .should("not.be.empty")97 .then(cy.wrap)98 99 return innerIframe100 .find("iframe[name=acsFrame]")101 .its("0.contentDocument.body")102 .should("not.be.empty")103 .then(cy.wrap)104 });105 Cypress.Commands.add('failForStripe3DIframe', callback => {106 const outerIframe = cy107 .get('iframe[name*="__privateStripeFrame"]')108 .its("0.contentDocument.body")109 .should("not.be.empty")110 .then(cy.wrap)111 112 const innerIframe = outerIframe113 .find('iframe[id=challengeFrame]')114 .its("0.contentDocument.body")115 .should("not.be.empty")116 .then(cy.wrap)117 118 return innerIframe119 .find("iframe[name=acsFrame]")120 .its("0.contentDocument.body")121 .should("not.be.empty")122 .then(cy.wrap)123 });124const getStripeSCAIframe = () => {125 const outerIframe = cy126 .get('iframe[src*="https://js.stripe.com/v3/authorize"]')127 .its("0.contentDocument.body")128 .should("not.be.empty")129 .then(cy.wrap)130 131 const innerIframe = outerIframe132 .find('iframe[src*="https://hooks.stripe.com"]')133 .its("0.contentDocument.body")134 .should("not.be.empty")135 .then(cy.wrap)136 137 return innerIframe138 .find('button', 'Complete').click()139 // .its("0.contentDocument.body")140 // .should("not.be.empty")141 // .then(cy.wrap)142 }143Cypress.Commands.add(144 'iframeLoaded',145 {prevSubject: 'element'},146 ($iframe) => {147 const contentWindow = $iframe.prop('contentWindow');148 return new Promise(resolve => {149 if (150 contentWindow &&151 contentWindow.document.readyState === 'complete'152 ) {153 resolve(contentWindow)154 } else {155 $iframe.on('load', () => {156 resolve(contentWindow)157 })158 }159 })160 });161Cypress.Commands.add(162 'getInDocument',163 {prevSubject: 'document'},164 (document, selector) => Cypress.$(selector, document)165)166Cypress.Commands.add(167 'getWithinIframe',168 (targetElement) => cy.get('iframe').iframeLoaded().its('document').getInDocument(targetElement)169)170Cypress.Commands.add('getIframeBody', () => {171 // get the iframe > document > body172 // and retry until the body element is not empty173 cy.log('getIframeBody')174 return cy175 .get('iframe[data-cy="the-frame"]', { log: false })176 .its('0.contentDocument.body', { log: false }).should('not.be.empty')177 // wraps "body" DOM element to allow178 // chaining more Cypress commands, like ".find(...)"179 // https://on.cypress.io/wrap180 .then((body) => cy.wrap(body, { log: false }))181 })182 Cypress.Commands.add('isIFrameReady', () => {183 return cy.window().then({ timeout: 10 * 1000 }, window => {184 return new Cypress.Promise(resolve => {185 window.addEventListener('message', e => {186 const data = typeof e.data === 'string' ? JSON.parse(e.data) : e.data187 188 if (data.code === 'Ready') {189 resolve()190 }191 })192 })193 })194 })195 Cypress.Commands.add('iframe', { prevSubject: 'element' }, $iframe => {196 return new Cypress.Promise(resolve => {197 $iframe.on('load', () => {198 resolve($iframe.contents().find('body'));199 });200 });201});202Cypress.Commands.add(203 'selectNth',204 { prevSubject: 'element' },205 (subject, pos) => {206 cy.wrap(subject)207 .children('option')208 .eq(pos)209 .then(e => {210 cy.wrap(subject).select(e.val())211 })212 }213)214Cypress.Commands.add("clickRecaptcha", () => {215 cy.window().then(win => {216 win.document217 .querySelector("iframe[src*='recaptcha']")218 .contentDocument.getElementById("recaptcha-token")219 .click();220 });...
utils.ts
Source:utils.ts
1/**2 * å
éå
èæ ·å¼å° iframe3 * @param innerIframe iframe4 */5function cloneInternalStyle2Iframe(innerIframe: HTMLIFrameElement) {6 const internalStyle = document.querySelectorAll('style');7 const fragment = document.createDocumentFragment();8 internalStyle.forEach((item) => fragment.appendChild(item.cloneNode(true)));9 innerIframe.contentDocument!.head.appendChild(fragment);10}11/**12 * å
éå¤èæ ·å¼å° iframe13 * @param innerIframe iframe14 */15function cloneExternalStyle2Iframe(innerIframe: HTMLIFrameElement) {16 const links = document.head.querySelectorAll('link');17 /* å¤èæ ·å¼è¯·æ±éå */18 const linkRequestQueue: Promise<Response | string>[] = [];19 links.forEach((item) =>20 linkRequestQueue.push(fetch(item.href).then((response) => response.text())),21 );22 return Promise.allSettled(linkRequestQueue).then((values) => {23 const fragment = document.createDocumentFragment();24 values.forEach((item) => {25 if (item.status === 'fulfilled') {26 const style = document.createElement('style');27 style.innerHTML = item.value as string;28 fragment.appendChild(style);29 }30 });31 innerIframe.contentDocument!.head.appendChild(fragment);32 });33}34/**35 * åæ¶ iframe ç html æå°å®½åº¦ min-width36 * @param innerIframe iframe37 */38function unsetIframeHtmlMinWidth(innerIframe: HTMLIFrameElement) {39 const style = document.createElement('style');40 style.innerHTML = 'html{min-width: unset;}';41 innerIframe.contentDocument!.head.appendChild(style);42}43/**44 * æå°45 * @param element è¦æå°ç dom å
ç´ 46 * @param innerIframe iframe47 */48function print(element: HTMLElement, innerIframe: HTMLIFrameElement) {49 const cloneElement = element.cloneNode(true);50 innerIframe.contentDocument!.body.appendChild(cloneElement);51 innerIframe.contentWindow?.print();52 document.body.removeChild(innerIframe);53}54/**55 * éè¿ iframe å®ç°çæå°56 * @param element è¦æå°ç dom å
ç´ 57 */58export default function ngfePrint(element: HTMLElement) {59 let innerIframe = document.createElement('iframe');60 document.body.appendChild(innerIframe);61 cloneInternalStyle2Iframe(innerIframe);62 // @ts-ignore63 if (window.__POWERED_BY_QIANKUN__) {64 // å¦æå¨åºåº§ï¼qiankun ä¼å°ååºç¨æ ·å¼è½¬æ¢ä¸ºå
èæ ·å¼ï¼å æ¤ä¸åéè¦å°ååºç¨å¤èæ ·å¼è½¬ä¸ºå
èæ ·å¼ã65 // ä½å¦ææ¯åºåº§ä½¿ç¨æ¬æ¹æ³ï¼å¯è½æé®é¢ï¼å 为åºåº§é¨åæ ·å¼åå¨äºå¤èæ ·å¼ä¸ã66 unsetIframeHtmlMinWidth(innerIframe);67 print(element, innerIframe);68 } else {69 // å¦æä¸å¨åºåº§ä¸ï¼åéè¦å°ååºç¨å¤èæ ·å¼è½¬ä¸ºå
èæ ·å¼ï¼å 为ååºç¨æ ·å¼æ¯éè¿ umi.css å¤èæ ·å¼å¼å
¥çã70 cloneExternalStyle2Iframe(innerIframe).then(() => {71 unsetIframeHtmlMinWidth(innerIframe);72 print(element, innerIframe);73 });74 }...
ignore-opens-during-unload.window.js
Source:ignore-opens-during-unload.window.js
1for (const [ev, target] of [2 ["beforeunload", iframe => iframe.contentWindow],3 ["pagehide", iframe => iframe.contentWindow],4 ["unload", iframe => iframe.contentWindow],5 ["visibilitychange", iframe => iframe.contentDocument],6]) {7 async_test(t => {8 const iframe = document.body.appendChild(document.createElement("iframe"));9 t.add_cleanup(() => iframe.remove());10 iframe.src = "/common/blank.html";11 iframe.onload = t.step_func(() => {12 target(iframe).addEventListener(ev, t.step_func_done(() => {13 assert_not_equals(iframe.contentDocument.childNodes.length, 0);14 assert_equals(iframe.contentDocument.open(), iframe.contentDocument);15 assert_not_equals(iframe.contentDocument.childNodes.length, 0);16 }));17 iframe.src = "about:blank";18 });19 }, `document.open should bail out when ignore-opens-during-unload is greater than 0 during ${ev} event (in top-level browsing context)`);20 async_test(t => {21 const iframe = document.body.appendChild(document.createElement("iframe"));22 t.add_cleanup(() => iframe.remove());23 iframe.src = "/common/blank.html?1";24 iframe.onload = t.step_func(() => {25 const doc = iframe.contentDocument;26 const innerIframe = doc.body.appendChild(doc.createElement("iframe"));27 innerIframe.src = "/common/blank.html?2";28 innerIframe.onload = t.step_func(() => {29 // Navigate the parent, listen on the child, and open() the parent.30 target(innerIframe).addEventListener(ev, t.step_func_done(() => {31 assert_not_equals(iframe.contentDocument.childNodes.length, 0);32 iframe.contentDocument.open();33 assert_not_equals(iframe.contentDocument.childNodes.length, 0);34 }));35 iframe.src = "about:blank";36 });37 });38 }, `document.open should bail out when ignore-opens-during-unload is greater than 0 during ${ev} event (open(parent) while unloading parent and child)`);39 async_test(t => {40 const iframe = document.body.appendChild(document.createElement("iframe"));41 t.add_cleanup(() => iframe.remove());42 iframe.src = "/common/blank.html?1";43 iframe.onload = t.step_func(() => {44 const doc = iframe.contentDocument;45 const innerIframe = doc.body.appendChild(doc.createElement("iframe"));46 innerIframe.src = "/common/blank.html?2";47 innerIframe.onload = t.step_func(() => {48 // Navigate the child, listen on the child, and open() the parent.49 target(innerIframe).addEventListener(ev, t.step_func_done(() => {50 assert_not_equals(iframe.contentDocument.childNodes.length, 0);51 iframe.contentDocument.open();52 assert_equals(iframe.contentDocument.childNodes.length, 0);53 }));54 innerIframe.src = "about:blank";55 });56 });57 }, `document.open should bail out when ignore-opens-during-unload is greater than 0 during ${ev} event (open(parent) while unloading child only)`);...
Using AI Code Generation
1var wptdriver = require('wptdriver');2wptdriver.innerIframe('iframe1', function() {3 wptdriver.innerIframe('iframe2', function() {4 wptdriver.innerIframe('iframe3', function() {5 wptdriver.clickElement('btn', function() {6 wptdriver.findElement('txt', function(element) {7 wptdriver.getElementText(element, function(txt) {8 wptdriver.log(txt);9 });10 });11 });12 });13 });14});
Using AI Code Generation
1try {2 if (err) {3 console.log(err);4 } else {5 console.log(data);6 }7 });8} catch (ex) {9 console.log(ex);10}11### new WebPageTest(serverUrl, apiKey, debug)12### runTest(url, options, callback)13### getTestStatus(testId, callback)14### getTestResults(testId, callback)15### getLocations(callback)16### getTesters(callback)17### getLocations(callback)
Using AI Code Generation
1var wptdriver = require('wptdriver');2var wptdriver = require('wptdriver');3var wptdriver = require('wptdriver');4var wptdriver = require('wptdriver');5var wptdriver = require('wptdriver');6var wptdriver = require('wptdriver');7var wptdriver = require('wptdriver');8var wptdriver = require('wptdriver');9var wptdriver = require('wptdriver');10var wptdriver = require('wptdriver');11var wptdriver = require('wptdriver');12var wptdriver = require('wptdriver');
Using AI Code Generation
1var wptDriver = require('wpt-driver');2 if (err) {3 console.log(err);4 } else {5 console.log(result);6 }7});8var wptDriver = require('wpt-driver');9 if (err) {10 console.log(err);11 } else {12 console.log(result);13 }14});15var wptDriver = require('wpt-driver');16 if (err) {17 console.log(err);18 } else {19 console.log(result);20 }21});22var wptDriver = require('wpt-driver');23 if (err) {24 console.log(err);25 } else {26 console.log(result);27 }28});29var wptDriver = require('wpt-driver');30 if (err) {31 console.log(err);32 } else {33 console.log(result);34 }35});36var wptDriver = require('wpt-driver');37 if (err) {38 console.log(err);39 } else {40 console.log(result);41 }42});43var wptDriver = require('wpt-driver');44 if (err) {45 console.log(err);46 } else {47 console.log(result);48 }49});50var wptDriver = require('wpt-driver');
Using AI Code Generation
1var iframe = wptb.innerIframe();2var iframeWindow = iframe.contentWindow;3var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;4iframeWindow.postMessage("innerIframe", "*");5window.addEventListener("message", function(event) {6 if (event.data == "innerIframe") {7 }8});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!