Best JavaScript code snippet using wpt
runner.js
Source:runner.js
...60 }61 return ret ;62 }63};64function ManifestIterator(manifest, path, test_types, use_regex) {65 this.manifest = manifest;66 this.paths = null;67 this.regex_pattern = null;68 this.test_types = test_types;69 this.test_types_index = -1;70 this.test_list = null;71 this.test_index = null;72 if (use_regex) {73 this.regex_pattern = path;74 } else {75 // Split paths by either a comma or whitespace, and ignore empty sub-strings.76 this.paths = path.split(/[,\s]+/).filter(function(s) { return s.length > 0; });77 }78}79ManifestIterator.prototype = {80 next: function() {81 var manifest_item = null;82 if (this.test_types.length === 0) {83 return null;84 }85 while (!manifest_item) {86 while (this.test_list === null || this.test_index >= this.test_list.length) {87 this.test_types_index++;88 if (this.test_types_index >= this.test_types.length) {89 return null;90 }91 this.test_index = 0;92 this.test_list = this.manifest.by_type(this.test_types[this.test_types_index]);93 }94 manifest_item = this.test_list[this.test_index++];95 while (manifest_item && !this.matches(manifest_item)) {96 manifest_item = this.test_list[this.test_index++];97 }98 if (manifest_item) {99 return this.to_test(manifest_item);100 }101 }102 },103 // Calculate the location of a match within a provided URL.104 //105 // @param {string} url - Valid URL106 //107 // @returns {null|object} - null if the URL does not satisfy the iterator's108 // filtering criteria. Otherwise, an object with109 // the following properties:110 //111 // - index - the zero-indexed offset of the start112 // of the match113 // - width - the total number of matching114 // characters115 match_location: function(url) {116 var match;117 if (this.regex_pattern) {118 match = url.match(this.regex_pattern);119 if (!match) {120 return null;121 }122 return { index: match.index, width: match[0].length };123 }124 this.paths.some(function(path) {125 if (url.indexOf(path) === 0) {126 match = path;127 return true;128 }129 return false;130 });131 if (!match) {132 return null;133 }134 return { index: 0, width: match.length };135 },136 matches: function(manifest_item) {137 return this.match_location(manifest_item.url) !== null;138 },139 to_test: function(manifest_item) {140 var test = {141 type: this.test_types[this.test_types_index],142 url: manifest_item.url143 };144 if (manifest_item.hasOwnProperty("references")) {145 test.ref_length = manifest_item.references.length;146 test.ref_type = manifest_item.references[0][1];147 test.ref_url = manifest_item.references[0][0];148 }149 return test;150 },151 count: function() {152 return this.test_types.reduce(function(prev, current) {153 var matches = this.manifest.by_type(current).filter(function(x) {154 return this.matches(x);155 }.bind(this));156 return prev + matches.length;157 }.bind(this), 0);158 }159};160function VisualOutput(elem, runner) {161 this.elem = elem;162 this.runner = runner;163 this.results_table = null;164 this.section_wrapper = null;165 this.results_table = this.elem.querySelector(".results > table");166 this.section = null;167 this.progress = this.elem.querySelector(".summary .progress");168 this.meter = this.progress.querySelector(".progress-bar");169 this.result_count = null;170 this.json_results_area = this.elem.querySelector("textarea");171 this.instructions = document.querySelector(".instructions");172 this.elem.style.display = "none";173 this.runner.manifest_wait_callbacks.push(this.on_manifest_wait.bind(this));174 this.runner.start_callbacks.push(this.on_start.bind(this));175 this.runner.result_callbacks.push(this.on_result.bind(this));176 this.runner.done_callbacks.push(this.on_done.bind(this));177 this.display_filter_state = {};178 var visual_output = this;179 var display_filter_inputs = this.elem.querySelectorAll(".result-display-filter");180 for (var i = 0; i < display_filter_inputs.length; ++i) {181 var display_filter_input = display_filter_inputs[i];182 this.display_filter_state[display_filter_input.value] = display_filter_input.checked;183 display_filter_input.addEventListener("change", function(e) {184 visual_output.apply_display_filter(e.target.value, e.target.checked);185 })186 }187}188VisualOutput.prototype = {189 clear: function() {190 this.result_count = {"PASS":0,191 "FAIL":0,192 "ERROR":0,193 "TIMEOUT":0,194 "NOTRUN":0};195 for (var p in this.result_count) {196 if (this.result_count.hasOwnProperty(p)) {197 this.elem.querySelector("td." + p).textContent = 0;198 }199 }200 if (this.json_results_area) {201 this.json_results_area.parentNode.removeChild(this.json_results_area);202 }203 this.meter.style.width = '0px';204 this.meter.textContent = '0%';205 this.meter.classList.remove("stopped", "loading-manifest");206 this.elem.querySelector(".jsonResults").style.display = "none";207 this.results_table.removeChild(this.results_table.tBodies[0]);208 this.results_table.appendChild(document.createElement("tbody"));209 },210 on_manifest_wait: function() {211 this.clear();212 this.instructions.style.display = "none";213 this.elem.style.display = "block";214 this.steady_status("loading-manifest");215 },216 on_start: function() {217 this.clear();218 this.instructions.style.display = "none";219 this.elem.style.display = "block";220 this.meter.classList.add("progress-striped", "active");221 },222 on_result: function(test, status, message, subtests) {223 var row = document.createElement("tr");224 var subtest_pass_count = subtests.reduce(function(prev, current) {225 return (current.status === "PASS") ? prev + 1 : prev;226 }, 0);227 var subtest_notrun_count = subtests.reduce(function(prev, current) {228 return (current.status === "NOTRUN") ? prev +1 : prev;229 }, 0);230 var subtests_count = subtests.length;231 var test_status;232 if (subtest_pass_count === subtests_count &&233 (status == "OK" || status == "PASS")) {234 test_status = "PASS";235 } else if ((!subtests_count && status === "NOTRUN") ||236 (subtests_count && (subtest_notrun_count == subtests_count) ) ) {237 test_status = "NOTRUN";238 } else if (subtests_count > 0 && status === "OK") {239 test_status = "FAIL";240 } else {241 test_status = status;242 }243 subtests.forEach(function(subtest) {244 if (this.result_count.hasOwnProperty(subtest.status)) {245 this.result_count[subtest.status] += 1;246 }247 }.bind(this));248 if (this.result_count.hasOwnProperty(status)) {249 this.result_count[status] += 1;250 }251 var name_node = row.appendChild(document.createElement("td"));252 name_node.appendChild(this.test_name_node(test));253 var status_node = row.appendChild(document.createElement("td"));254 status_node.textContent = test_status;255 status_node.className = test_status;256 var message_node = row.appendChild(document.createElement("td"));257 message_node.textContent = message || "";258 var subtests_node = row.appendChild(document.createElement("td"));259 if (subtests_count) {260 subtests_node.textContent = subtest_pass_count + "/" + subtests_count;261 } else {262 if (status == "PASS") {263 subtests_node.textContent = "1/1";264 } else {265 subtests_node.textContent = "0/1";266 }267 }268 var status_arr = ["PASS", "FAIL", "ERROR", "TIMEOUT", "NOTRUN"];269 for (var i = 0; i < status_arr.length; i++) {270 this.elem.querySelector("td." + status_arr[i]).textContent = this.result_count[status_arr[i]];271 }272 this.apply_display_filter_to_result_row(row, this.display_filter_state[test_status]);273 this.results_table.tBodies[0].appendChild(row);274 this.update_meter(this.runner.progress(), this.runner.results.count(), this.runner.test_count());275 },276 steady_status: function(statusName) {277 var statusTexts = {278 done: "Done!",279 stopped: "Stopped",280 "loading-manifest": "Updating and loading test manifest; this may take several minutes."281 };282 var textContent = statusTexts[statusName];283 this.meter.setAttribute("aria-valuenow", this.meter.getAttribute("aria-valuemax"));284 this.meter.style.width = "100%";285 this.meter.textContent = textContent;286 this.meter.classList.remove("progress-striped", "active", "stopped", "loading-manifest");287 this.meter.classList.add(statusName);288 this.runner.display_current_test(null);289 },290 on_done: function() {291 this.steady_status(this.runner.stop_flag ? "stopped" : "done");292 //add the json serialization of the results293 var a = this.elem.querySelector(".jsonResults");294 var json = this.runner.results.to_json();295 if (document.getElementById("dumpit").checked) {296 this.json_results_area = Array.prototype.slice.call(this.elem.querySelectorAll("textarea"));297 for(var i = 0,t = this.json_results_area.length; i < t; i++){298 this.elem.removeChild(this.json_results_area[i]);299 }300 this.json_results_area = document.createElement("textarea");301 this.json_results_area.style.width = "100%";302 this.json_results_area.setAttribute("rows", "50");303 this.elem.appendChild(this.json_results_area);304 this.json_results_area.textContent = json;305 }306 var blob = new Blob([json], { type: "application/json" });307 a.href = window.URL.createObjectURL(blob);308 a.download = "runner-results.json";309 a.textContent = "Download JSON results";310 if (!a.getAttribute("download")) a.textContent += " (right-click and save as to download)";311 a.style.display = "inline";312 },313 test_name_node: function(test) {314 if (!test.hasOwnProperty("ref_url")) {315 return this.link(test.url);316 } else {317 var wrapper = document.createElement("span");318 wrapper.appendChild(this.link(test.url));319 wrapper.appendChild(document.createTextNode(" " + test.ref_type + " "));320 wrapper.appendChild(this.link(test.ref_url));321 return wrapper;322 }323 },324 link: function(href) {325 var link = document.createElement("a");326 link.href = this.runner.server + href;327 link.textContent = href;328 return link;329 },330 update_meter: function(progress, count, total) {331 this.meter.setAttribute("aria-valuenow", count);332 this.meter.setAttribute("aria-valuemax", total);333 this.meter.textContent = this.meter.style.width = (progress * 100).toFixed(1) + "%";334 },335 apply_display_filter: function(test_status, display_state) {336 this.display_filter_state[test_status] = display_state;337 var result_cells = this.elem.querySelectorAll(".results > table tr td." + test_status);338 for (var i = 0; i < result_cells.length; ++i) {339 this.apply_display_filter_to_result_row(result_cells[i].parentNode, display_state)340 }341 },342 apply_display_filter_to_result_row: function(result_row, display_state) {343 result_row.style.display = display_state ? "" : "none";344 }345};346function ManualUI(elem, runner) {347 this.elem = elem;348 this.runner = runner;349 this.pass_button = this.elem.querySelector("button.pass");350 this.fail_button = this.elem.querySelector("button.fail");351 this.skip_button = this.elem.querySelector("button.skip");352 this.ref_buttons = this.elem.querySelector(".reftestUI");353 this.ref_type = this.ref_buttons.querySelector(".refType");354 this.ref_warning = this.elem.querySelector(".reftestWarn");355 this.test_button = this.ref_buttons.querySelector("button.test");356 this.ref_button = this.ref_buttons.querySelector("button.ref");357 this.hide();358 this.runner.test_start_callbacks.push(this.on_test_start.bind(this));359 this.runner.test_pause_callbacks.push(this.hide.bind(this));360 this.runner.done_callbacks.push(this.on_done.bind(this));361 this.pass_button.onclick = function() {362 this.disable_buttons();363 this.runner.on_result("PASS", "", []);364 }.bind(this);365 this.skip_button.onclick = function() {366 this.disable_buttons();367 this.runner.on_result("NOTRUN", "", []);368 }.bind(this);369 this.fail_button.onclick = function() {370 this.disable_buttons();371 this.runner.on_result("FAIL", "", []);372 }.bind(this);373}374ManualUI.prototype = {375 show: function() {376 this.elem.style.display = "block";377 setTimeout(this.enable_buttons.bind(this), 200);378 },379 hide: function() {380 this.elem.style.display = "none";381 },382 show_ref: function() {383 this.ref_buttons.style.display = "block";384 this.test_button.onclick = function() {385 this.runner.load(this.runner.current_test.url);386 }.bind(this);387 this.ref_button.onclick = function() {388 this.runner.load(this.runner.current_test.ref_url);389 }.bind(this);390 },391 hide_ref: function() {392 this.ref_buttons.style.display = "none";393 },394 disable_buttons: function() {395 this.pass_button.disabled = true;396 this.fail_button.disabled = true;397 },398 enable_buttons: function() {399 this.pass_button.disabled = false;400 this.fail_button.disabled = false;401 },402 on_test_start: function(test) {403 if (test.type == "manual" || test.type == "reftest") {404 this.show();405 } else {406 this.hide();407 }408 if (test.type == "reftest") {409 this.show_ref();410 this.ref_type.textContent = test.ref_type === "==" ? "equal" : "unequal";411 if (test.ref_length > 1) {412 this.ref_warning.textContent = "WARNING: only presenting first of " + test.ref_length + " references";413 this.ref_warning.style.display = "inline";414 } else {415 this.ref_warning.textContent = "";416 this.ref_warning.style.display = "none";417 }418 } else {419 this.hide_ref();420 }421 },422 on_done: function() {423 this.hide();424 }425};426function TestControl(elem, runner) {427 this.elem = elem;428 this.path_input = this.elem.querySelector(".path");429 this.path_input.addEventListener("change", function() {430 this.set_counts();431 }.bind(this), false);432 this.use_regex_input = this.elem.querySelector("#use_regex");433 this.use_regex_input.addEventListener("change", function() {434 this.set_counts();435 }.bind(this), false);436 this.pause_button = this.elem.querySelector("button.togglePause");437 this.start_button = this.elem.querySelector("button.toggleStart");438 this.type_checkboxes = Array.prototype.slice.call(439 this.elem.querySelectorAll("input[type=checkbox].test-type"));440 this.type_checkboxes.forEach(function(elem) {441 elem.addEventListener("change", function() {442 this.set_counts();443 }.bind(this),444 false);445 elem.addEventListener("click", function() {446 this.start_button.disabled = this.get_test_types().length < 1;447 }.bind(this),448 false);449 }.bind(this));450 this.timeout_input = this.elem.querySelector(".timeout_multiplier");451 this.render_checkbox = this.elem.querySelector(".render");452 this.testcount_area = this.elem.querySelector("#testcount");453 this.runner = runner;454 this.runner.done_callbacks.push(this.on_done.bind(this));455 this.set_start();456 this.set_counts();457}458TestControl.prototype = {459 set_start: function() {460 this.start_button.disabled = this.get_test_types().length < 1;461 this.pause_button.disabled = true;462 this.start_button.textContent = "Start";463 this.path_input.disabled = false;464 this.type_checkboxes.forEach(function(elem) {465 elem.disabled = false;466 });467 this.start_button.onclick = function() {468 var path = this.get_path();469 var test_types = this.get_test_types();470 var settings = this.get_testharness_settings();471 var use_regex = this.get_use_regex();472 this.runner.start(path, test_types, settings, use_regex);473 this.set_stop();474 this.set_pause();475 }.bind(this);476 },477 set_stop: function() {478 clearTimeout(this.runner.timeout);479 this.pause_button.disabled = false;480 this.start_button.textContent = "Stop";481 this.path_input.disabled = true;482 this.type_checkboxes.forEach(function(elem) {483 elem.disabled = true;484 });485 this.start_button.onclick = function() {486 this.runner.stop_flag = true;487 this.runner.done();488 }.bind(this);489 },490 set_pause: function() {491 this.pause_button.textContent = "Pause";492 this.pause_button.onclick = function() {493 this.runner.pause();494 this.set_resume();495 }.bind(this);496 },497 set_resume: function() {498 this.pause_button.textContent = "Resume";499 this.pause_button.onclick = function() {500 this.runner.unpause();501 this.set_pause();502 }.bind(this);503 },504 set_counts: function() {505 if (this.runner.manifest_loading) {506 setTimeout(function() {507 this.set_counts();508 }.bind(this), 1000);509 return;510 }511 var path = this.get_path();512 var test_types = this.get_test_types();513 var use_regex = this.get_use_regex();514 var iterator = new ManifestIterator(this.runner.manifest, path, test_types, use_regex);515 var count = iterator.count();516 this.testcount_area.textContent = count;517 },518 get_path: function() {519 return this.path_input.value;520 },521 get_test_types: function() {522 return this.type_checkboxes.filter(function(elem) {523 return elem.checked;524 }).map(function(elem) {525 return elem.value;526 });527 },528 get_testharness_settings: function() {529 return {timeout_multiplier: parseFloat(this.timeout_input.value),530 output: this.render_checkbox.checked};531 },532 get_use_regex: function() {533 return this.use_regex_input.checked;534 },535 on_done: function() {536 this.set_pause();537 this.set_start();538 }539};540function Results(runner) {541 this.test_results = null;542 this.runner = runner;543 this.runner.start_callbacks.push(this.on_start.bind(this));544}545Results.prototype = {546 on_start: function() {547 this.test_results = [];548 },549 set: function(test, status, message, subtests) {550 this.test_results.push({"test":test,551 "subtests":subtests,552 "status":status,553 "message":message});554 },555 count: function() {556 return this.test_results.length;557 },558 to_json: function() {559 var data = {560 "results": this.test_results.map(function(result) {561 var rv = {"test":(result.test.hasOwnProperty("ref_url") ?562 [result.test.url, result.test.ref_type, result.test.ref_url] :563 result.test.url),564 "subtests":result.subtests,565 "status":result.status,566 "message":result.message};567 return rv;568 })569 };570 return JSON.stringify(data, null, 2);571 }572};573function Runner(manifest_path) {574 this.server = location.protocol + "//" + location.host;575 this.manifest = new Manifest(manifest_path);576 this.path = null;577 this.test_types = null;578 this.manifest_iterator = null;579 this.test_window = null;580 this.test_div = document.getElementById('current_test');581 this.test_url = this.test_div.getElementsByTagName('a')[0];582 this.current_test = null;583 this.timeout = null;584 this.num_tests = null;585 this.pause_flag = false;586 this.stop_flag = false;587 this.done_flag = false;588 this.manifest_wait_callbacks = [];589 this.start_callbacks = [];590 this.test_start_callbacks = [];591 this.test_pause_callbacks = [];592 this.result_callbacks = [];593 this.done_callbacks = [];594 this.results = new Results(this);595 this.start_after_manifest_load = false;596 this.manifest_loading = true;597 this.manifest.load(this.manifest_loaded.bind(this));598}599Runner.prototype = {600 test_timeout: 20000, //ms601 currentTest: function() {602 return this.manifest[this.mTestCount];603 },604 ensure_test_window: function() {605 if (!this.test_window || this.test_window.location === null) {606 this.test_window = window.open("about:blank", 800, 600);607 }608 },609 manifest_loaded: function() {610 this.manifest_loading = false;611 if (this.start_after_manifest_load) {612 this.do_start();613 }614 },615 start: function(path, test_types, testharness_settings, use_regex) {616 this.pause_flag = false;617 this.stop_flag = false;618 this.done_flag = false;619 this.path = path;620 this.use_regex = use_regex;621 this.test_types = test_types;622 window.testharness_properties = testharness_settings;623 this.manifest_iterator = new ManifestIterator(this.manifest, this.path, this.test_types, this.use_regex);624 this.num_tests = null;625 this.ensure_test_window();626 if (this.manifest.data === null) {627 this.wait_for_manifest();628 } else {629 this.do_start();630 }631 },632 wait_for_manifest: function() {633 this.start_after_manifest_load = true;634 this.manifest_wait_callbacks.forEach(function(callback) {635 callback();636 });637 },...
Using AI Code Generation
1var wptb = require('wptb');2var manifestIterator = new wptb.ManifestIterator();3var callback = function (err, data) {4 if (err) {5 console.log("Error: " + err);6 } else {7 console.log("Data: " + data);8 }9};10manifestIterator.iterateManifest(callback);
Using AI Code Generation
1const wpt = require('webpagetest');2const webpagetest = new wpt('A.7c0d6c0d6c0d6c0d6c0d6c0d6c0d6c0d6');3const options = {4 lighthouseConfig: {5 settings: {6 },7 },
Using AI Code Generation
1var manifestIterator = require('./wptb-manifest.js').manifestIterator;2var manifest = require('./manifest.json');3var iterator = manifestIterator(manifest);4var testFile = iterator.next();5while(testFile.done !== true) {6 console.log(testFile.value);7 testFile = iterator.next();8}
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!!