Best JavaScript code snippet using wpt
sxg-util.js
Source:sxg-util.js
1// Opens |url| in an iframe, establish a message channel with it, and waits for2// a message from the frame content. Returns a promise that resolves with the3// data of the message, or rejects on 3000ms timeout.4function openSXGInIframeAndWaitForMessage(test_object, url, referrerPolicy) {5 return new Promise(async (resolve, reject) => {6 // We can't catch the network error on iframe. So we use the timer.7 test_object.step_timeout(() => reject('timeout'), 3000);8 const frame = await withIframe(url, 'sxg_iframe', referrerPolicy);9 const channel = new MessageChannel();10 channel.port1.onmessage = (event) => resolve(event.data);11 frame.contentWindow.postMessage(12 {port: channel.port2}, '*', [channel.port2]);13 });14}15function withIframe(url, name, referrerPolicy) {16 return new Promise((resolve, reject) => {17 const frame = document.createElement('iframe');18 frame.src = url;19 frame.name = name;20 if (referrerPolicy !== undefined) {21 frame.referrerPolicy = referrerPolicy;22 }23 frame.onload = () => resolve(frame);24 frame.onerror = () => reject('failed to load ' + url);25 document.body.appendChild(frame);26 });27}28function loadScript(url) {29 return new Promise((resolve, reject) => {30 const scriptTag = document.createElement('script');31 scriptTag.src = url;32 scriptTag.onload = () => resolve();33 scriptTag.onerror = () => reject('failed to load ' + url);34 document.head.appendChild(scriptTag);35 });36}37function innerURLOrigin() {38 return 'https://127.0.0.1:8444';39}40function runReferrerTests(test_cases) {41 for (const i in test_cases) {42 const test_case = test_cases[i];43 promise_test(async (t) => {44 const sxgUrl = test_case.origin + '/signed-exchange/resources/sxg/' +45 test_case.sxg;46 const message =47 await openSXGInIframeAndWaitForMessage(48 t, sxgUrl, test_case.referrerPolicy);49 assert_false(message.is_fallback);50 assert_equals(message.referrer, test_case.expectedReferrer);51 const invalidSxgUrl =52 test_case.origin + '/signed-exchange/resources/sxg/invalid-' +53 test_case.sxg;54 const fallbackMessage =55 await openSXGInIframeAndWaitForMessage(56 t, invalidSxgUrl, test_case.referrerPolicy);57 assert_true(fallbackMessage.is_fallback);58 assert_equals(fallbackMessage.referrer, test_case.expectedReferrer);59 }, 'Referrer of SignedHTTPExchange test : ' + JSON.stringify(test_case));60 }61}62function addPrefetch(url) {63 const link = document.createElement('link');64 link.rel = 'prefetch';65 link.href = url;66 document.body.appendChild(link);...
Using AI Code Generation
1const wptdriver = require('wptdriver');2const driver = new wptdriver.WebDriver();3driver.openSXGInIframeAndWaitForMessage(url, 'message');4driver.quit();5const wptdriver = require('wptdriver');6const driver = new wptdriver.WebDriver();7driver.openSXGInIframeAndWaitForMessage(url, 'message');8driver.quit();9const wptdriver = require('wptdriver');10const driver = new wptdriver.WebDriver();11driver.openSXGInIframeAndWaitForMessage(url, 'message');12driver.quit();13const wptdriver = require('wptdriver');14const driver = new wptdriver.WebDriver();15driver.openSXGInIframeAndWaitForMessage(url, 'message');16driver.quit();17const wptdriver = require('wptdriver');18const driver = new wptdriver.WebDriver();19driver.openSXGInIframeAndWaitForMessage(url, 'message');20driver.quit();21const wptdriver = require('wptdriver');22const driver = new wptdriver.WebDriver();23driver.openSXGInIframeAndWaitForMessage(url, 'message');24driver.quit();
Using AI Code Generation
1 testRunner.log("Received message from iframe: " + message);2});3testRunner.log("Verifying the message received from the iframe");4if (message == "hello") {5 testRunner.log("Received the expected message from the iframe");6} else {7 testRunner.log("Did not receive the expected message from the iframe");8}9testRunner.log("Verifying the message received from the iframe");10assert_equals(message, "hello", "Received the expected message from the iframe");11testRunner.log("Verifying the message received from the iframe");12assert_true(message == "hello", "Received the expected message from the iframe");13testRunner.log("Verifying the message received from the iframe");14assert_false(message == "hello", "Did not receive the expected message from the iframe");
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!!