Best JavaScript code snippet using wpt
testharness.js
Source:testharness.js
...244 *245 * A client document using testharness can use fetch_tests_from_worker() to246 * retrieve results from a worker. See apisample16.html.247 */248 function WorkerTestEnvironment() {249 this.name_counter = 0;250 this.all_loaded = true;251 this.message_list = [];252 this.message_ports = [];253 }254 WorkerTestEnvironment.prototype._dispatch = function(message) {255 this.message_list.push(message);256 for (var i = 0; i < this.message_ports.length; ++i)257 {258 this.message_ports[i].postMessage(message);259 }260 };261 // The only requirement is that port has a postMessage() method. It doesn't262 // have to be an instance of a MessagePort, and often isn't.263 WorkerTestEnvironment.prototype._add_message_port = function(port) {264 this.message_ports.push(port);265 for (var i = 0; i < this.message_list.length; ++i)266 {267 port.postMessage(this.message_list[i]);268 }269 };270 WorkerTestEnvironment.prototype.next_default_test_name = function() {271 var suffix = this.name_counter > 0 ? " " + this.name_counter : "";272 this.name_counter++;273 return "Untitled" + suffix;274 };275 WorkerTestEnvironment.prototype.on_new_harness_properties = function() {};276 WorkerTestEnvironment.prototype.on_tests_ready = function() {277 var this_obj = this;278 add_start_callback(279 function(properties) {280 this_obj._dispatch({281 type: "start",282 properties: properties,283 });284 });285 add_test_state_callback(286 function(test) {287 this_obj._dispatch({288 type: "test_state",289 test: test.structured_clone()290 });291 });292 add_result_callback(293 function(test) {294 this_obj._dispatch({295 type: "result",296 test: test.structured_clone()297 });298 });299 add_completion_callback(300 function(tests, harness_status) {301 this_obj._dispatch({302 type: "complete",303 tests: map(tests,304 function(test) {305 return test.structured_clone();306 }),307 status: harness_status.structured_clone()308 });309 });310 };311 WorkerTestEnvironment.prototype.add_on_loaded_callback = function() {};312 WorkerTestEnvironment.prototype.test_timeout = function() {313 // Tests running in a worker don't have a default timeout. I.e. all314 // worker tests behave as if settings.explicit_timeout is true.315 return null;316 };317 WorkerTestEnvironment.prototype.global_scope = function() {318 return self;319 };320 /*321 * Dedicated web workers.322 * https://html.spec.whatwg.org/multipage/workers.html#dedicatedworkerglobalscope323 *324 * This class is used as the test_environment when testharness is running325 * inside a dedicated worker.326 */327 function DedicatedWorkerTestEnvironment() {328 WorkerTestEnvironment.call(this);329 // self is an instance of DedicatedWorkerGlobalScope which exposes330 // a postMessage() method for communicating via the message channel331 // established when the worker is created.332 this._add_message_port(self);333 }334 DedicatedWorkerTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype);335 DedicatedWorkerTestEnvironment.prototype.on_tests_ready = function() {336 WorkerTestEnvironment.prototype.on_tests_ready.call(this);337 // In the absence of an onload notification, we a require dedicated338 // workers to explicitly signal when the tests are done.339 tests.wait_for_finish = true;340 };341 /*342 * Shared web workers.343 * https://html.spec.whatwg.org/multipage/workers.html#sharedworkerglobalscope344 *345 * This class is used as the test_environment when testharness is running346 * inside a shared web worker.347 */348 function SharedWorkerTestEnvironment() {349 WorkerTestEnvironment.call(this);350 var this_obj = this;351 // Shared workers receive message ports via the 'onconnect' event for352 // each connection.353 self.addEventListener("connect",354 function(message_event) {355 this_obj._add_message_port(message_event.source);356 });357 }358 SharedWorkerTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype);359 SharedWorkerTestEnvironment.prototype.on_tests_ready = function() {360 WorkerTestEnvironment.prototype.on_tests_ready.call(this);361 // In the absence of an onload notification, we a require shared362 // workers to explicitly signal when the tests are done.363 tests.wait_for_finish = true;364 };365 /*366 * Service workers.367 * http://www.w3.org/TR/service-workers/368 *369 * This class is used as the test_environment when testharness is running370 * inside a service worker.371 */372 function ServiceWorkerTestEnvironment() {373 WorkerTestEnvironment.call(this);374 this.all_loaded = false;375 this.on_loaded_callback = null;376 var this_obj = this;377 self.addEventListener("message",378 function(event) {379 if (event.data.type && event.data.type === "connect") {380 if (event.ports && event.ports[0]) {381 // If a MessageChannel was passed, then use it to382 // send results back to the main window. This383 // allows the tests to work even if the browser384 // does not fully support MessageEvent.source in385 // ServiceWorkers yet.386 this_obj._add_message_port(event.ports[0]);387 event.ports[0].start();388 } else {389 // If there is no MessageChannel, then attempt to390 // use the MessageEvent.source to send results391 // back to the main window.392 this_obj._add_message_port(event.source);393 }394 }395 });396 // The oninstall event is received after the service worker script and397 // all imported scripts have been fetched and executed. It's the398 // equivalent of an onload event for a document. All tests should have399 // been added by the time this event is received, thus it's not400 // necessary to wait until the onactivate event.401 on_event(self, "install",402 function(event) {403 this_obj.all_loaded = true;404 if (this_obj.on_loaded_callback) {405 this_obj.on_loaded_callback();406 }407 });408 }409 ServiceWorkerTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype);410 ServiceWorkerTestEnvironment.prototype.add_on_loaded_callback = function(callback) {411 if (this.all_loaded) {412 callback();413 } else {414 this.on_loaded_callback = callback;415 }416 };417 function create_test_environment() {418 if ('document' in self) {419 return new WindowTestEnvironment();420 }421 if ('DedicatedWorkerGlobalScope' in self &&422 self instanceof DedicatedWorkerGlobalScope) {423 return new DedicatedWorkerTestEnvironment();424 }425 if ('SharedWorkerGlobalScope' in self &&426 self instanceof SharedWorkerGlobalScope) {427 return new SharedWorkerTestEnvironment();428 }429 if ('ServiceWorkerGlobalScope' in self &&430 self instanceof ServiceWorkerGlobalScope) {431 return new ServiceWorkerTestEnvironment();432 }433 throw new Error("Unsupported test environment");434 }435 var test_environment = create_test_environment();436 function is_shared_worker(worker) {437 return 'SharedWorker' in self && worker instanceof SharedWorker;438 }439 function is_service_worker(worker) {440 return 'ServiceWorker' in self && worker instanceof ServiceWorker;441 }442 /*443 * API functions444 */445 function test(func, name, properties)...
Using AI Code Generation
1const { WorkerTestEnvironment } = require('wpt-runner');2const test = new WorkerTestEnvironment();3const { TestEnvironment } = require('wpt-runner');4const test = new TestEnvironment();5const { TestEnvironment } = require('wpt-runner');6const test = new TestEnvironment();7const { TestEnvironment } = require('wpt-runner');8const test = new TestEnvironment();9const { TestEnvironment } = require('wpt-runner');10const test = new TestEnvironment();11const { TestEnvironment } = require('wpt-runner');12const test = new TestEnvironment();13const { TestEnvironment } = require('wpt-runner');14const test = new TestEnvironment();15const { TestEnvironment } = require('wpt-runner');16const test = new TestEnvironment();17const { TestEnvironment } = require('wpt-runner');18const test = new TestEnvironment();19test('some test', t => {20 t.ok(true);21});22const { TestEnvironm
Using AI Code Generation
1const wpt = require('wpt-runner');2const { WorkerTestEnvironment } = wpt;3const { TestEnvironment } = require('jest-environment-node');4class CustomEnvironment extends TestEnvironment {5 constructor(config) {6 super(config);7 }8 async setup() {9 await super.setup();10 this.global.wpt = new WorkerTestEnvironment();11 }12 async teardown() {13 await super.teardown();14 }15 runScript(script) {16 return super.runScript(script);17 }18}19module.exports = CustomEnvironment;
Using AI Code Generation
1var test = async_test("Test to check the existence of ServiceWorkerRegistration.onupdatefound attribute");2test.step(function() {3 var script = 'resources/worker-basic.js';4 var scope = 'resources/scope/updatefound';5 var registration;6 var worker;7 var worker2;8 var worker3;9 var worker4;10 var worker5;11 var worker6;12 var worker7;13 var worker8;14 var worker9;15 var worker10;16 var worker11;17 var worker12;18 var worker13;19 var worker14;20 var worker15;21 var worker16;22 var worker17;23 var worker18;24 var worker19;25 var worker20;26 var worker21;27 var worker22;28 var worker23;29 var worker24;30 var worker25;31 var worker26;32 var worker27;33 var worker28;34 var worker29;35 var worker30;36 var worker31;37 var worker32;38 var worker33;39 var worker34;40 var worker35;41 var worker36;42 var worker37;43 var worker38;44 var worker39;45 var worker40;46 var worker41;47 var worker42;48 var worker43;49 var worker44;50 var worker45;51 var worker46;52 var worker47;53 var worker48;54 var worker49;55 var worker50;56 var worker51;57 var worker52;58 var worker53;59 var worker54;60 var worker55;61 var worker56;62 var worker57;63 var worker58;64 var worker59;65 var worker60;66 var worker61;67 var worker62;68 var worker63;69 var worker64;70 var worker65;71 var worker66;72 var worker67;73 var worker68;74 var worker69;75 var worker70;76 var worker71;77 var worker72;78 var worker73;79 var worker74;80 var worker75;81 var worker76;82 var worker77;83 var worker78;84 var worker79;85 var worker80;
Using AI Code Generation
1importScripts('wpt-runner.js');2const wpt = new WorkerTestEnvironment();3wpt.start();4### `new WorkerTestEnvironment()`5### `wpt.start()`6### `wpt.log(message)`7### `wpt.done()`8### `wpt.fetch(url)`9### `wpt.fetchText(url)`10### `wpt.fetchJSON(url)`11### `wpt.fetchBlob(url)`12### `wpt.fetchArrayBuffer(url)`13### `wpt.fetchData(url)`14### `wpt.fetchImage(url)`15### `wpt.fetchScript(url)`16### `wpt.fetchStylesheet(url)`
Using AI Code Generation
1import { WorkerTestEnvironment } from 'wpt-runner';2import { TestEnvironment } from 'web-platform-tests';3const testEnvironment = new WorkerTestEnvironment();4const testEnvironment2 = new WorkerTestEnvironment();5testEnvironment.add_completion_callback(() => {6 testEnvironment2.done();7});8testEnvironment2.add_completion_callback(() => {9 testEnvironment.done();10});11TestEnvironment.start();12### TestEnvironment.start()13### TestEnvironment.done()14### TestEnvironment.timeout(ms)15### TestEnvironment.add_completion_callback(callback)16### TestEnvironment.add_completion_callback(callback)17### TestEnvironment.add_result_callback(callback)18### TestEnvironment.add_result_callback(callback)19### TestEnvironment.add_start_callback(callback)20### TestEnvironment.add_start_callback(callback)21### TestEnvironment.add_end_callback(callback)
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!!