Best JavaScript code snippet using wpt
generic-sensor-tests.js
Source:generic-sensor-tests.js
...38 let sensorTest = new GenericSensorTest();39 await sensorTest.initialize();40 return sensorTest;41}42function sensor_test(func, name, properties) {43 promise_test(async (t) => {44 let sensorTest = await initialize_generic_sensor_tests();45 try {46 await func(t);47 } finally {48 await sensorTest.reset();49 };50 }, name, properties);51}52const properties = {53 'AmbientLightSensor' : ['timestamp', 'illuminance'],54 'Accelerometer' : ['timestamp', 'x', 'y', 'z'],55 'LinearAccelerationSensor' : ['timestamp', 'x', 'y', 'z'],56 "GravitySensor" : ['timestamp', 'x', 'y', 'z'],57 'Gyroscope' : ['timestamp', 'x', 'y', 'z'],58 'Magnetometer' : ['timestamp', 'x', 'y', 'z'],59 "UncalibratedMagnetometer" : ['timestamp', 'x', 'y', 'z',60 'xBias', 'yBias', 'zBias'],61 'AbsoluteOrientationSensor' : ['timestamp', 'quaternion'],62 'RelativeOrientationSensor' : ['timestamp', 'quaternion'],63 'GeolocationSensor' : ['timestamp', 'latitude', 'longitude', 'altitude',64 'accuracy', 'altitudeAccuracy', 'heading', 'speed'],65 'ProximitySensor' : ['timestamp', 'max']66};67const spatialSensors = ['Accelerometer',68 'LinearAccelerationSensor',69 'GravitySensor',70 'Gyroscope',71 'Magnetometer',72 'UncalibratedMagnetometer',73 'AbsoluteOrientationSensor',74 'RelativeOrientationSensor'];75function assert_reading_not_null(sensor) {76 for (let property in properties[sensor.constructor.name]) {77 let propertyName = properties[sensor.constructor.name][property];78 assert_not_equals(sensor[propertyName], null);79 }80}81function assert_reading_null(sensor) {82 for (let property in properties[sensor.constructor.name]) {83 let propertyName = properties[sensor.constructor.name][property];84 assert_equals(sensor[propertyName], null);85 }86}87function reading_to_array(sensor) {88 const arr = new Array();89 for (let property in properties[sensor.constructor.name]) {90 let propertyName = properties[sensor.constructor.name][property];91 arr[property] = sensor[propertyName];92 }93 return arr;94}95function runGenericSensorTests(sensorName) {96 const sensorType = self[sensorName];97 sensor_test(async t => {98 assert_true(sensorName in self);99 const sensor = new sensorType();100 const sensorWatcher = new EventWatcher(t, sensor, ["reading", "error"]);101 sensor.start();102 await sensorWatcher.wait_for("reading");103 assert_reading_not_null(sensor);104 assert_true(sensor.hasReading);105 sensor.stop();106 assert_reading_null(sensor);107 assert_false(sensor.hasReading);108 }, `${sensorName}: Test that 'onreading' is called and sensor reading is valid`);109 sensor_test(async t => {110 assert_true(sensorName in self);111 const sensor1 = new sensorType();112 const sensor2 = new sensorType();113 const sensorWatcher = new EventWatcher(t, sensor1, ["reading", "error"]);114 sensor2.start();115 sensor1.start();116 await sensorWatcher.wait_for("reading");117 // Reading values are correct for both sensors.118 assert_reading_not_null(sensor1);119 assert_reading_not_null(sensor2);120 //After first sensor stops its reading values are null,121 //reading values for the second sensor remains122 sensor1.stop();123 assert_reading_null(sensor1);124 assert_reading_not_null(sensor2);125 sensor2.stop();126 assert_reading_null(sensor2);127 }, `${sensorName}: sensor reading is correct`);128 sensor_test(async t => {129 assert_true(sensorName in self);130 const sensor = new sensorType();131 const sensorWatcher = new EventWatcher(t, sensor, ["reading", "error"]);132 sensor.start();133 await sensorWatcher.wait_for("reading");134 const cachedTimeStamp1 = sensor.timestamp;135 await sensorWatcher.wait_for("reading");136 const cachedTimeStamp2 = sensor.timestamp;137 assert_greater_than(cachedTimeStamp2, cachedTimeStamp1);138 sensor.stop();139 }, `${sensorName}: sensor timestamp is updated when time passes`);140 sensor_test(async t => {141 assert_true(sensorName in self);142 const sensor = new sensorType();143 const sensorWatcher = new EventWatcher(t, sensor, ["activate", "error"]);144 assert_false(sensor.activated);145 sensor.start();146 assert_false(sensor.activated);147 await sensorWatcher.wait_for("activate");148 assert_true(sensor.activated);149 sensor.stop();150 assert_false(sensor.activated);151 }, `${sensorName}: Test that sensor can be successfully created and its states are correct.`);152 sensor_test(async t => {153 assert_true(sensorName in self);154 const sensor = new sensorType();155 const sensorWatcher = new EventWatcher(t, sensor, ["activate", "error"]);156 const start_return = sensor.start();157 await sensorWatcher.wait_for("activate");158 assert_equals(start_return, undefined);159 sensor.stop();160 }, `${sensorName}: sensor.start() returns undefined`);161 sensor_test(async t => {162 assert_true(sensorName in self);163 const sensor = new sensorType();164 const sensorWatcher = new EventWatcher(t, sensor, ["activate", "error"]);165 sensor.start();166 sensor.start();167 await sensorWatcher.wait_for("activate");168 assert_true(sensor.activated);169 sensor.stop();170 }, `${sensorName}: no exception is thrown when calling start() on already started sensor`);171 sensor_test(async t => {172 assert_true(sensorName in self);173 const sensor = new sensorType();174 const sensorWatcher = new EventWatcher(t, sensor, ["activate", "error"]);175 sensor.start();176 await sensorWatcher.wait_for("activate");177 const stop_return = sensor.stop();178 assert_equals(stop_return, undefined);179 }, `${sensorName}: sensor.stop() returns undefined`);180 sensor_test(async t => {181 assert_true(sensorName in self);182 const sensor = new sensorType();183 const sensorWatcher = new EventWatcher(t, sensor, ["activate", "error"]);184 sensor.start();185 await sensorWatcher.wait_for("activate");186 sensor.stop();187 sensor.stop();188 assert_false(sensor.activated);189 }, `${sensorName}: no exception is thrown when calling stop() on already stopped sensor`);190 sensor_test(async t => {191 assert_true(sensorName in self);192 const sensor = new sensorType();193 const sensorWatcher = new EventWatcher(t, sensor, ["reading", "error"]);194 sensor.start();195 await sensorWatcher.wait_for("reading");196 assert_true(sensor.hasReading);197 const timestamp = sensor.timestamp;198 sensor.stop();199 assert_false(sensor.hasReading);200 sensor.start();201 await sensorWatcher.wait_for("reading");202 assert_true(sensor.hasReading);203 assert_greater_than(timestamp, 0);204 assert_greater_than(sensor.timestamp, timestamp);205 sensor.stop();206 }, `${sensorName}: Test that fresh reading is fetched on start()`);207// TBD file a WPT issue: visibilityChangeWatcher times out.208// sensor_test(async t => {209// const sensor = new sensorType();210// const sensorWatcher = new EventWatcher(t, sensor, ["reading", "error"]);211// const visibilityChangeWatcher = new EventWatcher(t, document, "visibilitychange");212// sensor.start();213// await sensorWatcher.wait_for("reading");214// assert_reading_not_null(sensor);215// const cachedSensor1 = reading_to_array(sensor);216// const win = window.open('', '_blank');217// await visibilityChangeWatcher.wait_for("visibilitychange");218// const cachedSensor2 = reading_to_array(sensor);219// win.close();220// sensor.stop();221// assert_object_equals(cachedSensor1, cachedSensor2);222// }, `${sensorName}: sensor readings can not be fired on the background tab`);223 sensor_test(async t => {224 assert_true(sensorName in self);225 const fastSensor = new sensorType({frequency: 30});226 const slowSensor = new sensorType({frequency: 5});227 slowSensor.start();228 const fastCounter = await new Promise((resolve, reject) => {229 let fastCounter = 0;230 let slowCounter = 0;231 fastSensor.onreading = () => {232 fastCounter++;233 }234 slowSensor.onreading = () => {235 slowCounter++;236 if (slowCounter == 1) {237 fastSensor.start();238 } else if (slowCounter == 3) {239 fastSensor.stop();240 slowSensor.stop();241 resolve(fastCounter);242 }243 }244 fastSensor.onerror = reject;245 slowSensor.onerror = reject;246 });247 assert_greater_than(fastCounter, 2,248 "Fast sensor overtakes the slow one");249 }, `${sensorName}: frequency hint works`);250 sensor_test(async t => {251 assert_true(sensorName in self);252 // Create a focused editbox inside a cross-origin iframe,253 // sensor notification must suspend.254 const iframeSrc = 'data:text/html;charset=utf-8,<html><body>'255 + '<input type="text" autofocus></body></html>';256 const iframe = document.createElement('iframe');257 iframe.src = encodeURI(iframeSrc);258 const sensor = new sensorType();259 const sensorWatcher = new EventWatcher(t, sensor, ["reading", "error"]);260 sensor.start();261 await sensorWatcher.wait_for("reading");262 assert_reading_not_null(sensor);263 const cachedTimestamp = sensor.timestamp;264 const cachedSensor1 = reading_to_array(sensor);265 const iframeWatcher = new EventWatcher(t, iframe, "load");266 document.body.appendChild(iframe);267 await iframeWatcher.wait_for("load");268 const cachedSensor2 = reading_to_array(sensor);269 assert_array_equals(cachedSensor1, cachedSensor2);270 iframe.remove();271 await sensorWatcher.wait_for("reading");272 const cachedSensor3 = reading_to_array(sensor);273 assert_greater_than(sensor.timestamp, cachedTimestamp);274 sensor.stop();275 }, `${sensorName}: sensor receives suspend / resume notifications when\276 cross-origin subframe is focused`);277// Re-enable after https://github.com/w3c/sensors/issues/361 is fixed.278// test(() => {279// assert_throws("NotSupportedError", () => { new sensorType({invalid: 1}) });280// assert_throws("NotSupportedError", () => { new sensorType({frequency: 60, invalid: 1}) });281// if (spatialSensors.indexOf(sensorName) == -1) {282// assert_throws("NotSupportedError", () => { new sensorType({referenceFrame: "screen"}) });283// }284// }, `${sensorName}: throw 'NotSupportedError' for an unsupported sensor option`);285 test(() => {286 assert_true(sensorName in self);287 const invalidFreqs = [288 "invalid",289 NaN,290 Infinity,291 -Infinity,292 {}293 ];294 invalidFreqs.map(freq => {295 assert_throws(new TypeError(),296 () => { new sensorType({frequency: freq}) },297 `when freq is ${freq}`);298 });299 }, `${sensorName}: throw 'TypeError' if frequency is invalid`);300 if (spatialSensors.indexOf(sensorName) == -1) {301 // The sensorType does not represent a spatial sensor.302 return;303 }304 sensor_test(async t => {305 assert_true(sensorName in self);306 const sensor = new sensorType({referenceFrame: "screen"});307 const sensorWatcher = new EventWatcher(t, sensor, ["reading", "error"]);308 sensor.start();309 await sensorWatcher.wait_for("reading");310 //TODO use mock data to verify sensor readings, blocked by issue:311 // https://github.com/web-platform-tests/wpt/issues/9686312 assert_reading_not_null(sensor);313 sensor.stop();314 }, `${sensorName}: sensor reading is correct when options.referenceFrame is 'screen'`);315 test(() => {316 assert_true(sensorName in self);317 const invalidRefFrames = [318 "invalid",...
Using AI Code Generation
1var wpt = require('wpt');2wpt.sensor_test(function(err, data) {3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9var wpt = require('wpt');10wpt.sensor_test(function(err, data) {11 if (err) {12 console.log(err);13 } else {14 console.log(data);15 }16});17var wpt = require('wpt');18wpt.sensor_test(function(err, data) {19 if (err) {20 console.log(err);21 } else {22 console.log(data);23 }24});25var wpt = require('wpt');26wpt.sensor_test(function(err, data) {27 if (err) {28 console.log(err);29 } else {30 console.log(data);31 }32});33var wpt = require('wpt');34wpt.sensor_test(function(err, data) {35 if (err) {36 console.log(err);37 } else {38 console.log(data);39 }40});41var wpt = require('wpt');42wpt.sensor_test(function(err, data) {43 if (err) {44 console.log(err);45 } else {46 console.log(data);47 }48});49var wpt = require('wpt');50wpt.sensor_test(function(err, data) {51 if (err) {52 console.log(err);53 } else {54 console.log(data);55 }56});57var wpt = require('wpt');58wpt.sensor_test(function(err, data) {59 if (err) {60 console.log(err);61 } else {62 console.log(data);63 }64});65var wpt = require('wpt');66wpt.sensor_test(function(err, data
Using AI Code Generation
1var wpt = require('wpt');2var wptSensor = new wpt();3wptSensor.sensor_test(function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10var wpt = require('wpt');11var wptSensor = new wpt();12wptSensor.sensor_test(function(err, data) {13 if (err) {14 console.log(err);15 } else {16 console.log(data);17 }18});19var wpt = require('wpt');20var wptSensor = new wpt();21wptSensor.sensor_test(function(err, data) {22 if (err) {23 console.log(err);24 } else {25 console.log(data);26 }27});28var wpt = require('wpt');29var wptSensor = new wpt();30wptSensor.sensor_test(function(err, data) {31 if (err) {32 console.log(err);33 } else {34 console.log(data);35 }36});37var wpt = require('wpt');38var wptSensor = new wpt();39wptSensor.sensor_test(function(err, data) {40 if (err) {41 console.log(err);42 } else {43 console.log(data);44 }45});46var wpt = require('wpt');47var wptSensor = new wpt();48wptSensor.sensor_test(function(err, data) {49 if (err) {50 console.log(err);51 } else {52 console.log(data);53 }54});55var wpt = require('wpt');56var wptSensor = new wpt();57wptSensor.sensor_test(function(err, data) {58 if (err) {59 console.log(err);60 } else {61 console.log(data);62 }63});64var wpt = require('w
Using AI Code Generation
1var wpt = require('wpt');2 if (err) {3 console.log(err);4 } else {5 console.log(res);6 }7});8var wpt = require('wpt');9wpt.get_locations(function (err, res) {10 if (err) {11 console.log(err);12 } else {13 console.log(res);14 }15});16var wpt = require('wpt');17wpt.get_testers("Dulles:Chrome", function (err, res) {18 if (err) {19 console.log(err);20 } else {21 console.log(res);22 }23});24var wpt = require('wpt');25wpt.get_test_status("130801_7V_1E", function (err, res) {26 if (err) {27 console.log(err);28 } else {29 console.log(res);30 }31});32var wpt = require('wpt');33wpt.get_test_results("130801_7V_1E", function (err, res) {34 if (err) {35 console.log(err);36 } else {37 console.log(res);38 }39});40var wpt = require('wpt');41wpt.get_testers("Dulles:Chrome", function (err, res) {42 if (err) {43 console.log(err);44 } else {45 console.log(res);46 }47});48var wpt = require('wpt');49wpt.get_testers("Dulles:Chrome", function (err, res) {50 if (err) {51 console.log(err);52 } else {53 console.log(res);54 }55});56var wpt = require('wpt');57wpt.get_testers("Dulles:Chrome", function (err, res) {58 if (err) {59 console.log(err);60 } else {61 console.log(res);62 }63});
Using AI Code Generation
1var wpt = require('wpt');2wpt.sensor_test(function(data) {3 console.log(data);4});5var wpt = require('wpt');6exports.sensor_test = function(callback) {7 wpt.sensor_test(function(data) {8 callback(data);9 });10};11var exec = require('child_process').exec;12exports.sensor_test = function(callback) {13 exec('sensor_test', function(err, stdout, stderr) {14 var data = JSON.parse(stdout);15 callback(data);16 });17};18var exec = require('child_process').exec;19exports.sensor_test = function(callback) {20 exec('sensor_test', function(err, stdout, stderr) {21 var data = JSON.parse(stdout);22 callback(data);23 });24};25var exec = require('child_process').exec;26exports.sensor_test = function(callback) {27 exec('sensor_test', function(err, stdout, stderr) {28 var data = JSON.parse(stdout);29 callback(data);30 });31};32var exec = require('child_process').exec;33exports.sensor_test = function(callback) {34 exec('sensor_test', function(err, stdout, stderr) {35 var data = JSON.parse(stdout);36 callback(data);37 });38};39var exec = require('child_process').exec;40exports.sensor_test = function(callback) {41 exec('sensor_test', function(err, stdout, stderr) {42 var data = JSON.parse(stdout);43 callback(data);44 });45};46var exec = require('child_process').exec;47exports.sensor_test = function(callback) {48 exec('sensor_test', function(err, stdout, stderr) {49 var data = JSON.parse(stdout);50 callback(data);51 });52};53var exec = require('child_process').exec;54exports.sensor_test = function(callback) {55 exec('sensor_test', function(err, stdout, stderr) {56 var data = JSON.parse(stdout
Using AI Code Generation
1var wpt = require('wpt');2wpt.sensor_test(function(err, data) {3 console.log(err, data);4});5module.exports = {6 sensor_test: function(callback) {7 var sensor_test = function(data) {8 callback(null, data);9 };10 var error = function(err) {11 callback(err);12 };13 exec('sensor_test', sensor_test, error);14 }15};16console.log('sensor_test');17console.log(process.argv);18process.exit(0);19public class MainActivity extends Activity {20 private static final String TAG = "MainActivity";21 private SensorManager mSensorManager;22 private Sensor mAccelerometer;23 private Sensor mGyroscope;24 private Sensor mRotationVector;25 private Sensor mLinearAcceleration;26 private Sensor mMagneticField;27 private Sensor mGravity;28 private Sensor mOrientation;29 private Sensor mStepCounter;30 private Sensor mStepDetector;31 private Sensor mSignificantMotion;32 protected void onCreate(Bundle savedInstanceState) {33 super.onCreate(savedInstanceState);34 setContentView(R.layout.activity_main);35 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);36 mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);37 mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);38 mRotationVector = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);39 mLinearAcceleration = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);40 mMagneticField = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);41 mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);42 mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);43 mStepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);44 mStepDetector = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);45 mSignificantMotion = mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);46 Log.d(TAG, "mAccelerometer: " + mAccelerometer);47 Log.d(TAG
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.4c0d7f9c9b5a5b8c0e5b5f5d5b5f5e5');3 if (err) return console.log(err);4 console.log('Test status: ' + data.statusText);5 console.log('Test ID: ' + data.data.testId);6 console.log('Test URL: ' + data.data.summary);7});8var wpt = require('webpagetest');9var wpt = new WebPageTest('www.webpagetest.org', 'A.4c0d7f9c9b5a5b8c0e5b5f5d5b5f5e5');10 if (err) return console.log(err);11 console.log('Test status: ' + data.statusText);12 console.log('Test ID: ' + data.data.testId);13 console.log('Test URL: ' + data.data.summary);14});15var wpt = require('webpagetest');16var wpt = new WebPageTest('www.webpagetest.org', 'A.4c0d7f9c9b5a5b8c0e5b5f5d5b5f5e5');17 if (err) return console.log(err);18 console.log('Test status: ' + data.statusText);19 console.log('Test ID: ' + data.data.testId);20 console.log('Test URL: ' + data.data.summary);21});22var wpt = require('webpagetest');23var wpt = new WebPageTest('www.webpagetest.org', 'A.4c0d7f9c9b5a5b8c0e5b
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!!