Best JavaScript code snippet using wpt
peerconnection.js
Source:peerconnection.js
1/**2 * @fileoverview Utility functions for tests utilizing PeerConnections3 */4/**5 * Exchanges offers and answers between two peer connections.6 *7 * pc1's offer is set as local description in pc1 and8 * remote description in pc2. After that, pc2's answer9 * is set as it's local description and remote description in pc1.10 *11 * @param {!RTCPeerConnection} pc1 The first peer connection.12 * @param {!RTCPeerConnection} pc2 The second peer connection.13 */14async function exchangeOfferAnswer(pc1, pc2) {15 await pc1.setLocalDescription(await pc1.createOffer());16 await pc2.setRemoteDescription(pc1.localDescription);17 await pc2.setLocalDescription(await pc2.createAnswer());18 await pc1.setRemoteDescription(pc2.localDescription);19}20/**21 * Sets the specified codec preference if it's included in the transceiver's22 * list of supported codecs.23 * @param {!RTCRtpTransceiver} transceiver The RTP transceiver.24 * @param {string} codecPreference The codec preference.25 */26function setTransceiverCodecPreference(transceiver, codecPreference) {27 for (const codec of RTCRtpSender.getCapabilities('video').codecs) {28 if (codec.mimeType.includes(codecPreference)) {29 transceiver.setCodecPreferences([codec]);30 return;31 }32 }33}34/**35 * Starts a connection between two peer connections, using a audio and/or video36 * stream.37 * @param {*} t Test instance.38 * @param {boolean} audio True if audio should be used.39 * @param {boolean} video True if video should be used.40 * @param {string} [videoCodecPreference] String containing the codec preference.41 * @returns an array with the two connected peer connections, the remote stream,42 * and an object containing transceivers by kind.43 */44async function startConnection(t, audio, video, videoCodecPreference) {45 const stream = await navigator.mediaDevices.getUserMedia({audio, video});46 t.add_cleanup(() => stream.getTracks().forEach(track => track.stop()));47 const pc1 = new RTCPeerConnection();48 t.add_cleanup(() => pc1.close());49 const pc2 = new RTCPeerConnection();50 t.add_cleanup(() => pc2.close());51 const transceivers = {};52 for (const track of stream.getTracks()) {53 const transceiver = pc1.addTransceiver(track, {streams: [stream]});54 transceivers[track.kind] = transceiver;55 if (videoCodecPreference && track.kind == 'video') {56 setTransceiverCodecPreference(transceiver, videoCodecPreference);57 }58 }59 for (const [local, remote] of [[pc1, pc2], [pc2, pc1]]) {60 local.addEventListener('icecandidate', ({candidate}) => {61 if (!candidate || remote.signalingState == 'closed') return;62 remote.addIceCandidate(candidate);63 });64 }65 const haveTrackEvent = new Promise(r => pc2.ontrack = r);66 await exchangeOfferAnswer(pc1, pc2);67 const {streams} = await haveTrackEvent;68 return [pc1, pc2, streams[0], transceivers];69}70/**71 * Given a peer connection, return after at least numFramesOrPackets72 * frames (video) or packets (audio) have been received.73 * @param {*} t Test instance.74 * @param {!RTCPeerConnection} pc The peer connection.75 * @param {boolean} lookForAudio True if audio packets should be waited for.76 * @param {boolean} lookForVideo True if video packets should be waited for.77 * @param {int} numFramesOrPackets Number of frames (video) and packets (audio)78 * to wait for.79 */80async function waitForReceivedFramesOrPackets(81 t, pc, lookForAudio, lookForVideo, numFramesOrPackets) {82 let initialAudioPackets = 0;83 let initialVideoFrames = 0;84 while (lookForAudio || lookForVideo) {85 const report = await pc.getStats();86 for (const stats of report.values()) {87 if (stats.type == 'inbound-rtp') {88 if (lookForAudio && stats.kind == 'audio') {89 if (!initialAudioPackets) {90 initialAudioPackets = stats.packetsReceived;91 } else if (stats.packetsReceived > initialAudioPackets +92 numFramesOrPackets) {93 lookForAudio = false;94 }95 }96 if (lookForVideo && stats.kind == 'video') {97 if (!initialVideoFrames) {98 initialVideoFrames = stats.framesDecoded;99 } else if (stats.framesDecoded > initialVideoFrames +100 numFramesOrPackets) {101 lookForVideo = false;102 }103 }104 }105 }106 await new Promise(r => t.step_timeout(r, 100));107 }108}109/**110 * Given a peer connection, return after one of its inbound RTP connections111 * includes use of the specified codec.112 * @param {*} t Test instance.113 * @param {!RTCPeerConnection} pc The peer connection.114 * @param {string} codecToLookFor The waited-for codec.115 */116async function waitForReceivedCodec(t, pc, codecToLookFor) {117 let currentCodecId;118 for (;;) {119 const report = await pc.getStats();120 for (const stats of report.values()) {121 if (stats.type == 'inbound-rtp' && stats.kind == 'video') {122 if (stats.codecId) {123 if (report.get(stats.codecId).mimeType.toLowerCase()124 .includes(codecToLookFor.toLowerCase())) {125 return;126 }127 }128 }129 }130 await new Promise(r => t.step_timeout(r, 100));131 }...
Using AI Code Generation
1var wptAgent = require('wpt-agent');2wptAgent.haveTrackEvent();3wptAgent.trackEvent('eventName', 'eventValue');4var wptAgent = require('wpt-agent');5wptAgent.haveTrackEvent(function (err, data) {6 if (err) {7 console.log('error in haveTrackEvent');8 }9 if (data) {10 console.log('trackEvent is available');11 }12});13wptAgent.trackEvent('eventName', 'eventValue', function (err, data) {14 if (err) {15 console.log('error in trackEvent');16 }17 if (data) {18 console.log('trackEvent is executed');19 }20});21var wptAgent = require('wpt-agent');22wptAgent.haveTrackEvent(function (err, data) {23 if (err) {24 console.log('error in haveTrackEvent');25 }26 if (data) {27 console.log('trackEvent is available');28 wptAgent.trackEvent('eventName', 'eventValue', function (err, data) {29 if (err) {30 console.log('error in trackEvent');31 }32 if (data) {33 console.log('trackEvent is executed');34 }35 });36 }37});38var wptAgent = require('wpt-agent');39wptAgent.haveTrackEvent(function (err, data) {40 if (err) {41 console.log('error in haveTrackEvent');42 }43 if (data) {44 console.log('trackEvent is available');45 wptAgent.trackEvent('eventName', 'eventValue');46 }47});48var wptAgent = require('wpt-agent');49wptAgent.haveTrackEvent(function (err, data) {50 if (err) {51 console.log('error in haveTrackEvent');52 }53 if (data) {54 console.log('trackEvent is available');55 wptAgent.trackEvent('eventName', 'eventValue');56 wptAgent.trackEvent('eventName2', 'eventValue2');57 }58});59var wptAgent = require('wpt-agent');60wptAgent.haveTrackEvent(function (err, data) {61 if (err) {62 console.log('
Using AI Code Generation
1var wpt = require('wpt-api');2var wpt = new wpt('your api key');3var options = {4};5wpt.runTest(options, function(err, data) {6 if (err) {7 console.log('Error: ', err);8 } else {9 console.log('Test ID: ', data.data.testId);10 console.log('Test Status from wpt: ', data.data.statusText);11 console.log('Test Status from wpt-api: ', data.status);12 console.log('Test Status from wpt-api: ', data.statusText);13 console.log('Test Status from wpt-api: ', data.statusCode);14 console.log('Test Status from wpt-api: ', data.data);15 }16});17wpt.haveTrackEvent('Test ID', function(err, data) {18 if (err) {19 console.log('Error: ', err);20 } else {21 console.log('Test Status from wpt: ', data.data.statusText);22 console.log('Test Status from wpt-api: ', data.status);23 console.log('Test Status from wpt-api: ', data.statusText);24 console.log('Test Status from wpt-api: ', data.statusCode);25 console.log('Test Status from wpt-api: ', data.data);26 }27});28wpt.haveVideo('Test ID', function(err, data) {29 if (err) {30 console.log('Error: ', err);31 } else {32 console.log('Test Status from wpt: ', data.data.statusText);33 console.log('Test Status from wpt-api: ', data.status);34 console.log('Test Status from wpt-api: ', data.statusText);35 console.log('Test Status from wpt-api: ', data.statusCode);36 console.log('Test Status from wpt-api: ', data.data);37 }38});39wpt.haveScreenshots('Test ID', function(err, data) {40 if (err) {41 console.log('Error: ', err);42 } else {43 console.log('Test Status from wpt: ', data.data.statusText);44 console.log('Test Status from wpt-api: ', data.status);45 console.log('Test Status from wpt-api: ', data.statusText);46 console.log('Test Status from wpt-api:
Using AI Code Generation
1wpt.haveTrackEvent('video', 'play', function (err, result) {2});3wpt.haveTrackEvent('video', 'pause', function (err, result) {4});5wpt.haveTrackEvent('video', 'ended', function (err, result) {6});7wpt.haveTrackEvent('video', 'seeking', function (err, result) {8});9wpt.haveTrackEvent('video', 'seeked', function (err, result) {10});11wpt.haveTrackEvent('video', 'timeupdate', function (err, result) {12});13wpt.haveTrackEvent('video', 'volumechange', function (err, result) {14});15wpt.haveTrackEvent('video', 'ratechange', function (err, result) {16});17wpt.haveTrackEvent('video', 'stalled', function (err, result) {18});19wpt.haveTrackEvent('video', 'suspend', function (err, result) {20});21wpt.haveTrackEvent('video', 'resize', function (err, result) {22});
Using AI Code Generation
1wpt.haveTrackEvent("testEvent", function (result) {2 console.log("result is " + result);3});4wpt.haveTrackEvent("testEvent", function (result) {5 console.log("result is " + result);6});7wpt.haveTrackEvent("testEvent", function (result) {8 console.log("result is " + result);9});10wpt.haveTrackEvent("testEvent", function (result) {11 console.log("result is " + result);12});13wpt.haveTrackEvent("testEvent", function (result) {14 console.log("result is " + result);15});16wpt.haveTrackEvent("testEvent", function (result) {17 console.log("result is " + result);18});19wpt.haveTrackEvent("testEvent", function (result) {20 console.log("result is " + result);21});22wpt.haveTrackEvent("testEvent", function (result) {23 console.log("result is " + result);24});25wpt.haveTrackEvent("testEvent", function (result) {26 console.log("result is " + result);27});28wpt.haveTrackEvent("testEvent", function (result) {29 console.log("result is " + result);30});31wpt.haveTrackEvent("testEvent", function (result) {32 console.log("result is " + result);33});34wpt.haveTrackEvent("testEvent", function (result) {35 console.log("result is " + result);36});37wpt.haveTrackEvent("testEvent", function (result) {38 console.log("result is " + result);39});40wpt.haveTrackEvent("testEvent", function (result) {
Using AI Code Generation
1wpt.haveTrackEvent('load', 5)2 .then(function(result){3 console.log(result);4 });5wpt.haveTrackEvent('load', 5)6 .then(function(result){7 console.log(result);8 });9wpt.haveTrackEvent('load', 5)10 .then(function(result){11 console.log(result);12 });13wpt.haveTrackEvent('load', 5)14 .then(function(result){15 console.log(result);16 });17wpt.haveTrackEvent('load', 5)18 .then(function(result){19 console.log(result);20 });21wpt.haveTrackEvent('load', 5)22 .then(function(result){23 console.log(result);24 });
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!!