Best JavaScript code snippet using wpt
generic-sensor-tests.js
Source:generic-sensor-tests.js
1let unreached = event => {2 assert_unreached(event.error.name + ":" + event.error.message);3};4function runGenericSensorTests(sensorType, verifyReading, readingToArray) {5 async_test(t => {6 let sensor = new sensorType();7 sensor.onchange = t.step_func_done(() => {8 assert_true(verifyReading(sensor));9 sensor.stop();10 assert_true(verifyReading(sensor, false));11 });12 sensor.onerror = t.step_func_done(unreached);13 sensor.start();14 }, "Test that 'onchange' is called and sensor reading is valid");15 async_test(t => {16 let sensor1 = new sensorType();17 let sensor2 = new sensorType();18 sensor1.onactivate = t.step_func_done(() => {19 // Reading values are correct for both sensors.20 assert_true(verifyReading(sensor1));21 assert_true(verifyReading(sensor2));22 //After first sensor stops its reading values are null,23 //reading values for the second sensor remains24 sensor1.stop();25 assert_true(verifyReading(sensor1, false));26 assert_true(verifyReading(sensor2));27 sensor2.stop();28 assert_true(verifyReading(sensor2, false));29 });30 sensor1.onerror = t.step_func_done(unreached);31 sensor2.onerror = t.step_func_done(unreached);32 sensor1.start();33 sensor2.start();34 }, "sensor reading is correct");35 async_test(t => {36 let sensor = new sensorType();37 let cachedTimeStamp1;38 sensor.onactivate = () => {39 cachedTimeStamp1 = sensor.timestamp;40 };41 sensor.onerror = t.step_func_done(unreached);42 sensor.start();43 t.step_timeout(() => {44 sensor.onchange = t.step_func_done(() => {45 //sensor.timestamp changes.46 let cachedTimeStamp2 = sensor.timestamp;47 assert_greater_than(cachedTimeStamp2, cachedTimeStamp1);48 sensor.stop();49 });50 }, 1000);51 }, "sensor timestamp is updated when time passes");52 async_test(t => {53 window.onmessage = t.step_func(e => {54 assert_equals(e.data, "SecurityError");55 t.done();56 });57 }, "throw a 'SecurityError' when firing sensor readings within iframes");58 async_test(t => {59 let sensor = new sensorType();60 sensor.onactivate = t.step_func(() => {61 assert_true(verifyReading(sensor));62 let cachedSensor1 = readingToArray(sensor);63 let win = window.open('', '_blank');64 t.step_timeout(() => {65 let cachedSensor2 = readingToArray(sensor);66 win.close();67 sensor.stop();68 assert_array_equals(cachedSensor1, cachedSensor2);69 t.done();70 }, 1000);71 });72 sensor.onerror = t.step_func_done(unreached);73 sensor.start();74 }, "sensor readings can not be fired on the background tab");75 async_test(t => {76 let sensor = new sensorType();77 sensor.onerror = t.step_func_done(unreached);78 assert_false(sensor.activated);79 sensor.onchange = t.step_func_done(() => {80 assert_true(sensor.activated);81 sensor.stop();82 assert_false(sensor.activated);83 });84 sensor.start();85 assert_false(sensor.activated);86 }, "Test that sensor can be successfully created and its states are correct.");87 test(() => {88 let sensor, start_return;89 sensor = new sensorType();90 sensor.onerror = unreached;91 start_return = sensor.start();92 assert_equals(start_return, undefined);93 sensor.stop();94 }, "sensor.start() returns undefined");95 test(() => {96 try {97 let sensor = new sensorType();98 sensor.onerror = unreached;99 sensor.start();100 sensor.start();101 assert_false(sensor.activated);102 sensor.stop();103 } catch (e) {104 assert_unreached(e.name + ": " + e.message);105 }106 }, "no exception is thrown when calling start() on already started sensor");107 test(() => {108 let sensor, stop_return;109 sensor = new sensorType();110 sensor.onerror = unreached;111 sensor.start();112 stop_return = sensor.stop();113 assert_equals(stop_return, undefined);114 }, "sensor.stop() returns undefined");115 test(() => {116 try {117 let sensor = new sensorType();118 sensor.onerror = unreached;119 sensor.start();120 sensor.stop();121 sensor.stop();122 assert_false(sensor.activated);123 } catch (e) {124 assert_unreached(e.name + ": " + e.message);125 }126 }, "no exception is thrown when calling stop() on already stopped sensor");127}128function runGenericSensorInsecureContext(sensorType) {129 test(() => {130 assert_throws('SecurityError', () => { new sensorType(); });131 }, "throw a 'SecurityError' when construct sensor in an insecure context");132}133function runGenericSensorOnerror(sensorType) {134 async_test(t => {135 let sensor = new sensorType();136 sensor.onactivate = t.step_func_done(assert_unreached);137 sensor.onerror = t.step_func_done(event => {138 assert_false(sensor.activated);139 assert_equals(event.error.name, 'NotReadableError');140 });141 sensor.start();142 }, "'onerror' event is fired when sensor is not supported");143}144function runSensorFrequency(sensorType) {145 async_test(t => {146 let fastSensor = new sensorType({frequency: 30});147 let slowSensor = new sensorType({frequency: 9});148 let fastSensorNumber = 0;149 let slowSensorNumber = 0;150 fastSensor.onchange = () => {151 fastSensorNumber++;152 };153 slowSensor.onchange = t.step_func(() => {154 slowSensorNumber++;155 if (slowSensorNumber == 1) {156 fastSensor.start();157 } else if (slowSensorNumber == 2) {158 assert_equals(fastSensorNumber, 3);159 fastSensor.stop();160 slowSensor.stop();161 t.done();162 }163 });164 fastSensor.onerror = t.step_func_done(unreached);165 slowSensor.onerror = t.step_func_done(unreached);166 slowSensor.start();167 }, "Test that the frequency hint is correct");168 async_test(t => {169 let sensor = new sensorType({frequency: 600});170 let number = 0;171 sensor.onchange = () => {172 number++;173 };174 sensor.onerror = t.step_func_done(unreached);175 sensor.start();176 t.step_timeout(() => {177 assert_less_than_equal(number, 60);178 sensor.stop();179 t.done();180 }, 1000);181 }, "frequency is capped to 60.0 Hz");...
Using AI Code Generation
1var wpt = require('wpt');2 if (err) return console.log(err);3 console.log('Test status: ' + data.statusCode);4 console.log('Test ID: ' + data.data.testId);5 console.log('Test URL: ' + data.data.userUrl);6 console.log('Test results: ' + data.data.summary);7});8wpt.getTestStatus('130805_7V_1C', function(err, data) {9 if (err) return console.log(err);10 console.log('Test status: ' + data.statusCode);11 console.log('Test ID: ' + data.data.testId);12 console.log('Test URL: ' + data.data.userUrl);13 console.log('Test results: ' + data.data.summary);14});15wpt.getTestResults('130805_7V_1C', function(err, data) {16 if (err) return console.log(err);17 console.log('Test status: ' + data.statusCode);18 console.log('Test ID: ' + data.data.testId);19 console.log('Test URL: ' + data.data.userUrl);20 console.log('Test results: ' + data.data.summary);21});22wpt.getLocations(function(err, data) {23 if (err) return console.log(err);24 console.log('Test status: ' + data.statusCode);25 console.log('Test ID: ' + data.data.testId);26 console.log('Test URL: ' + data.data.userUrl);27 console.log('Test results: ' + data.data.summary);28});29wpt.getTesters(function(err, data) {30 if (err) return console.log(err);31 console.log('Test status: ' + data.statusCode);32 console.log('Test ID: ' + data.data.testId);33 console.log('Test URL: ' + data.data.userUrl);34 console.log('Test results: ' + data.data.summary);35});36wpt.getTestHistory('www.example.com', function(err, data) {37 if (err) return console.log(err);38 console.log('Test status: ' + data.statusCode);39 console.log('Test ID: ' + data.data.testId);40 console.log('Test URL: ' + data.data.userUrl);41 console.log('Test
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new wpt('API_Key');3wpt.slowSensor('Test_ID', function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10var wpt = require('wpt');11var wpt = new wpt('API_Key');12wpt.getLocations(function(err, data) {13 if (err) {14 console.log(err);15 } else {16 console.log(data);17 }18});19var wpt = require('wpt');20var wpt = new wpt('API_Key');21wpt.getTesters(function(err, data) {22 if (err) {23 console.log(err);24 } else {25 console.log(data);26 }27});28var wpt = require('wpt');29var wpt = new wpt('API_Key');30wpt.getLocations(function(err, data) {
Using AI Code Generation
1var wpt = require('wpt');2var sensor = new wpt.slowSensor();3sensor.on('data', function(data) {4 console.log(data);5});6sensor.start();7* `data` {Object} - The data object8* `error` {Object} - The error object9#### slowSensor.start()10#### slowSensor.stop()
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!!