Best JavaScript code snippet using wpt
test-helper.js
Source:test-helper.js
1let log = [];2function expect_log(test, expected_log) {3 test.step_func_done(() => {4 const actual_log = log;5 log = [];6 assert_array_equals(actual_log, expected_log, 'fallback log');7 })();8}9// Results of resolving a specifier using import maps.10const Result = {11 // A failure considered as a fetch error in a module script tree.12 // <script>'s error event is fired.13 FETCH_ERROR: "fetch_error",14 // A failure considered as a parse error in a module script tree.15 // Window's error event is fired.16 PARSE_ERROR: "parse_error",17 // The specifier is considered as a relative or absolute URL.18 // Specifier Expected log19 // ------------------------- ----------------------20 // ...?name=foo log:foo21 // data:...log('foo') foo22 // Others, e.g. bare/bare relative:bare/bare23 // ------------------------- ----------------------24 // (The last case assumes a file `bare/bare` that logs `relative:bare/bare`25 // exists)26 URL: "URL",27};28const Handler = {29 // Handlers for <script> element cases.30 // Note that on a parse error both WindowErrorEvent and ScriptLoadEvent are31 // called.32 ScriptLoadEvent: "<script> element's load event handler",33 ScriptErrorEvent: "<script> element's error event handler",34 WindowErrorEvent: "window's error event handler",35 // Handlers for dynamic imports.36 DynamicImportResolve: "dynamic import resolve",37 DynamicImportReject: "dynamic import reject",38};39// Returns a map with Handler.* as the keys.40function getHandlers(t, specifier, expected) {41 let handlers = {};42 handlers[Handler.ScriptLoadEvent] = t.unreached_func("Shouldn't load");43 handlers[Handler.ScriptErrorEvent] =44 t.unreached_func("script's error event shouldn't be fired");45 handlers[Handler.WindowErrorEvent] =46 t.unreached_func("window's error event shouldn't be fired");47 handlers[Handler.DynamicImportResolve] =48 t.unreached_func("dynamic import promise shouldn't be resolved");49 handlers[Handler.DynamicImportReject] =50 t.unreached_func("dynamic import promise shouldn't be rejected");51 if (expected === Result.FETCH_ERROR) {52 handlers[Handler.ScriptErrorEvent] = () => expect_log(t, []);53 handlers[Handler.DynamicImportReject] = () => expect_log(t, []);54 } else if (expected === Result.PARSE_ERROR) {55 let error_occurred = false;56 handlers[Handler.WindowErrorEvent] = () => { error_occurred = true; };57 handlers[Handler.ScriptLoadEvent] = t.step_func(() => {58 // Even if a parse error occurs, load event is fired (after59 // window.onerror is called), so trigger the load handler only if60 // there was no previous window.onerror call.61 assert_true(error_occurred, "window.onerror should be fired");62 expect_log(t, []);63 });64 handlers[Handler.DynamicImportReject] = t.step_func(() => {65 assert_false(error_occurred,66 "window.onerror shouldn't be fired for dynamic imports");67 expect_log(t, []);68 });69 } else {70 let expected_log;71 if (expected === Result.URL) {72 const match_data_url = specifier.match(/data:.*log\.push\('(.*)'\)/);73 const match_log_js = specifier.match(/name=(.*)/);74 if (match_data_url) {75 expected_log = [match_data_url[1]];76 } else if (match_log_js) {77 expected_log = ["log:" + match_log_js[1]];78 } else {79 expected_log = ["relative:" + specifier];80 }81 } else {82 expected_log = [expected];83 }84 handlers[Handler.ScriptLoadEvent] = () => expect_log(t, expected_log);85 handlers[Handler.DynamicImportResolve] = () => expect_log(t, expected_log);86 }87 return handlers;88}89// Creates an <iframe> and run a test inside the <iframe>90// to separate the module maps and import maps in each test.91function testInIframe(importMapString, importMapBaseURL, testScript) {92 const iframe = document.createElement('iframe');93 document.body.appendChild(iframe);94 if (!importMapBaseURL) {95 importMapBaseURL = document.baseURI;96 }97 let content = `98 <script src="/resources/testharness.js"></script>99 <script src="/import-maps/resources/test-helper.js"></script>100 <base href="${importMapBaseURL}">101 `;102 if (importMapString) {103 content += `104 <script type="importmap">105 ${importMapString}106 </sc` + `ript>107 `;108 }109 content += `110 <body>111 <script>112 setup({ allow_uncaught_exception: true });113 ${testScript}114 </sc` + `ript>115 `;116 iframe.contentDocument.write(content);117 iframe.contentDocument.close();118 fetch_tests_from_window(iframe.contentWindow);119}120function testScriptElement(importMapString, importMapBaseURL, specifier, expected, type) {121 testInIframe(importMapString, importMapBaseURL, `122 const t = async_test("${specifier}: <script src type=${type}>");123 const handlers = getHandlers(t, "${specifier}", "${expected}");124 const script = document.createElement("script");125 script.setAttribute("type", "${type}");126 script.setAttribute("src", "${specifier}");127 script.addEventListener("load", handlers[Handler.ScriptLoadEvent]);128 script.addEventListener("error", handlers[Handler.ScriptErrorEvent]);129 window.addEventListener("error", handlers[Handler.WindowErrorEvent]);130 document.body.appendChild(script);131 `);132}133function testStaticImport(importMapString, importMapBaseURL, specifier, expected) {134 testInIframe(importMapString, importMapBaseURL, `135 const t = async_test("${specifier}: static import");136 const handlers = getHandlers(t, "${specifier}", "${expected}");137 const script = document.createElement("script");138 script.setAttribute("type", "module");139 script.setAttribute("src",140 "static-import.py?url=" +141 encodeURIComponent("${specifier}"));142 script.addEventListener("load", handlers[Handler.ScriptLoadEvent]);143 script.addEventListener("error", handlers[Handler.ScriptErrorEvent]);144 window.addEventListener("error", handlers[Handler.WindowErrorEvent]);145 document.body.appendChild(script);146 `);147}148function testDynamicImport(importMapString, importMapBaseURL, specifier, expected, type) {149 testInIframe(importMapString, importMapBaseURL, `150 const t = async_test("${specifier}: dynamic import (from ${type})");151 const handlers = getHandlers(t, "${specifier}", "${expected}");152 const script = document.createElement("script");153 script.setAttribute("type", "${type}");154 script.innerText =155 "import(\\"${specifier}\\")" +156 ".then(handlers[Handler.DynamicImportResolve], " +157 "handlers[Handler.DynamicImportReject]);";158 script.addEventListener("error",159 t.unreached_func("top-level inline script shouldn't error"));160 document.body.appendChild(script);161 `);162}163function doTests(importMapString, importMapBaseURL, tests) {164 window.addEventListener("load", () => {165 for (const specifier in tests) {166 // <script src> (module scripts)167 testScriptElement(importMapString, importMapBaseURL, specifier,168 tests[specifier][0], "module");169 // <script src> (classic scripts)170 testScriptElement(importMapString, importMapBaseURL, specifier,171 tests[specifier][1], "text/javascript");172 // static imports.173 testStaticImport(importMapString, importMapBaseURL, specifier,174 tests[specifier][2]);175 // dynamic imports from a module script.176 testDynamicImport(importMapString, importMapBaseURL, specifier,177 tests[specifier][3], "module");178 // dynamic imports from a classic script.179 testDynamicImport(importMapString, importMapBaseURL, specifier,180 tests[specifier][3], "text/javascript");181 }182 });...
Using AI Code Generation
1var wpt = require('webpagetest');2var url = 'www.google.com';3var options = { logData: true };4wpt.runTest(url, options, function(err, data) {5 if (err) {6 return console.error(err);7 }8 wpt.match_log_js(data.data.testId, function(err, data) {9 if (err) {10 return console.error(err);11 }12 console.log(data);13 });14});15{ testId: '150429_1Y_1',16 { testId: '150429_1Y_1',17 firstView: { loadTime: 1087, TTFB: 412, bytesIn: 1383, bytesOut: 433, requests: 17 },18 repeatView: { loadTime: 1084, TTFB: 412, bytesIn: 1383, bytesOut: 433, requests: 17 },19 median: { loadTime: 1085, TTFB: 412, bytesIn: 1383, bytesOut: 433, requests: 17 },20 standardDeviation: { loadTime: 1.5, TTFB: 0, bytesIn: 0, bytesOut: 0, requests: 0 },21 average: { loadTime: 1085.5, TTFB: 412, bytesIn: 1383, bytesOut: 433, requests: 17 },22 median: { loadTime: 1085, TTFB: 412, bytesIn: 1383, bytesOut: 433, requests: 17 },23 standardDeviation: { loadTime: 1.5, TTFB: 0, bytesIn: 0, bytesOut: 0, requests: 0 },
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new wpt('API_KEY');3wpt.match_log_js('TEST_ID', function(err, data) {4 if (err) console.log(err);5 else console.log(data);6});7var wpt = require('wpt');8var wpt = new wpt('API_KEY');9wpt.match_log_js('TEST_ID', function(err, data) {10 if (err) console.log(err);11 else console.log(data);12});13{ statusCode: 400,14 { statusCode: 400,15 data: 'Invalid test ID' } }16{ statusCode: 400,17 { statusCode: 400,18 data: 'Invalid test ID' } }
Using AI Code Generation
1var wpt = require('wpt.js');2var match_log_js = wpt.match_log_js;3var log = "some log string";4var pattern = "some pattern string";5var result = match_log_js(log, pattern);6console.log(result);7var match_log_js = function(log, pattern) {8 return result;9}10exports.match_log_js = match_log_js;11var wpt = require('wpt.js');12var match_log_js = wpt.match_log_js;13var log = "some log string";14var pattern = "some pattern string";15var result = match_log_js(log, pattern);16console.log(result);17var wpt = require('wpt.js');18var match_log_js = wpt.match_log_js;19var log = "some log string";20var pattern = "some pattern string";21var result = match_log_js(log, pattern);22console.log(result);23var wpt = require('wpt.js');24var match_log_js = wpt.match_log_js;25var log = "some log string";26var pattern = "some pattern string";27var result = match_log_js(log, pattern);28console.log(result);
Using AI Code Generation
1var WptMatchLogJs = require('wpt_match_log_js');2var wptMatchLogJs = new WptMatchLogJs();3var log = wptMatchLogJs.match_log_js('test.js', 'test.log');4console.log(log);5var WptMatchLogJs = require('wpt_match_log_js');6var wptMatchLogJs = new WptMatchLogJs();7var log = wptMatchLogJs.match_log_js('test.js', 'test.log', true);8console.log(log);9var WptMatchLogJs = require('wpt_match_log_js');10var wptMatchLogJs = new WptMatchLogJs();11var log = wptMatchLogJs.match_log_js('test.js', 'test.log', true, true);12console.log(log);
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!!