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);...
Using AI Code Generation
1var wpt = require('wptdriver');2 if (err) {3 console.log(err);4 } else {5 console.log(data);6 }7});8### SRIPreloadTest(url, callback)9* data - The data returned from the test (see below for details)10### SRIPreloadTest(url, options, callback)11* data - The data returned from the test (see below for details)12* location - The location to test from (default: "Dulles:Chrome")13* connectivity - The network connectivity to test with (default: "Cable")14* runs - The number of test runs to average together (default: 3)15* firstViewOnly - If true, only the first view will be tested (default: false)16* video - If true, a video of the test will be captured (default: false)17* pollResults - If true, the results will be polled until the test is complete (default: false)18* timeout - The number of milliseconds to wait for the test to complete before timing out (default: 5 minutes)19* label - A label to associate with the test (default: none)20* private - If true, the test will not be included in public statistics (default: false)21* priority - The priority to use for the test (default: 0)22* web10 - If true, Web10 will be enabled for the test (default: false)23* block - If true, the test will be blocked from starting (default: false)24* login - If specified, the login credentials to use for the test (see below for details)25* timeline - If true, a timeline trace will be captured (default: false)26* trace - If true, a full page trace will be captured (default: false)27* basicAuth - If true, basic authentication will be used for the test (default: false)
Using AI Code Generation
1function SRIPreloadTest()2{3 var test = new SRIPreloadTest();4 test.run();5}6function SRIPreloadTest()7{8 var test = new SRIPreloadTest();9 test.run();10}11function SRIPreloadTest()12{13 var test = new SRIPreloadTest();14 test.run();15}16function SRIPreloadTest()17{18 var test = new SRIPreloadTest();19 test.run();20}21function SRIPreloadTest()22{23 var test = new SRIPreloadTest();24 test.run();25}26function SRIPreloadTest()27{28 var test = new SRIPreloadTest();29 test.run();30}31function SRIPreloadTest()32{33 var test = new SRIPreloadTest();34 test.run();35}36function SRIPreloadTest()37{38 var test = new SRIPreloadTest();39 test.run();40}
Using AI Code Generation
1var wpt = require('wpt');2var wptdriver = new wpt.WptDriver();3 if (err) {4 console.log(err);5 }6 else {7 console.log(result);8 }9});10 if (err) {11 console.log(err);12 }13 else {14 console.log(result);15 }16});17 if (err) {18 console.log(err);19 }20 else {21 console.log(result);22 }23});24 if (err) {25 console.log(err);26 }27 else {28 console.log(result);29 }30});
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!!