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`);...
Using AI Code Generation
1var wptb_iframe = new WPTB_Iframe();2wptb_iframe.send_message_to_iframe( 'wptb_iframe', 'test', 'test' );3var wptb_iframe = new WPTB_Iframe();4wptb_iframe.on_message( function( event ) {5 console.log( event );6} );7var wptb_iframe = new WPTB_Iframe();8wptb_iframe.on_message( function( event ) {9 console.log( event );10} );11var wptb_iframe = new WPTB_Iframe();12wptb_iframe.on_message( function( event ) {13 console.log( event );14} );15var wptb_iframe = new WPTB_Iframe();16wptb_iframe.on_message( function( event ) {17 console.log( event );18} );19var wptb_iframe = new WPTB_Iframe();20wptb_iframe.on_message( function( event ) {21 console.log( event );22} );23var wptb_iframe = new WPTB_Iframe();24wptb_iframe.on_message( function( event ) {25 console.log( event );26} );27var wptb_iframe = new WPTB_Iframe();28wptb_iframe.on_message( function( event ) {29 console.log( event );30} );31var wptb_iframe = new WPTB_Iframe();32wptb_iframe.on_message( function(
Using AI Code Generation
1var iframe = document.getElementById('wpTextbox1_ifr');2var iframeWindow = iframe.contentWindow;3iframeWindow.wpTextbox1.send_message_to_iframe('insert', 'This is a test');4var iframe = document.getElementById('wpTextbox1_ifr');5var iframeWindow = iframe.contentWindow;6iframeWindow.wpTextbox1.send_message_to_parent('insert', 'This is a test');7var iframe = document.getElementById('wpTextbox1_ifr');8var iframeWindow = iframe.contentWindow;9iframeWindow.wpTextbox1.send_message_to_iframe('insert', 'This is a test');10var iframe = document.getElementById('wpTextbox1_ifr');11var iframeWindow = iframe.contentWindow;12iframeWindow.wpTextbox1.send_message_to_parent('insert', 'This is a test');
Using AI Code Generation
1Wptp.send_message_to_iframe('test', 'test');2Wptp.receive_message_from_parent(function(data) {3 console.log(data);4});5Wptp.send_message_to_parent('test', 'test');6Wptp.receive_message_from_iframe(function(data) {7 console.log(data);8});
Using AI Code Generation
1var wptools = require('wptools');2wptools.send_message_to_iframe('iframe_id', 'hello iframe');3wptools.listen_for_message_from_iframe(function(message) {4 console.log(message);5});6wptools.listen_for_message_from_parent_window(function(message) {7 console.log(message);8});9wptools.send_message_to_parent_window('hello parent window');
Using AI Code Generation
1send_message_to_iframe("iframe", "message", "hello iframe");2send_message_to_parent("message", "hello parent");3send_message_to_self("message", "hello self");4send_message_to_window("message", "hello window");5wpt_add_event_listener("message", function(event) {6});7wpt_add_event_listener("message", function(event) {8}, "iframe");9wpt_add_event_listener("message", function(event) {10}, "parent");11wpt_add_event_listener("message", function(event) {12}, "self");13wpt_add_event_listener("message", function(event) {14}, "window");
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!!