Best JavaScript code snippet using wpt
hidDevice_reports.https.window.js
Source:hidDevice_reports.https.window.js
...28 assert_true(device.opened);29 const fakeConnection = fake.getFakeConnection(guid);30 return {device, fakeConnection};31}32hid_test(async (t, fake) => {33 const {device, fakeConnection} = await addAndOpenDevice(fake);34 // Register an oninputreport listener and check that it receives a simulated35 // input report.36 fakeConnection.simulateInputReport(kReportId, kReportBytes);37 const inputReport = await oninputreport(device);38 assert_equals(inputReport.reportId, kReportId);39 compareDataViews(inputReport.data, new DataView(kReportBytes.buffer));40}, 'oninputreport event listener works');41hid_test(async (t, fake) => {42 const {device, fakeConnection} = await addAndOpenDevice(fake);43 // Simulate write() success.44 fakeConnection.queueExpectedWrite(true, kReportId, kReportBytes);45 await device.sendReport(kReportId, kReportBytes);46 fakeConnection.assertExpectationsMet();47}, 'sendReport works');48hid_test(async (t, fake) => {49 const {device, fakeConnection} = await addAndOpenDevice(fake);50 // Simulate write() failure.51 fakeConnection.queueExpectedWrite(false, kReportId, kReportBytes);52 await promise_rejects_dom(53 t, 'NotAllowedError', device.sendReport(kReportId, kReportBytes));54 fakeConnection.assertExpectationsMet();55}, 'Failed sendReport rejects');56hid_test(async (t, fake) => {57 const {device, fakeConnection} = await addAndOpenDevice(fake);58 const promise = device.forget();59 await promise_rejects_dom(60 t, 'InvalidStateError', device.sendReport(kReportId, kReportBytes));61 await promise;62}, 'sendReport rejects while device is forgetting');63hid_test(async (t, fake) => {64 const {device, fakeConnection} = await addAndOpenDevice(fake);65 await device.forget();66 await promise_rejects_dom(67 t, 'InvalidStateError', device.sendReport(kReportId, kReportBytes));68}, 'sendReport rejects when device is forgotten');69hid_test(async (t, fake) => {70 const {device, fakeConnection} = await addAndOpenDevice(fake);71 const detachedReport = new Uint8Array(7);72 detachBuffer(detachedReport.buffer);73 const emptyReport = new Uint8Array(0);74 fakeConnection.queueExpectedWrite(true, kReportId, emptyReport);75 await device.sendReport(kReportId, detachedReport);76 fakeConnection.assertExpectationsMet();77}, 'sendReport treates a detached buffer as an empty report');78hid_test(async (t, fake) => {79 const {device, fakeConnection} = await addAndOpenDevice(fake);80 // Simulate getFeatureReport() success.81 fakeConnection.queueExpectedGetFeatureReport(true, kReportId, kReportBytes);82 const featureReport = await device.receiveFeatureReport(kReportId);83 compareDataViews(featureReport, new DataView(kReportBytes.buffer));84 fakeConnection.assertExpectationsMet();85}, 'receiveFeatureReport works');86hid_test(async (t, fake) => {87 const {device, fakeConnection} = await addAndOpenDevice(fake);88 // Simulate getFeatureReport() failure.89 fakeConnection.queueExpectedGetFeatureReport(false, kReportId, kReportBytes);90 await promise_rejects_dom(91 t, 'NotAllowedError', device.receiveFeatureReport(kReportId));92 fakeConnection.assertExpectationsMet();93}, 'Failed receiveFeatureReport rejects');94hid_test(async (t, fake) => {95 const {device, fakeConnection} = await addAndOpenDevice(fake);96 const promise = device.forget();97 await promise_rejects_dom(98 t, 'InvalidStateError', device.receiveFeatureReport(kReportId));99 await promise;100}, 'receiveFeatureReport rejects while device is forgetting');101hid_test(async (t, fake) => {102 const {device, fakeConnection} = await addAndOpenDevice(fake);103 await device.forget();104 await promise_rejects_dom(105 t, 'InvalidStateError', device.receiveFeatureReport(kReportId));106}, 'receiveFeatureReport rejects when device is forgotten');107hid_test(async (t, fake) => {108 const {device, fakeConnection} = await addAndOpenDevice(fake);109 // Simulate sendFeatureReport() success.110 fakeConnection.queueExpectedSendFeatureReport(true, kReportId, kReportBytes);111 await device.sendFeatureReport(kReportId, kReportBytes);112 fakeConnection.assertExpectationsMet();113}, 'sendFeatureReport works');114hid_test(async (t, fake) => {115 const {device, fakeConnection} = await addAndOpenDevice(fake);116 // Simulate sendFeatureReport() failure.117 fakeConnection.queueExpectedSendFeatureReport(false, kReportId, kReportBytes);118 await promise_rejects_dom(119 t, 'NotAllowedError', device.sendFeatureReport(kReportId, kReportBytes));120 fakeConnection.assertExpectationsMet();121}, 'Failed sendFeatureReport rejects');122hid_test(async (t, fake) => {123 const {device, fakeConnection} = await addAndOpenDevice(fake);124 const promise = device.forget();125 await promise_rejects_dom(126 t, 'InvalidStateError',127 device.sendFeatureReport(kReportId, kReportBytes));128 await promise;129}, 'sendFeatureReport rejects while device is forgetting');130hid_test(async (t, fake) => {131 const {device, fakeConnection} = await addAndOpenDevice(fake);132 await device.forget();133 await promise_rejects_dom(134 t, 'InvalidStateError',135 device.sendFeatureReport(kReportId, kReportBytes));136}, 'sendFeatureReport rejects when device is forgotten');137hid_test(async (t, fake) => {138 const {device, fakeConnection} = await addAndOpenDevice(fake);139 const detachedReport = new Uint8Array(7);140 detachBuffer(detachedReport.buffer);141 const emptyReport = new Uint8Array(0);142 fakeConnection.queueExpectedSendFeatureReport(true, kReportId, emptyReport);143 await device.sendFeatureReport(kReportId, detachedReport);144 fakeConnection.assertExpectationsMet();...
hidDevice_forget.https.window.js
Source:hidDevice_forget.https.window.js
...5// META: script=/webhid/resources/automation.js6'use strict';7const kTestVendorId = 0x1234;8const kTestProductId = 0xabcd;9hid_test(async (t, fake) => {10 const guid = fake.addDevice(fake.makeDevice(kTestVendorId, kTestProductId));11 fake.setSelectedDevice(guid);12 await trustedClick();13 const devices = await navigator.hid.requestDevice({filters: []});14 assert_true(devices instanceof Array);15 assert_equals(devices.length, 1);16 const device = devices[0];17 assert_true(device instanceof HIDDevice);18 let devicesFirst = await navigator.hid.getDevices();19 assert_equals(devicesFirst.length, 1);20 assert_true(devicesFirst[0] instanceof HIDDevice);21 await device.forget();22 let devicesSecond = await navigator.hid.getDevices();23 assert_equals(devicesSecond.length, 0);24}, 'A forgotten device is not available in getDevices() anymore');25hid_test(async (t, fake) => {26 // Construct two HidDeviceInfo objects with the same physical device ID to27 // simulate a device with two HID interfaces.28 let device0 = fake.makeDevice(kTestVendorId, kTestProductId);29 let device1 = fake.makeDevice(kTestVendorId, kTestProductId);30 device1.physicalDeviceId = device0.physicalDeviceId;31 let key0 = fake.addDevice(device0);32 let key1 = fake.addDevice(device1);33 assert_equals(key0, key1);34 fake.setSelectedDevice(key0);35 await trustedClick();36 const devices = await navigator.hid.requestDevice({filters: []});37 assert_true(devices instanceof Array);38 assert_equals(devices.length, 2);39 const device = devices[0];40 assert_true(device instanceof HIDDevice);41 let devicesFirst = await navigator.hid.getDevices();42 assert_equals(devicesFirst.length, 2);43 assert_true(devicesFirst[0] instanceof HIDDevice);44 assert_true(devicesFirst[1] instanceof HIDDevice);45 await device.forget();46 let devicesSecond = await navigator.hid.getDevices();47 assert_equals(devicesSecond.length, 0);48}, 'Forgetting a device with multiple interfaces revokes access to all interfaces');49hid_test(async (t, fake) => {50 const guid = fake.addDevice(fake.makeDevice(kTestVendorId, kTestProductId));51 fake.setSelectedDevice(guid);52 await trustedClick();53 const devices = await navigator.hid.requestDevice({filters: []});54 assert_true(devices instanceof Array);55 assert_equals(devices.length, 1);56 const device = devices[0];57 assert_true(device instanceof HIDDevice);58 await device.forget();59 await device.forget();60}, 'Forgetting a device twice is not an error');61hid_test(async (t, fake) => {62 const guid = fake.addDevice(fake.makeDevice(kTestVendorId, kTestProductId));63 fake.setSelectedDevice(guid);64 await trustedClick();65 const devices = await navigator.hid.requestDevice({filters: []});66 assert_true(devices instanceof Array);67 assert_equals(devices.length, 1);68 const device = devices[0];69 assert_true(device instanceof HIDDevice);70 assert_false(device.opened);71 const firstRequest = device.open();72 await promise_rejects_dom(t, 'InvalidStateError', device.forget());73 await firstRequest;74}, 'Forgetting a device while it is opening is an error');75hid_test(async (t, fake) => {76 const guid = fake.addDevice(fake.makeDevice(kTestVendorId, kTestProductId));77 fake.setSelectedDevice(guid);78 await trustedClick();79 const devices = await navigator.hid.requestDevice({filters: []});80 assert_true(devices instanceof Array);81 assert_equals(devices.length, 1);82 const device = devices[0];83 assert_true(device instanceof HIDDevice);84 assert_false(device.opened);85 await device.open();86 assert_true(device.opened);87 const firstRequest = device.forget();88 await promise_rejects_dom(t, 'InvalidStateError', device.close());89 await firstRequest;90}, 'Closing a device while it is forgetting is an error');91hid_test(async (t, fake) => {92 const guid = fake.addDevice(fake.makeDevice(kTestVendorId, kTestProductId));93 fake.setSelectedDevice(guid);94 await trustedClick();95 const devices = await navigator.hid.requestDevice({filters: []});96 assert_true(devices instanceof Array);97 assert_equals(devices.length, 1);98 const device = devices[0];99 assert_true(device instanceof HIDDevice);100 assert_false(device.opened);101 await device.open();102 assert_true(device.opened);103 await device.forget();104 await promise_rejects_dom(t, 'InvalidStateError', device.close());105}, 'Closing a device when it is forgotten is an error');106hid_test(async (t, fake) => {107 const guid = fake.addDevice(fake.makeDevice(kTestVendorId, kTestProductId));108 fake.setSelectedDevice(guid);109 await trustedClick();110 const devices = await navigator.hid.requestDevice({filters: []});111 assert_true(devices instanceof Array);112 assert_equals(devices.length, 1);113 const device = devices[0];114 assert_true(device instanceof HIDDevice);115 assert_false(device.opened);116 const firstRequest = device.forget();117 await promise_rejects_dom(t, 'InvalidStateError', device.open());118 await firstRequest;119}, 'Opening a device while it is forgetting is an error');120hid_test(async (t, fake) => {121 const guid = fake.addDevice(fake.makeDevice(kTestVendorId, kTestProductId));122 fake.setSelectedDevice(guid);123 await trustedClick();124 const devices = await navigator.hid.requestDevice({filters: []});125 assert_true(devices instanceof Array);126 assert_equals(devices.length, 1);127 const device = devices[0];128 assert_true(device instanceof HIDDevice);129 assert_false(device.opened);130 await device.forget();131 await promise_rejects_dom(t, 'InvalidStateError', device.open());132}, 'Opening a device when it is forgotten is an error');133hid_test(async (t, fake) => {134 const device = fake.makeDevice(kTestVendorId, kTestProductId);135 // Disconnect device after granting access to it.136 const guid = fake.addDevice(device, /*grantPermission=*/ true);137 fake.removeDevice(guid);138 let devices = await navigator.hid.getDevices();139 assert_equals(devices.length, 1);140 // Forget device after disconnecting it.141 await devices[0].forget();142 devices = await navigator.hid.getDevices();143 assert_equals(devices.length, 0);144 // Connect device again.145 fake.addDevice(device, /*grantPermission=*/ false);146 devices = await navigator.hid.getDevices();147 assert_equals(devices.length, 0);...
Using AI Code Generation
1var wptk = require('wptoolkit');2wptk.hid_test(function(err, data) {3 if (err) {4 console.log('Error: ' + err);5 } else {6 console.log('Data: ' + data);7 }8});
Using AI Code Generation
1var wptb = require('wptb');2wptb.hid_test(function(data){3 console.log(data);4});5var wptb = require('wptb');6wptb.hid_test(function(data){7 console.log(data);8});
Using AI Code Generation
1var wptools = require('wptools');2wptools.hid_test();3var wptools = {4 hid_test: function() {5 console.log("hid_test");6 }7}8module.exports = wptools;
Using AI Code Generation
1var wpt = require('wpt');2wpt.hid_test();3exports.hid_test = function() {4}5var wpt = require('wpt');6var wptObj = new wpt();7wptObj.hid_test();8var wpt = function() {9}10wpt.prototype.hid_test = function() {11}12module.exports = wpt;13var wpt = require('wpt');14var wptObj = new wpt();15wptObj.hid_test();16var wpt = function() {17}18wpt.prototype.hid_test = function() {19}20exports = wpt;
Using AI Code Generation
1var wptbhid = require('wptbhid');2var hid = new wptbhid();3var result = hid.hid_test();4console.log(result);5var wptbhid = require('wptbhid');6var hid = new wptbhid();7var result = hid.hid_test();8console.log(result);
Using AI Code Generation
1var wpt = require('wpt');2wpt.hid_test('hid_test', function(err, data){3 if(err) console.log(err);4 else console.log(data);5});6var wpt = require('wpt');7wpt.hid_test('hid_test', function(err, data){8 if(err) console.log(err);9 else console.log(data);10});11var wpt = require('wpt');12wpt.hid_test('hid_test', function(err, data){13 if(err) console.log(err);14 else console.log(data);15});16var wpt = require('wpt');17wpt.hid_test('hid_test', function(err, data){18 if(err) console.log(err);19 else console.log(data);20});21var wpt = require('wpt');22wpt.hid_test('hid_test', function(err, data){23 if(err) console.log(err);24 else console.log(data);25});26var wpt = require('wpt');27wpt.hid_test('hid_test', function(err, data){28 if(err) console.log(err);29 else console.log(data);30});31var wpt = require('wpt');32wpt.hid_test('hid_test', function(err, data){33 if(err) console.log(err);34 else console.log(data);35});36var wpt = require('wpt');37wpt.hid_test('hid_test', function(err, data){38 if(err) console.log(err);39 else console.log(data);40});41var wpt = require('wpt');42wpt.hid_test('hid_test', function(err, data){43 if(err) console.log(err);44 else console.log(data);45});46var wpt = require('wpt');47wpt.hid_test('hid_test', function(err, data){48 if(err) console.log(err);49 else console.log(data);50});
Using AI Code Generation
1var wpt_hid = require('./wpt_hid');2var hid = new wpt_hid.HID();3var device = hid.open(0x04D8, 0x000A);4console.log(device);5hid.close(device);6hid.free();7var ref = require('ref');8var ffi = require('ffi');9var wpt_hid = ffi.Library('./wpt_hid', {10});11var HID = function() {12 this.hid_init();13};14HID.prototype.hid_init = wpt_hid.hid_init;15HID.prototype.hid_exit = wpt_hid.hid_exit;16HID.prototype.hid_open = wpt_hid.hid_open;17HID.prototype.hid_close = wpt_hid.hid_close;18HID.prototype.hid_read = wpt_hid.hid_read;19HID.prototype.hid_write = wpt_hid.hid_write;20HID.prototype.hid_set_nonblocking = wpt_hid.hid_set_nonblocking;21HID.prototype.hid_error = wpt_hid.hid_error;22HID.prototype.free = wpt_hid.hid_exit;23module.exports.HID = HID;
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!!