Best JavaScript code snippet using wpt
testharness-helper.js
Source: testharness-helper.js
1function assert_no_csp_event_for_url(test, url) {2 document.addEventListener("securitypolicyviolation", test.step_func(e => {3 if (e.blockedURI !== url)4 return;5 assert_unreached("SecurityPolicyViolation event fired for " + url);6 }));7}8function assert_no_event(test, obj, name) {9 obj.addEventListener(name, test.unreached_func("The '" + name + "' event should not have fired."));10}11function waitUntilCSPEventForURL(test, url) {12 return new Promise((resolve, reject) => {13 document.addEventListener("securitypolicyviolation", test.step_func(e => {14 if (e.blockedURI == url)15 resolve(e);16 }));17 });18}19function waitUntilEvent(obj, name) {20 return new Promise((resolve, reject) => {21 obj.addEventListener(name, resolve);22 });23}24// Given the URL of a worker that pings its opener upon load, this25// function builds a test that asserts that the ping is received,26// and that no CSP event fires.27function assert_worker_is_loaded(url, description) {28 async_test(t => {29 assert_no_csp_event_for_url(t, url);30 var w = new Worker(url);31 assert_no_event(t, w, "error");32 waitUntilEvent(w, "message")33 .then(t.step_func_done(e => {34 assert_equals(e.data, "ping");35 }));36 }, description);37}38function assert_shared_worker_is_loaded(url, description) {39 async_test(t => {40 assert_no_csp_event_for_url(t, url);41 var w = new SharedWorker(url);42 assert_no_event(t, w, "error");43 waitUntilEvent(w.port, "message")44 .then(t.step_func_done(e => {45 assert_equals(e.data, "ping");46 }));47 w.port.start();48 }, description);49}50function assert_service_worker_is_loaded(url, description) {51 promise_test(t => {52 assert_no_csp_event_for_url(t, url);53 return Promise.all([54 waitUntilEvent(navigator.serviceWorker, "message")55 .then(e => {56 assert_equals(e.data, "ping");57 }),58 navigator.serviceWorker.register(url, { scope: url })59 .then(r => {60 var sw = r.active || r.installing || r.waiting;61 t.add_cleanup(_ => r.unregister());62 sw.postMessage("pong?");63 })64 ]);65 }, description);66}...
connect-src-allow.sub.js
Source: connect-src-allow.sub.js
2importScripts("{{location[server]}}/content-security-policy/support/testharness-helper.js");3// Same-origin4async_test(t => {5 var url = "{{location[server]}}/content-security-policy/support/resource.py?same-origin-fetch";6 assert_no_csp_event_for_url(t, url);7 fetch(url)8 .then(t.step_func_done(r => assert_equals(r.status, 200)));9}, "Same-origin 'fetch()' in " + self.location.protocol + self.location.search);10async_test(t => {11 var url = "{{location[server]}}/content-security-policy/support/resource.py?same-origin-xhr";12 assert_no_csp_event_for_url(t, url);13 var xhr = new XMLHttpRequest();14 xhr.open("GET", url);15 xhr.onload = t.step_func_done();16 xhr.onerror = t.unreached_func();17 xhr.send();18}, "Same-origin XHR in " + self.location.protocol + self.location.search);19// Cross-origin20async_test(t => {21 var url = "http://{{domains[www]}}:{{ports[http][1]}}/content-security-policy/support/resource.py?cross-origin-fetch";22 assert_no_csp_event_for_url(t, url);23 fetch(url)24 .then(t.step_func_done(r => assert_equals(r.status, 200)));25}, "Cross-origin 'fetch()' in " + self.location.protocol + self.location.search);26async_test(t => {27 var url = "http://{{domains[www]}}:{{ports[http][1]}}/content-security-policy/support/resource.py?cross-origin-xhr";28 assert_no_csp_event_for_url(t, url);29 var xhr = new XMLHttpRequest();30 xhr.open("GET", url);31 xhr.onload = t.step_func_done();32 xhr.onerror = t.unreached_func();33 xhr.send();34}, "Cross-origin XHR in " + self.location.protocol + self.location.search);35// Same-origin redirecting to cross-origin36async_test(t => {37 var url = "{{location[server]}}/common/redirect-opt-in.py?status=307&location=http://{{domains[www]}}:{{ports[http][1]}}/content-security-policy/support/resource.py?cross-origin-fetch";38 assert_no_csp_event_for_url(t, url);39 fetch(url)40 .then(t.step_func_done(r => assert_equals(r.status, 200)));41}, "Same-origin => cross-origin 'fetch()' in " + self.location.protocol + self.location.search);...
Using AI Code Generation
1function assert_no_csp_event_for_url(url) {2 var csp_events = performance.getEntriesByType("cspviolation");3 for (var i = 0; i < csp_events.length; i++) {4 if (csp_events[i].blockedURI == url)5 assert_unreached("CSP event for " + url);6 }7}8function assert_csp_event_for_url(url, expectedViolatedDirective, expectedBlockedURI) {9 var csp_events = performance.getEntriesByType("cspviolation");10 for (var i = 0; i < csp_events.length; i++) {11 if (csp_events[i].blockedURI == url) {12 assert_equals(csp_events[i].violatedDirective, expectedViolatedDirective);13 assert_equals(csp_events[i].blockedURI, expectedBlockedURI);14 return;15 }16 }17 assert_unreached("No CSP event for " + url);18}19function assert_no_csp_event() {20 var csp_events = performance.getEntriesByType("cspviolation");21 if (csp_events.length != 0)22 assert_unreached("CSP event for " + csp_events[0].blockedURI);23}24function assert_csp_event(expectedViolatedDirective, expectedBlockedURI) {25 var csp_events = performance.getEntriesByType("cspviolation");26 assert_equals(csp_events.length, 1);27 assert_equals(csp_events[0].violatedDirective, expectedViolatedDirective);28 assert_equals(csp_events[0].blockedURI, expectedBlockedURI);29}30function assert_csp_event_count(expectedCount) {31 var csp_events = performance.getEntriesByType("cspviolation");32 assert_equals(csp_events.length, expectedCount);33}34function assert_csp_event_for_url_count(url, expectedCount) {35 var csp_events = performance.getEntriesByType("cspviolation");36 var count = 0;37 for (var i =
Using AI Code Generation
1function assert_no_csp_event_for_url(url) {2 var csp_events = performance.getEntriesByType("navigation");3 for (var i = 0; i < csp_events.length; i++) {4 if (csp_events[i].name == url) {5 test_failed("CSP event found for " + url);6 return;7 }8 }9 test_passed("No CSP event found for " + url);10}11function assert_csp_event_for_url(url) {12 var csp_events = performance.getEntriesByType("navigation");13 for (var i = 0; i < csp_events.length; i++) {14 if (csp_events[i].name == url) {15 test_passed("CSP event found for " + url);16 return;17 }18 }19 test_failed("No CSP event found for " + url);20}
Using AI Code Generation
1function assert_no_csp_event_for_url(url) {2 var req = new XMLHttpRequest();3 req.open("GET", url, false);4 req.send(null);5 assert_equals(req.status, 200);6 assert_equals(req.responseText, "PASS");7}
Using AI Code Generation
1function assert_no_csp_event_for_url(url) {2 return new Promise(function(resolve, reject) {3 var xhr = new XMLHttpRequest();4 xhr.open("GET", url, true);5 xhr.onload = function() {6 if (xhr.status == 200) {7 resolve(xhr.responseText);8 } else {9 reject(xhr.responseText);10 }11 };12 xhr.send();13 });14}15 assert_equals(response, "PASS");16}).catch(function(error) {17 assert_unreached(error);18});19function assert_csp_event_for_url(url) {20 return new Promise(function(resolve, reject) {21 var xhr = new XMLHttpRequest();22 xhr.open("GET", url, true);23 xhr.onload = function() {24 if (xhr.status == 200) {25 resolve(xhr.responseText);26 } else {27 reject(xhr.responseText);28 }29 };30 xhr.send();31 });32}33 assert_equals(response, "PASS");34}).catch(function(error) {35 assert_unreached(error);36});
Using AI Code Generation
1function assert_no_csp_event_for_url(url) {2 var xhr = new XMLHttpRequest();3 xhr.open("GET", url, false);4 xhr.send();5 if (xhr.status != 200) {6 throw new Error("Unexpected status code: " + xhr.status);7 }8 var event = xhr.responseText;9 if (event != "no event") {10 throw new Error("Unexpected event: " + event);11 }12}13function assert_csp_event_for_url(url) {14 var xhr = new XMLHttpRequest();15 xhr.open("GET", url, false);16 xhr.send();17 if (xhr.status != 200) {18 throw new Error("Unexpected status code: " + xhr.status);19 }20 var event = xhr.responseText;21 if (event == "no event") {22 throw new Error("No event received");23 }24 return event;25}26function assert_csp_event_for_url_with_type(url, type) {27 var event = assert_csp_event_for_url(url);28 var event_type = event.split(" ")[0];29 if (event_type != type) {30 throw new Error("Unexpected event type: " + event_type);31 }32}33function assert_csp_event_for_url_with_type_and_uri(url, type, uri) {34 var event = assert_csp_event_for_url(url);35 var event_type = event.split(" ")[0];36 if (event_type != type) {37 throw new Error("Unexpected event type: " + event_type);38 }39 var event_uri = event.split(" ")[1];40 if (event_uri != uri) {41 throw new Error("Unexpected event uri: " + event_uri);42 }43}44function assert_csp_event_for_url_with_type_and_uri_and_source_file(url, type, uri, source_file) {45 var event = assert_csp_event_for_url(url);46 var event_type = event.split(" ")[0];47 if (event_type != type) {48 throw new Error("Unexpected event
Using AI Code Generation
1function assert_no_csp_event_for_url(url, description) {2 var request = new XMLHttpRequest();3 request.open("GET", url, false);4 request.send();5 if (request.status != 200) {6 assert_unreached("Unexpected status code for request to " + url + ": " + request.status);7 }8 assert_equals(request.responseText, "PASS", description);9}10function assert_csp_event_for_url(url, description) {11 var request = new XMLHttpRequest();12 request.open("GET", url, false);13 request.send();14 if (request.status != 200) {15 assert_unreached("Unexpected status code for request to " + url + ": " + request.status);16 }17 assert_equals(request.responseText, "FAIL", description);18}19function assert_csp_event_for_url_with_reason(url, description, reason) {20 var request = new XMLHttpRequest();21 request.open("GET", url, false);22 request.send();23 if (request.status != 200) {24 assert_unreached("Unexpected status code for request to " + url + ": " + request.status);25 }26 assert_equals(request.responseText, reason, description);27}28function assert_no_csp_event_for_url_with_reason(url, description, reason) {29 var request = new XMLHttpRequest();30 request.open("GET", url, false);31 request.send();32 if (request.status != 200) {33 assert_unreached("Unexpected status code for request to " + url + ": " + request.status);34 }35 assert_not_equals(request.responseText, reason, description);36}37function assert_csp_event_for_url_with_status(url, description, status) {38 var request = new XMLHttpRequest();39 request.open("GET", url, false);40 request.send();41 assert_equals(request.status, status, description);42}43function assert_no_csp_event_for_url_with_status(url, description, status) {44 var request = new XMLHttpRequest();45 request.open("GET", url, false);46 request.send();
Using AI Code Generation
1function run_test() {2 var csp = "script-src 'none'";3 assert_no_csp_event_for_url(url, csp);4}5The Web Platform Tests (WPT) are a collection of tests for the Web platform,6[Web Platform Tests Project](
Check out the latest blogs from LambdaTest on this topic:
Testing is a critical step in any web application development process. However, it can be an overwhelming task if you don’t have the right tools and expertise. A large percentage of websites still launch with errors that frustrate users and negatively affect the overall success of the site. When a website faces failure after launch, it costs time and money to fix.
We launched LT Browser in 2020, and we were overwhelmed by the response as it was awarded as the #5 product of the day on the ProductHunt platform. Today, after 74,585 downloads and 7,000 total test runs with an average of 100 test runs each day, the LT Browser has continued to help developers build responsive web designs in a jiffy.
Smartphones have changed the way humans interact with technology. Be it travel, fitness, lifestyle, video games, or even services, it’s all just a few touches away (quite literally so). We only need to look at the growing throngs of smartphone or tablet users vs. desktop users to grasp this reality.
As part of one of my consulting efforts, I worked with a mid-sized company that was looking to move toward a more agile manner of developing software. As with any shift in work style, there is some bewilderment and, for some, considerable anxiety. People are being challenged to leave their comfort zones and embrace a continuously changing, dynamic working environment. And, dare I say it, testing may be the most ‘disturbed’ of the software roles in agile development.
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!!