Best JavaScript code snippet using wpt
RTCPeerConnection-perfect-negotiation-helper.js
Source:RTCPeerConnection-perfect-negotiation-helper.js
1'use strict'2function peer(other, polite, fail = null) {3 const send = (tgt, msg) => tgt.postMessage(JSON.parse(JSON.stringify(msg)),4 "*");5 if (!fail) fail = e => send(window.parent, {error: `${e.name}: ${e.message}`});6 const pc = new RTCPeerConnection();7 if (!window.assert_equals) {8 window.assert_equals = (a, b, msg) => a === b ||9 fail(new Error(`${msg} expected ${b} but got ${a}`));10 }11 const commands = {12 async addTransceiver() {13 const transceiver = pc.addTransceiver("video");14 await new Promise(r => pc.addEventListener("negotiated", r, {once: true}));15 if (!transceiver.currentDirection) {16 // Might have just missed the negotiation train. Catch next one.17 await new Promise(r => pc.addEventListener("negotiated", r, {once: true}));18 }19 assert_equals(transceiver.currentDirection, "sendonly", "have direction");20 return pc.getTransceivers().length;21 },22 async simpleConnect() {23 const p = commands.addTransceiver();24 await new Promise(r => pc.oniceconnectionstatechange =25 () => pc.iceConnectionState == "connected" && r());26 return await p;27 },28 async getNumTransceivers() {29 return pc.getTransceivers().length;30 },31 };32 try {33 pc.addEventListener("icecandidate", ({candidate}) => send(other,34 {candidate}));35 let makingOffer = false, ignoreOffer = false;36 let srdAnswerPending = false;37 pc.addEventListener("negotiationneeded", async () => {38 try {39 assert_equals(pc.signalingState, "stable", "negotiationneeded always fires in stable state");40 assert_equals(makingOffer, false, "negotiationneeded not already in progress");41 makingOffer = true;42 await pc.setLocalDescription();43 assert_equals(pc.signalingState, "have-local-offer", "negotiationneeded not racing with onmessage");44 assert_equals(pc.localDescription.type, "offer", "negotiationneeded SLD worked");45 send(other, {description: pc.localDescription});46 } catch (e) {47 fail(e);48 } finally {49 makingOffer = false;50 }51 });52 window.onmessage = async ({data: {description, candidate, run}}) => {53 try {54 if (description) {55 // If we have a setRemoteDescription() answer operation pending, then56 // we will be "stable" by the time the next setRemoteDescription() is57 // executed, so we count this being stable when deciding whether to58 // ignore the offer.59 let isStable =60 pc.signalingState == "stable" ||61 (pc.signalingState == "have-local-offer" && srdAnswerPending);62 ignoreOffer = description.type == "offer" && !polite &&63 (makingOffer || !isStable);64 if (ignoreOffer) {65 return;66 }67 if (description.type == "answer")68 srdAnswerPending = true;69 await pc.setRemoteDescription(description);70 srdAnswerPending = false;71 if (description.type == "offer") {72 assert_equals(pc.signalingState, "have-remote-offer", "Remote offer");73 assert_equals(pc.remoteDescription.type, "offer", "SRD worked");74 await pc.setLocalDescription();75 assert_equals(pc.signalingState, "stable", "onmessage not racing with negotiationneeded");76 assert_equals(pc.localDescription.type, "answer", "onmessage SLD worked");77 send(other, {description: pc.localDescription});78 } else {79 assert_equals(pc.remoteDescription.type, "answer", "Answer was set");80 assert_equals(pc.signalingState, "stable", "answered");81 pc.dispatchEvent(new Event("negotiated"));82 }83 } else if (candidate) {84 try {85 await pc.addIceCandidate(candidate);86 } catch (e) {87 if (!ignoreOffer) throw e;88 }89 } else if (run) {90 send(window.parent, {[run.id]: await commands[run.cmd]() || 0});91 }92 } catch (e) {93 fail(e);94 }95 };96 } catch (e) {97 fail(e);98 }99 return pc;100}101async function setupPeerIframe(t, polite) {102 const iframe = document.createElement("iframe");103 t.add_cleanup(() => iframe.remove());104 iframe.srcdoc =105 `<html\><script\>(${peer.toString()})(window.parent, ${polite});</script\></html\>`;106 document.documentElement.appendChild(iframe);107 const failCatcher = t.step_func(({data}) =>108 ("error" in data) && assert_unreached(`Error in iframe: ${data.error}`));109 window.addEventListener("message", failCatcher);110 t.add_cleanup(() => window.removeEventListener("message", failCatcher));111 await new Promise(r => iframe.onload = r);112 return iframe;113}114function setupPeerTopLevel(t, other, polite) {115 const pc = peer(other, polite, t.step_func(e => { throw e; }));116 t.add_cleanup(() => { pc.close(); window.onmessage = null; });117}118let counter = 0;119async function run(target, cmd) {120 const id = `result${counter++}`;121 target.postMessage({run: {cmd, id}}, "*");122 return new Promise(r => window.addEventListener("message",123 function listen({data}) {124 if (!(id in data)) return;125 window.removeEventListener("message", listen);126 r(data[id]);127 }));128}129let iframe;130async function setupAB(t, politeA, politeB) {131 iframe = await setupPeerIframe(t, politeB);132 return setupPeerTopLevel(t, iframe.contentWindow, politeA);133}134const runA = cmd => run(window, cmd);135const runB = cmd => run(iframe.contentWindow, cmd);136const runBoth = (cmdA, cmdB = cmdA) => Promise.all([runA(cmdA), runB(cmdB)]);137async function promise_test_both_roles(f, name) {138 promise_test(async t => f(t, await setupAB(t, true, false)), name);139 promise_test(async t => f(t, await setupAB(t, false, true)),140 `${name} with roles reversed`);...
Using AI Code Generation
1var wpt = require('webpagetest');2var client = wpt('www.webpagetest.org');3client.getLocations(function(err, data) {4 if (err) {5 console.log('Error: ' + err);6 } else {7 console.log(data);8 }9});10}, function(err, data) {11 if (err) {12 console.log('Error: ' + err);13 } else {14 console.log(data);15 }16});17client.getTestStatus('140628_7V_58f', function(err, data) {18 if (err) {19 console.log('Error: ' + err);20 } else {21 console.log(data);22 }23});24client.getTestResults('140628_7V_58f', function(err, data) {25 if (err) {26 console.log('Error: ' + err);27 } else {28 console.log(data);29 }30});31client.getTestResults('140628_7V_58f', {32}, function(err, data) {33 if (err) {34 console.log('Error: ' + err);35 } else {36 console.log(data);37 }38});39client.getTestResults('140628_7V_58f', {40}, function(err, data) {41 if (err) {42 console.log('Error: ' + err);43 } else {44 console.log(data);45 }46});47client.getTestResults('140628_7V_58f', {48}, function(err, data) {49 if (err) {50 console.log('Error: ' + err);51 } else {52 console.log(data);53 }54});55client.getTestResults('140628_7V_58f', {56}, function(err, data) {57 if (err) {58 console.log('Error: ' + err);59 } else {60 console.log(data);61 }62});63client.getTestResults('140628
Using AI Code Generation
1var wpt = require('webpagetest');2var client = wpt('www.webpagetest.org');3var failCatcher = require('./failCatcher');4}, function(err, data) {5 if (err) {6 console.log(err);7 return;8 }9 console.log(data.data.average.firstView.TTFB);10});11test.on('error', failCatcher);
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.0d9d2e2e8b1f5c7b5e0e5c5a5f5b5c5d');3wpt.runTest('www.google.com', {firstViewOnly: true}, function(err, data) {4 if (err) {5 console.log(err);6 } else {7 wpt.getTestResults(data.data.testId, {failCatcher: true}, function(err, data) {8 if (err) {9 console.log(err);10 } else {11 console.log(data);12 }13 });14 }15});16### WebPageTest(host, apiKey)17### WebPageTest#runTest(url, options, callback)18- `location` - the location to run the test from (default: `Dulles:Chrome`)19- `runs` - the number of test runs to perform (default: `1`)20- `firstViewOnly` - if `true`, only the first view of the test will be run (default: `false`)21- `private` - if `true`, the test will not be visible on the public test log (default: `false`)22- `connectivity` - the type of connectivity to emulate (default: `Cable`)23- `bwDown` - the download bandwidth to emulate (default: `5000`)24- `bwUp` - the upload bandwidth to emulate (default: `768`)25- `latency` - the latency to emulate (default: `28`)26- `plr` - the packet loss rate to emulate (default: `0`)27- `mobile` - if `true`, the test will be run on a mobile device (default
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!!