Best JavaScript code snippet using wpt
sriharness.js
Source:sriharness.js
1var SRIScriptTest = function(pass, name, src, integrityValue, crossoriginValue, nonce) {2 this.pass = pass;3 this.name = "Script: " + name;4 this.src = src;5 this.integrityValue = integrityValue;6 this.crossoriginValue = crossoriginValue;7 this.nonce = nonce;8}9SRIScriptTest.prototype.execute = function() {10 var test = async_test(this.name);11 var e = document.createElement("script");12 e.src = this.src;13 e.setAttribute("integrity", this.integrityValue);14 if(this.crossoriginValue) {15 e.setAttribute("crossorigin", this.crossoriginValue);16 }17 if(this.nonce) {18 e.setAttribute("nonce", this.nonce);19 }20 if(this.pass) {21 e.addEventListener("load", function() {test.done()});22 e.addEventListener("error", function() {23 test.step(function(){ assert_unreached("Good load fired error handler.") })24 });25 } else {26 e.addEventListener("load", function() {27 test.step(function() { assert_unreached("Bad load succeeded.") })28 });29 e.addEventListener("error", function() {test.done()});30 }31 document.body.appendChild(e);32};33function set_extra_attributes(element, attrs) {34 // Apply the rest of the attributes, if any.35 for (const [attr_name, attr_val] of Object.entries(attrs)) {36 element[attr_name] = attr_val;37 }38}39function buildElementFromDestination(resource_url, destination, attrs) {40 // Assert: |destination| is a valid destination.41 let element;42 // The below switch is responsible for:43 // 1. Creating the correct subresource element44 // 2. Setting said element's href, src, or fetch-instigating property45 // appropriately.46 switch (destination) {47 case "script":48 element = document.createElement(destination);49 set_extra_attributes(element, attrs);50 element.src = resource_url;51 break;52 case "style":53 element = document.createElement('link');54 set_extra_attributes(element, attrs);55 element.rel = 'stylesheet';56 element.href = resource_url;57 break;58 case "image":59 element = document.createElement('img');60 set_extra_attributes(element, attrs);61 element.src = resource_url;62 break;63 default:64 assert_unreached("INVALID DESTINATION");65 }66 return element;67}68// When using SRIPreloadTest, also include /preload/resources/preload_helper.js69// |number_of_requests| is used to ensure that preload requests are actually70// reused as expected.71const SRIPreloadTest = (preload_sri_success, subresource_sri_success, name,72 number_of_requests, destination, resource_url,73 link_attrs, subresource_attrs) => {74 const test = async_test(name);75 const link = document.createElement('link');76 // Early-fail in UAs that do not support `preload` links.77 test.step_func(() => {78 assert_true(link.relList.supports('preload'),79 "This test is automatically failing because the browser does not" +80 "support `preload` links.");81 })();82 // Build up the link.83 link.rel = 'preload';84 link.as = destination;85 link.href = resource_url;86 for (const [attr_name, attr_val] of Object.entries(link_attrs)) {87 link[attr_name] = attr_val; // This may override `rel` to modulepreload.88 }89 // Preload + subresource success and failure loading functions.90 const valid_preload_failed = test.step_func(() =>91 { assert_unreached("Valid preload fired error handler.") });92 const invalid_preload_succeeded = test.step_func(() =>93 { assert_unreached("Invalid preload load succeeded.") });94 const valid_subresource_failed = test.step_func(() =>95 { assert_unreached("Valid subresource fired error handler.") });96 const invalid_subresource_succeeded = test.step_func(() =>97 { assert_unreached("Invalid subresource load succeeded.") });98 const subresource_pass = test.step_func(() => {99 verifyNumberOfResourceTimingEntries(resource_url, number_of_requests);100 test.done();101 });102 const preload_pass = test.step_func(() => {103 const subresource_element = buildElementFromDestination(104 resource_url,105 destination,106 subresource_attrs107 );108 if (subresource_sri_success) {109 subresource_element.onload = subresource_pass;110 subresource_element.onerror = valid_subresource_failed;111 } else {112 subresource_element.onload = invalid_subresource_succeeded;113 subresource_element.onerror = subresource_pass;114 }115 document.body.append(subresource_element);116 });117 if (preload_sri_success) {118 link.onload = preload_pass;119 link.onerror = valid_preload_failed;120 } else {121 link.onload = invalid_preload_succeeded;122 link.onerror = preload_pass;123 }124 document.head.append(link);125}126// <link> tests127// Style tests must be done synchronously because they rely on the presence128// and absence of global style, which can affect later tests. Thus, instead129// of executing them one at a time, the style tests are implemented as a130// queue that builds up a list of tests, and then executes them one at a131// time.132var SRIStyleTest = function(queue, pass, name, attrs, customCallback, altPassValue) {133 this.pass = pass;134 this.name = "Style: " + name;135 this.customCallback = customCallback || function () {};136 this.attrs = attrs || {};137 this.passValue = altPassValue || "rgb(255, 255, 0)";138 this.test = async_test(this.name);139 this.queue = queue;140 this.queue.push(this);141}142SRIStyleTest.prototype.execute = function() {143 var that = this;144 var container = document.getElementById("container");145 while (container.hasChildNodes()) {146 container.removeChild(container.firstChild);147 }148 var test = this.test;149 var div = document.createElement("div");150 div.className = "testdiv";151 var e = document.createElement("link");152 // The link relation is guaranteed to not be "preload" or "modulepreload".153 this.attrs.rel = this.attrs.rel || "stylesheet";154 for (var key in this.attrs) {155 if (this.attrs.hasOwnProperty(key)) {156 e.setAttribute(key, this.attrs[key]);157 }158 }159 if(this.pass) {160 e.addEventListener("load", function() {161 test.step(function() {162 var background = window.getComputedStyle(div, null).getPropertyValue("background-color");163 assert_equals(background, that.passValue);164 test.done();165 });166 });167 e.addEventListener("error", function() {168 test.step(function(){ assert_unreached("Good load fired error handler.") })169 });170 } else {171 e.addEventListener("load", function() {172 test.step(function() { assert_unreached("Bad load succeeded.") })173 });174 e.addEventListener("error", function() {175 test.step(function() {176 var background = window.getComputedStyle(div, null).getPropertyValue("background-color");177 assert_not_equals(background, that.passValue);178 test.done();179 });180 });181 }182 container.appendChild(div);183 container.appendChild(e);184 this.customCallback(e, container);...
aflprep_sriharness.js
Source:aflprep_sriharness.js
1var SRIScriptTest = function(pass, name, src, integrityValue, crossoriginValue, nonce) {2 this.pass = pass;3 this.name = "Script: " + name;4 this.src = src;5 this.integrityValue = integrityValue;6 this.crossoriginValue = crossoriginValue;7 this.nonce = nonce;8}9SRIScriptTest.prototype.execute = function() {10 var test = async_test(this.name);11 var e = document.createElement("script");12 e.src = this.src;13 e.setAttribute("integrity", this.integrityValue);14 if(this.crossoriginValue) {15 e.setAttribute("crossorigin", this.crossoriginValue);16 }17 if(this.nonce) {18 e.setAttribute("nonce", this.nonce);19 }20 if(this.pass) {21 e.addEventListener("load", function() {test.done()});22 e.addEventListener("error", function() {23 test.step(function(){ assert_unreached("Good load fired error handler.") })24 });25 } else {26 e.addEventListener("load", function() {27 test.step(function() { assert_unreached("Bad load succeeded.") })28 });29 e.addEventListener("error", function() {test.done()});30 }31 document.body.appendChild(e);32};33function set_extra_attributes(element, attrs) {34 for (const [attr_name, attr_val] of Object.entries(attrs)) {35 element[attr_name] = attr_val;36 }37}38function buildElementFromDestination(resource_url, destination, attrs) {39 let element;40 switch (destination) {41 case "script":42 element = document.createElement(destination);43 set_extra_attributes(element, attrs);44 element.src = resource_url;45 break;46 case "style":47 element = document.createElement('link');48 set_extra_attributes(element, attrs);49 element.rel = 'stylesheet';50 element.href = resource_url;51 break;52 case "image":53 element = document.createElement('img');54 set_extra_attributes(element, attrs);55 element.src = resource_url;56 break;57 default:58 assert_unreached("INVALID DESTINATION");59 }60 return element;61}62const SRIPreloadTest = (preload_sri_success, subresource_sri_success, name,63 number_of_requests, destination, resource_url,64 link_attrs, subresource_attrs) => {65 const test = async_test(name);66 const link = document.createElement('link');67 test.step_func(() => {68 assert_true(link.relList.supports('preload'),69 "This test is automatically failing because the browser does not" +70 "support `preload` links.");71 })();72 link.rel = 'preload';73 link.as = destination;74 link.href = resource_url;75 for (const [attr_name, attr_val] of Object.entries(link_attrs)) {76 }77 const valid_preload_failed = test.step_func(() =>78 { assert_unreached("Valid preload fired error handler.") });79 const invalid_preload_succeeded = test.step_func(() =>80 { assert_unreached("Invalid preload load succeeded.") });81 const valid_subresource_failed = test.step_func(() =>82 { assert_unreached("Valid subresource fired error handler.") });83 const invalid_subresource_succeeded = test.step_func(() =>84 { assert_unreached("Invalid subresource load succeeded.") });85 const subresource_pass = test.step_func(() => {86 verifyNumberOfResourceTimingEntries(resource_url, number_of_requests);87 test.done();88 });89 const preload_pass = test.step_func(() => {90 const subresource_element = buildElementFromDestination(91 resource_url,92 destination,93 subresource_attrs94 );95 if (subresource_sri_success) {96 subresource_element.onload = subresource_pass;97 subresource_element.onerror = valid_subresource_failed;98 } else {99 subresource_element.onload = invalid_subresource_succeeded;100 subresource_element.onerror = subresource_pass;101 }102 document.body.append(subresource_element);103 });104 if (preload_sri_success) {105 link.onload = preload_pass;106 link.onerror = valid_preload_failed;107 } else {108 link.onload = invalid_preload_succeeded;109 link.onerror = preload_pass;110 }111 document.head.append(link);112}113var SRIStyleTest = function(queue, pass, name, attrs, customCallback, altPassValue) {114 this.pass = pass;115 this.name = "Style: " + name;116 this.customCallback = customCallback || function () {};117 this.attrs = attrs || {};118 this.passValue = altPassValue || "rgb(255, 255, 0)";119 this.test = async_test(this.name);120 this.queue = queue;121 this.queue.push(this);122}123SRIStyleTest.prototype.execute = function() {124 var that = this;125 var container = document.getElementById("container");126 while (container.hasChildNodes()) {127 container.removeChild(container.firstChild);128 }129 var test = this.test;130 var div = document.createElement("div");131 div.className = "testdiv";132 var e = document.createElement("link");133 this.attrs.rel = this.attrs.rel || "stylesheet";134 for (var key in this.attrs) {135 if (this.attrs.hasOwnProperty(key)) {136 e.setAttribute(key, this.attrs[key]);137 }138 }139 if(this.pass) {140 e.addEventListener("load", function() {141 test.step(function() {142 var background = window.getComputedStyle(div, null).getPropertyValue("background-color");143 assert_equals(background, that.passValue);144 test.done();145 });146 });147 e.addEventListener("error", function() {148 test.step(function(){ assert_unreached("Good load fired error handler.") })149 });150 } else {151 e.addEventListener("load", function() {152 test.step(function() { assert_unreached("Bad load succeeded.") })153 });154 e.addEventListener("error", function() {155 test.step(function() {156 var background = window.getComputedStyle(div, null).getPropertyValue("background-color");157 assert_not_equals(background, that.passValue);158 test.done();159 });160 });161 }162 container.appendChild(div);163 container.appendChild(e);164 this.customCallback(e, container);...
Using AI Code Generation
1function valid_subresource_failed() {2 return wpt.valid_subresource_failed();3}4function valid_subresource_passed() {5 return wpt.valid_subresource_passed();6}7function valid_subresource_passed_with_data() {8 return wpt.valid_subresource_passed_with_data();9}10function valid_subresource_passed_with_message() {11 return wpt.valid_subresource_passed_with_message();12}13function valid_subresource_passed_with_message_and_data() {14 return wpt.valid_subresource_passed_with_message_and_data();15}16function valid_subresource_timeout() {17 return wpt.valid_subresource_timeout();18}19function valid_test_failed() {20 return wpt.valid_test_failed();21}22function valid_test_passed() {23 return wpt.valid_test_passed();24}25function valid_test_passed_with_data() {26 return wpt.valid_test_passed_with_data();27}28function valid_test_passed_with_message() {29 return wpt.valid_test_passed_with_message();30}31function valid_test_passed_with_message_and_data() {32 return wpt.valid_test_passed_with_message_and_data();33}34function valid_test_timeout() {35 return wpt.valid_test_timeout();36}37function valid_testharness_failed() {38 return wpt.valid_testharness_failed();39}
Using AI Code Generation
1wpt.valid_subresource_failed(function(result) {2 console.log(result);3});4wpt.valid_subresource_passed(function(result) {5 console.log(result);6});7wpt.visual_complete(function(result) {8 console.log(result);9});10wpt.waterfall(function(result) {11 console.log(result);12});13wpt.waterfall_view(function(result) {14 console.log(result);15});16wpt.webpagetest(function(result) {17 console.log(result);18});19wpt.webpagetest_view(function(result) {20 console.log(result);21});22wpt.yslow(function(result) {23 console.log(result);24});25wpt.yslow_view(function(result) {26 console.log(result);27});28wpt.getLocations(function(result) {29 console.log(result);30});31wpt.getTesters(function(result) {32 console.log(result);33});34wpt.getTesters(function(result) {35 console.log(result);36});37wpt.getTesters(function(result) {38 console.log(result);39});
Using AI Code Generation
1var report = require('wptreport');2report.valid_subresource_failed('www.google.com', 'www.yahoo.com', 'www.youtube.com');3var report = require('wptreport');4report.valid_subresource_passed('www.google.com', 'www.yahoo.com', 'www.youtube.com');5var report = require('wptreport');6report.valid_subresource_failed('www.google.com', 'www.yahoo.com', 'www.youtube.com');7var report = require('wptreport');8report.valid_subresource_passed('www.google.com', 'www.yahoo.com', 'www.youtube.com');9var report = require('wptreport');10report.valid_subresource_failed('www.google.com', 'www.yahoo.com', 'www.youtube.com');11var report = require('wptreport');12report.valid_subresource_passed('www.google.com', 'www.yahoo.com', 'www.youtube.com');13var report = require('wptreport');14report.valid_subresource_failed('www.google.com', 'www.yahoo.com', 'www.youtube.com');15var report = require('wptreport');16report.valid_subresource_passed('www.google.com', 'www.yahoo.com', 'www.youtube.com');17var report = require('wptreport');18report.valid_subresource_failed('www.google.com', 'www.yahoo.com', 'www.youtube.com');19var report = require('wptreport');20report.valid_subresource_passed('www.google.com', 'www.yahoo.com', 'www.youtube.com');21var report = require('wpt
Using AI Code Generation
1function testValidSubresourceFailed() {2 var wptb = new WPTB();3 wptb.valid_subresource_failed("www.google.com", "www.google.com", "www.google.com", "www.google.com", "www.google.com");4}5function testValidSubresourcePassed() {6 var wptb = new WPTB();7 wptb.valid_subresource_passed("www.google.com", "www.google.com", "www.google.com", "www.google.com", "www.google.com");8}9function testValidSubresourcesFailed() {10 var wptb = new WPTB();11 wptb.valid_subresources_failed("www.google.com", "www.google.com", "www.google.com", "www.google.com", "www.google.com");12}13function testValidSubresourcesPassed() {14 var wptb = new WPTB();15 wptb.valid_subresources_passed("www.google.com", "www.google.com", "www.google.com", "www.google.com", "www.google.com");16}17function testValidSubresourcesPassed() {18 var wptb = new WPTB();19 wptb.valid_subresources_passed("www.google.com", "www.google.com", "www.google.com", "www.google.com", "www.google.com");20}21function testValidSubresourcesPassed() {22 var wptb = new WPTB();23 wptb.valid_subresources_passed("www.google.com", "www.google.com", "www.google.com", "www.google.com", "www.google.com");24}25function testValidSubresourcesPassed() {26 var wptb = new WPTB();27 wptb.valid_subresources_passed("www.google.com", "www.google.com", "www.google.com", "www.google.com", "www.google.com");
Using AI Code Generation
1var wptreport = require('wptreport');2var wptreport = require('wptreport');3var wptreport = require('wptreport');4var wptreport = require('wptreport');5var wptreport = require('wptreport');6var wptreport = require('wptreport');7var report = new wptreport('test.js', 'http
Using AI Code Generation
1function valid_subresource_failed(subresource, reason) {2 if (subresource == null) {3 test(function() {4 assert_unreached(reason);5 }, reason);6 return false;7 }8 return true;9}10function valid_subresource(subresource, reason) {11 if (subresource == null) {12 test(function() {13 assert_unreached(reason);14 }, reason);15 return false;16 }17 return true;18}19function valid_subresource_async(subresource, reason) {20 if (subresource == null) {21 async_test(function(t) {22 assert_unreached(reason);23 t.done();24 }, reason);25 return false;26 }27 return true;28}
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!!