Best JavaScript code snippet using wpt
generic-sensor-iframe-tests.sub.js
Source:generic-sensor-iframe-tests.sub.js
1function send_message_to_iframe(iframe, message, reply) {2 if (reply === undefined) {3 reply = 'success';4 }5 return new Promise((resolve, reject) => {6 window.addEventListener('message', (e) => {7 if (e.data.command !== message.command) {8 reject(`Expected reply with command '${message.command}', got '${e.data.command}' instead`);9 return;10 }11 if (e.data.result === reply) {12 resolve();13 } else {14 reject(`Got unexpected reply '${e.data.result}' to command '${message.command}', expected '${reply}'`);15 }16 }, { once: true });17 iframe.contentWindow.postMessage(message, '*');18 });19}20function run_generic_sensor_iframe_tests(sensorName) {21 const sensorType = self[sensorName];22 const featurePolicies = get_feature_policies_for_sensor(sensorName);23 sensor_test(async t => {24 assert_implements(sensorName in self, `${sensorName} is not supported.`);25 const iframe = document.createElement('iframe');26 iframe.allow = featurePolicies.join(';') + ';';27 iframe.src = 'https://{{domains[www1]}}:{{ports[https][0]}}/generic-sensor/resources/iframe_sensor_handler.html';28 // Create sensor inside cross-origin nested browsing context.29 const iframeLoadWatcher = new EventWatcher(t, iframe, 'load');30 document.body.appendChild(iframe);31 t.add_cleanup(async () => {32 await send_message_to_iframe(iframe, { command: 'reset_sensor_backend' });33 iframe.parentNode.removeChild(iframe);34 });35 await iframeLoadWatcher.wait_for('load');36 await send_message_to_iframe(iframe, {command: 'create_sensor',37 type: sensorName});38 // Focus on the main frame and test that sensor receives readings.39 window.focus();40 const sensor = new sensorType();41 const sensorWatcher = new EventWatcher(t, sensor, ['reading', 'error']);42 sensor.start();43 await sensorWatcher.wait_for('reading');44 const cachedTimeStamp = sensor.timestamp;45 // Focus on the cross-origin frame and verify that sensor reading updates in46 // the top level browsing context are suspended.47 iframe.contentWindow.focus();48 await send_message_to_iframe(iframe, {command: 'start_sensor'});49 // Focus on the main frame, verify that sensor reading updates are resumed.50 window.focus();51 await sensorWatcher.wait_for('reading');52 assert_greater_than(sensor.timestamp, cachedTimeStamp);53 sensor.stop();54 // Verify that sensor in cross-origin frame is suspended.55 await send_message_to_iframe(iframe, {command: 'is_sensor_suspended'}, true);56 }, `${sensorName}: sensor is suspended and resumed when focus traverses from\57 to cross-origin frame`);58 sensor_test(async t => {59 assert_implements(sensorName in self, `${sensorName} is not supported.`);60 const iframe = document.createElement('iframe');61 iframe.allow = featurePolicies.join(';') + ';';62 iframe.src = 'https://{{host}}:{{ports[https][0]}}/generic-sensor/resources/iframe_sensor_handler.html';63 // Create sensor inside same-origin nested browsing context.64 const iframeLoadWatcher = new EventWatcher(t, iframe, 'load');65 document.body.appendChild(iframe);66 t.add_cleanup(async () => {67 await send_message_to_iframe(iframe, { command: 'reset_sensor_backend' });68 iframe.parentNode.removeChild(iframe);69 });70 await iframeLoadWatcher.wait_for('load');71 await send_message_to_iframe(iframe, {command: 'create_sensor',72 type: sensorName});73 // Focus on main frame and test that sensor receives readings.74 window.focus();75 const sensor = new sensorType({76 // generic_sensor_mocks.js uses a default frequency of 5Hz for sensors.77 // We deliberately use a higher frequency here to make it easier to spot78 // spurious, unexpected 'reading' events caused by the main frame's79 // sensor not stopping early enough.80 // TODO(rakuco): Create a constant with the 5Hz default frequency instead81 // of using magic numbers.82 frequency: 1583 });84 const sensorWatcher = new EventWatcher(t, sensor, ['reading', 'error']);85 sensor.start();86 await sensorWatcher.wait_for('reading');87 let cachedTimeStamp = sensor.timestamp;88 // Stop sensor in main frame, so that sensorWatcher would not receive89 // readings while sensor in iframe is started. Sensors that are active and90 // belong to the same-origin context are not suspended automatically when91 // focus changes to another same-origin iframe, so if we do not explicitly92 // stop them we may receive extra 'reading' events that cause the test to93 // fail (see e.g. https://crbug.com/857520).94 sensor.stop();95 iframe.contentWindow.focus();96 await send_message_to_iframe(iframe, {command: 'start_sensor'});97 // Start sensor on main frame, verify that readings are updated.98 window.focus();99 sensor.start();100 await sensorWatcher.wait_for('reading');101 assert_greater_than(sensor.timestamp, cachedTimeStamp);102 cachedTimeStamp = sensor.timestamp;103 sensor.stop();104 // Verify that sensor in nested browsing context is not suspended.105 await send_message_to_iframe(iframe, {command: 'is_sensor_suspended'}, false);106 // Verify that sensor in top level browsing context is receiving readings.107 iframe.contentWindow.focus();108 sensor.start();109 await sensorWatcher.wait_for('reading');110 assert_greater_than(sensor.timestamp, cachedTimeStamp);111 sensor.stop();112 }, `${sensorName}: sensor is not suspended when focus traverses from\113 to same-origin frame`);114 sensor_test(async t => {115 assert_implements(sensorName in self, `${sensorName} is not supported.`);116 const iframe = document.createElement('iframe');117 iframe.allow = featurePolicies.join(';') + ';';118 iframe.src = 'https://{{host}}:{{ports[https][0]}}/generic-sensor/resources/iframe_sensor_handler.html';119 // Create sensor in the iframe (we do not care whether this is a120 // cross-origin nested context in this test).121 const iframeLoadWatcher = new EventWatcher(t, iframe, 'load');122 document.body.appendChild(iframe);123 await iframeLoadWatcher.wait_for('load');124 await send_message_to_iframe(iframe, {command: 'create_sensor',125 type: sensorName});126 iframe.contentWindow.focus();127 await send_message_to_iframe(iframe, {command: 'start_sensor'});128 // Remove iframe from main document and change focus. When focus changes,129 // we need to determine whether a sensor must have its execution suspended130 // or resumed (section 4.2.3, "Focused Area" of the Generic Sensor API131 // spec). In Blink, this involves querying a frame, which might no longer132 // exist at the time of the check.133 // Note that we cannot send the "reset_sensor_backend" command because the134 // iframe is discarded with the removeChild call.135 iframe.parentNode.removeChild(iframe);136 window.focus();137 }, `${sensorName}: losing a document's frame with an active sensor does not crash`);138 sensor_test(async t => {139 assert_implements(sensorName in self, `${sensorName} is not supported.`);140 const iframe = document.createElement('iframe');141 iframe.allow = featurePolicies.join(';') + ';';142 iframe.src = 'https://{{host}}:{{ports[https][0]}}/generic-sensor/resources/iframe_sensor_handler.html';143 // Create sensor in the iframe (we do not care whether this is a144 // cross-origin nested context in this test).145 const iframeLoadWatcher = new EventWatcher(t, iframe, 'load');146 document.body.appendChild(iframe);147 await iframeLoadWatcher.wait_for('load');148 // The purpose of this message is to initialize the mock backend in the149 // iframe. We are not going to use the sensor created there.150 await send_message_to_iframe(iframe, {command: 'create_sensor',151 type: sensorName});152 const iframeSensor = new iframe.contentWindow[sensorName]();153 assert_not_equals(iframeSensor, null);154 // Remove iframe from main document. |iframeSensor| no longer has a155 // non-null browsing context. Calling start() should probably throw an156 // error when called from a non-fully active document, but that depends on157 // https://github.com/w3c/sensors/issues/415158 iframe.parentNode.removeChild(iframe);159 iframeSensor.start();160 }, `${sensorName}: calling start() in a non-fully active document does not crash`);...
Using AI Code Generation
1var wpt = require('./wpt.js');2var options = {3 videoParams: {4 }5};6wpt.iframeSensor(options, function(err, data) {7 if (err) {8 console.log("Error: " + err);9 }10 else {11 console.log(data);12 }13});14### wpt.iframeSensor(options, callback)15MIT © [Marty Spellerberg](
Using AI Code Generation
1var iframeSensor = require('wpt').iframeSensor;2var wpt = new iframeSensor('API_KEY');3 if (err){4 console.log(err);5 }else{6 console.log(data);7 }8});9var video = require('wpt').video;10var wpt = new video('API_KEY');11 if (err){12 console.log(err);13 }else{14 console.log(data);15 }16});17var video = require('wpt').video;18var wpt = new video('API_KEY');19 if (err){20 console.log(err);21 }else{22 console.log(data);23 }24});25var video = require('wpt').video;26var wpt = new video('API_KEY');27 if (err){28 console.log(err);29 }else{30 console.log(data);31 }32});33var video = require('wpt').video;34var wpt = new video('API_KEY');35 if (err){36 console.log(err);37 }else{38 console.log(data);39 }40});41var video = require('wpt').video;42var wpt = new video('API_KEY');43 if (err){44 console.log(err);45 }else{46 console.log(data);47 }48});49var video = require('wpt').video;50var wpt = new video('API_KEY');
Using AI Code Generation
1function iframeSensor()2{3 var iframe = document.getElementById('iframe');4 var doc = iframe.contentDocument || iframe.contentWindow.document;5 var wpt = wpt || doc.wpt;6 var iframeLoaded = false;7 var iframeLoadedCount = 0;8 var iframeLoadTime = 0;9 var iframeLoadTimeStart = 0;10 var iframeStart = 0;11 var iframeStartCount = 0;12 var iframeStartFirst = 0;13 var iframeStartLast = 0;14 var iframeStartAvg = 0;15 var iframeStartMin = 0;16 var iframeStartMax = 0;17 var iframeStartInterval = 0;18 var iframeStartIntervalCount = 0;19 var iframeStartIntervalFirst = 0;20 var iframeStartIntervalLast = 0;21 var iframeStartIntervalAvg = 0;22 var iframeStartIntervalMin = 0;23 var iframeStartIntervalMax = 0;24 var iframeStartIntervalTotal = 0;25 var iframeStartIntervalTotalCount = 0;26 var iframeStartIntervalTotalFirst = 0;27 var iframeStartIntervalTotalLast = 0;28 var iframeStartIntervalTotalAvg = 0;29 var iframeStartIntervalTotalMin = 0;30 var iframeStartIntervalTotalMax = 0;31 var iframeStartIntervalTotalTotal = 0;32 var iframeStartIntervalTotalTotalCount = 0;33 var iframeStartIntervalTotalTotalFirst = 0;34 var iframeStartIntervalTotalTotalLast = 0;35 var iframeStartIntervalTotalTotalAvg = 0;36 var iframeStartIntervalTotalTotalMin = 0;37 var iframeStartIntervalTotalTotalMax = 0;38 var iframeStartIntervalTotalTotalTotal = 0;39 var iframeStartIntervalTotalTotalTotalCount = 0;
Using AI Code Generation
1var wpt = require('wpt');2var wpt = require('wpt');3 console.log('Iframe Sensor: ' + data.data);4});5 console.log('Iframe Sensor: ' + data.data);6});7var wpt = require('wpt');8var wpt = require('wpt');9 console.log('Iframe Sensor: ' + data.data);10});11 console.log('Iframe Sensor: ' + data.data);12});13var wpt = require('wpt');14var wpt = require('wpt');15 console.log('Iframe Sensor: ' + data.data);16});17 console.log('Iframe Sensor: ' + data.data);18});19var wpt = require('wpt');20var wpt = require('wpt');21 console.log('Iframe Sensor: ' + data.data);22});23 console.log('Iframe Sensor: ' + data.data);24});25var wpt = require('wpt');26var wpt = require('wpt');
Using AI Code Generation
1var wptSensor = require('wptSensor');2var options = { 3};4wptSensor.iframeSensor(options, function(err, data) {5 if (!err) {6 console.log(data);7 }8});9### wptSensor.iframeSensor(options, callback)10### wptSensor.iframeSensor(options)
Using AI Code Generation
1var iframeSensor = require('./wptSensor.js').iframeSensor;2sensor.on('load', function(data) {3 console.log(data);4});5sensor.on('error', function(err) {6 console.log(err);7});8sensor.on('complete', function(data) {9 console.log(data);10});11sensor.start();12var phantom = require('phantom');13var sitepage = null;14var phInstance = null;15phantom.create()16.then(instance => {17phInstance = instance;18return instance.createPage();19})20.then(page => {21sitepage = page;22})23.then(status => {24console.log(status);25return sitepage.property('content');26})27.then(content => {28console.log(content);29sitepage.close();30phInstance.exit();31})32.catch(error => {33console.log(error);34phInstance.exit();35});36var phantom = require('phantom');37var sitepage = null;38var phInstance = null;39phantom.create()40.then(instance => {41phInstance = instance;42return instance.createPage();43})44.then(page => {45sitepage = page;46})47.then(status => {48console.log(status);49return sitepage.property('content');50})51.then(content => {52console.log(content);53sitepage.close();54phInstance.exit();55})56.catch(error => {57console.log(error);58phInstance.exit();59});
Using AI Code Generation
1var wptHook = new WptHook();2var iframeSensor = wptHook.iframeSensor;3var iframe = document.getElementById('iframe-id');4iframe.contentWindow.postMessage('message to send', '*');5window.addEventListener('message', function(event) {6 console.log(event.data);7});8window.parent.postMessage('message to send', '*');9window.addEventListener('message', function(event) {10 console.log(event.data);11});12var parent = window.parent;13parent.postMessage('message to send', '*');14window.addEventListener('message', function(event) {15 console.log(event.data);16});17var opener = window.opener;18opener.postMessage('message to send', '*');19window.addEventListener('message', function(event) {20 console.log(event.data);21});22var opener = window.opener;23opener.postMessage('message to send', '*');24window.addEventListener('message', function(event) {25 console.log(event.data);26});27var parent = window.parent;28parent.postMessage('message to send', '*');29window.addEventListener('message', function(event) {30 console.log(event.data);31});32var top = window.top;33top.postMessage('message to send', '*');34window.addEventListener('message', function(event) {35 console.log(event.data);36});37var top = window.top;38top.postMessage('message to send', '*');39window.addEventListener('message', function(event) {40 console.log(event.data);41});42var opener = window.opener;43opener.postMessage('message to send', '*');44window.addEventListener('message', function(event) {45 console.log(event.data);46});47var top = window.top;48top.postMessage('message to send', '*');49window.addEventListener('message', function(event
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!!