Best JavaScript code snippet using wpt
index.js
Source:index.js
1(function app() {2 'use strict';3 /**4 * Remote origin for iframe5 * @type {string}6 */7 var REMOTE_ORIGIN = 'http://localhost:8080';8 /**9 * Previously used email10 * @type {string|null}11 */12 var email = localStorage.getItem('email') || null;13 // Shared states14 var remoteFrame = null;15 var remote = null;16 // DOM17 var $log = document.getElementById('list-log');18 var $email = document.getElementById('list-log');19 var $btnLogin = document.getElementById('btn-login');20 var $btnLogout = document.getElementById('btn-logout');21 /**22 * Load remote frame23 * @param {Function} Done callback24 */25 function loadRemote() {26 remoteFrame = document.createElement('iframe');27 remoteFrame.style.height = '1px';28 remoteFrame.onerror = function() {29 log('[Remote] Network Error');30 };31 remoteFrame.onload = function() {32 log('[Remote] Loaded DOM');33 };34 remoteFrame.src = REMOTE_ORIGIN + '/index.html';35 document.body.appendChild(remoteFrame);36 log('[Remote] Injected: ' + REMOTE_ORIGIN);37 }38 /**39 * Send payload to remote window40 * @param {object} Payload41 */42 function toRemote(payload) {43 remote.postMessage(JSON.stringify(payload), REMOTE_ORIGIN);44 }45 function verify(assertion) {46 var req = new XMLHttpRequest({47 mozSystem: true48 });49 req.open('POST', 'https://verifier.login.persona.org/verify', true);50 req.addEventListener('load', function() {51 if (req.status != 200) {52 log('Verify request failed with bad status');53 return;54 }55 var response = JSON.parse(req.responseText);56 if (response.status != 'okay') {57 log('[Verify] Invalid assertion: ' + response.reason);58 return;59 }60 log('[Verify] Verified ' + response.email + ' for ' +61 response.audience + '. Issued by ' + response.issuer + ', valid until ' + response.expires);62 loginByEmail(response.email, true);63 })64 req.addEventListener('error', function() {65 log('[Verify] Request errored');66 });67 var data = 'assertion=' + encodeURIComponent(assertion) + '&audience=' + encodeURIComponent(REMOTE_ORIGIN);68 log('[Verify] Sending');69 req.send(data);70 }71 function loginByEmail(email, store) {72 if (store) {73 localStorage.setItem('email', email);74 }75 $email.textContent = email;76 document.documentElement.classList.add('state-loggedin');77 }78 function logout() {79 localStorage.removeItem('email');80 $email.textContent = '[none]';81 document.documentElement.classList.remove('state-loggedin');82 }83 window.addEventListener('message', function fromRemote(evt) {84 if (evt.origin != REMOTE_ORIGIN) {85 console.warn('Ignored message from ' + evt.origin);86 return;87 }88 evt.stopPropagation();89 var payload = JSON.parse(evt.data);90 switch (payload.event) {91 case 'load':92 log('[Remote] Loaded scripts');93 remote = remoteFrame.contentWindow;94 toRemote({95 event: 'setup',96 loggedInUser: email97 });98 break;99 case 'ready':100 log('[Persona] Ready');101 document.documentElement.classList.add('state-ready');102 break;103 case 'login':104 log('[Persona] Logged in');105 var assertion = payload.assertion;106 verify(assertion);107 break;108 case 'logout':109 log('[Persona] Logged out');110 logout();111 break;112 }113 }, false);114 $btnLogin.addEventListener('click', function(evt) {115 evt.preventDefault();116 log('[Remote] Message: login');117 toRemote({118 event: 'login'119 });120 });121 $btnLogout.addEventListener('click', function(evt) {122 evt.preventDefault();123 log('[Remote] Message: logout');124 toRemote({125 event: 'logout'126 });127 });128 log('App starting up ' + location.origin);129 // Update UI130 if (email) {131 loginByEmail(email);132 }133 loadRemote();134 // Utility logs135 function log(msg) {136 console.log(msg);137 var $item = document.createElement('li');138 $item.textContent = msg;139 $log.insertBefore($item, $log.firstChild);140 }...
browser_target_command_frames_reload_server_side_targets.js
Source:browser_target_command_frames_reload_server_side_targets.js
1/* Any copyright is dedicated to the Public Domain.2 http://creativecommons.org/publicdomain/zero/1.0/ */3"use strict";4// Test that the framework handles reloading a document with multiple remote frames (See Bug 1724909).5const REMOTE_ORIGIN = "https://example.com/";6const REMOTE_IFRAME_URL_1 =7 REMOTE_ORIGIN + "/document-builder.sjs?html=first_remote_iframe";8const REMOTE_IFRAME_URL_2 =9 REMOTE_ORIGIN + "/document-builder.sjs?html=second_remote_iframe";10const TEST_URL =11 "https://example.org/document-builder.sjs?html=org" +12 `<iframe src=${REMOTE_IFRAME_URL_1}></iframe>` +13 `<iframe src=${REMOTE_IFRAME_URL_2}></iframe>`;14add_task(async function() {15 // Turn on server side targets16 await pushPref("devtools.target-switching.server.enabled", true);17 // Create a TargetCommand for a given test tab18 const tab = await addTab(TEST_URL);19 const commands = await CommandsFactory.forTab(tab);20 const targetCommand = commands.targetCommand;21 const { TYPES } = targetCommand;22 await targetCommand.startListening();23 // Assert that watchTargets will call the create callback for all existing frames24 const targets = [];25 const destroyedTargets = [];26 const onAvailable = ({ targetFront }) => {27 targets.push(targetFront);28 };29 const onDestroyed = ({ targetFront }) => {30 destroyedTargets.push(targetFront);31 };32 await targetCommand.watchTargets([TYPES.FRAME], onAvailable, onDestroyed);33 await waitFor(() => targets.length === 3);34 ok(35 true,36 "We are notified about the top-level document and the 2 remote iframes"37 );38 info("Reload the page");39 // When a new target will be created, we need to wait until it's fully processed40 // to avoid pending promises.41 const onNewTargetProcessed = targetCommand.once("processed-available-target");42 gBrowser.reloadTab(tab);43 await onNewTargetProcessed;44 await waitFor(() => targets.length === 6 && destroyedTargets.length === 3);45 // Get the previous targets in a dedicated array and remove them from `targets`46 const previousTargets = targets.splice(0, 3);47 ok(48 previousTargets.every(targetFront => targetFront.isDestroyed()),49 "The previous targets are all destroyed"50 );51 ok(52 targets.every(targetFront => !targetFront.isDestroyed()),53 "The new targets are not destroyed"54 );55 info("Reload one of the iframe");56 SpecialPowers.spawn(tab.linkedBrowser, [], () => {57 const iframeEl = content.document.querySelector("iframe");58 SpecialPowers.spawn(iframeEl.browsingContext, [], () => {59 content.document.location.reload();60 });61 });62 await waitFor(63 () =>64 targets.length + previousTargets.length === 7 &&65 destroyedTargets.length === 466 );67 const iframeTarget = targets.find(t => t === destroyedTargets.at(-1));68 ok(iframeTarget, "Got the iframe target that got destroyed");69 for (const target of targets) {70 if (target == iframeTarget) {71 ok(72 target.isDestroyed(),73 "The iframe target we navigated from is destroyed"74 );75 } else {76 ok(77 !target.isDestroyed(),78 `Target ${target.actorID}|${target.url} isn't destroyed`79 );80 }81 }82 targetCommand.unwatchTargets([TYPES.FRAME], onAvailable, onDestroyed);83 targetCommand.destroy();84 BrowserTestUtils.removeTab(tab);85 await commands.destroy();...
server.ts
Source:server.ts
1const LOCAL_ORIGIN = 'http://localhost'2const REMOTE_ORIGIN = 'http://www.uniquemo.cn'3export const PORT = 80804export const SERVER = __LOCALHOST__ ? `${LOCAL_ORIGIN}:${PORT}/api` : `${REMOTE_ORIGIN}/api`...
Using AI Code Generation
1var wpt = require('webpagetest');2var api = new wpt('www.webpagetest.org');3}, function(err, data) {4 if (err) {5 console.log(err);6 return;7 }8 console.log(data.data.runs[1].firstView.videoFrames);9 console.log(data.data.runs[1].firstView.video);10});
Using AI Code Generation
1var wpt = require('webpagetest');2var client = wpt('www.webpagetest.org');3client.runTest(url, {4 lighthouseConfig: {5 settings: {6 },7 },8}, function (err, result) {9 if (err) {10 console.log(err);11 return;12 }13 console.log(result);14});15{ statusCode: 400,16 { server: 'nginx',17 expires: 'Fri, 01 Jan 1990 00:00:00 GMT' } }18{ statusCode: 400,19 { server: 'nginx',
Using AI Code Generation
1var wpt = require('wpt');2 videoParams: {3 }4}, function(err, data) {5 if (err) return console.error(err);6 console.log(data);7});8var wpt = require('wpt');9 videoParams: {10 }11}, function(err, data) {12 if (err) return console.error(err);13 console.log(data);14});15var wpt = require('wpt');16 videoParams: {
Using AI Code Generation
1var wptools = require('wptools');2var wp = wptools.page('Albert Einstein');3wp.get(function(err, resp) {4 if (err) {5 console.log(err);6 return;7 }8 console.log(resp);9});10var wptools = require('wptools');11var wp = wptools.page('Albert Einstein');12wp.get(function(err, resp) {13 if (err) {14 console.log(err);15 return;16 }17 console.log(resp);18});19var wptools = require('wptools');20var wp = wptools.page('Albert Einstein');21wp.get(function(err, resp) {22 if (err) {23 console.log(err);24 return;25 }26 console.log(resp);27});28var wptools = require('wptools');29var wp = wptools.page('Albert Einstein');30wp.get(function(err, resp) {31 if (err) {32 console.log(err);33 return;34 }35 console.log(resp);36});37var wptools = require('wptools');38var wp = wptools.page('Albert Einstein');39wp.get(function(err, resp) {40 if (err) {41 console.log(err);42 return;43 }44 console.log(resp);45});46var wptools = require('wptools');47var wp = wptools.page('Albert Einstein');48wp.get(function(err, resp) {49 if (err) {50 console.log(err);51 return;52 }53 console.log(resp);54});55var wptools = require('wptools');56var wp = wptools.page('Albert Einstein');57wp.get(function(err, resp) {58 if (err) {59 console.log(err);60 return;61 }62 console.log(resp);63});64var wptools = require('wptools');65var wp = wptools.page('Albert Einstein');66wp.get(function(err, resp) {67 if (err) {68 console.log(err);69 return;70 }
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!!