Best JavaScript code snippet using wpt
featurepolicy.js
Source:featurepolicy.js
1// Feature test to avoid timeouts2function assert_feature_policy_supported() {3 assert_not_equals(document.featurePolicy, undefined,4 'Feature Policy is supported');5}6// Tests whether a feature that is enabled/disabled by feature policy works7// as expected.8// Arguments:9// feature_description: a short string describing what feature is being10// tested. Examples: "usb.GetDevices()", "PaymentRequest()".11// test: test created by testharness. Examples: async_test, promise_test.12// src: URL where a feature's availability is checked. Examples:13// "/feature-policy/resources/feature-policy-payment.html",14// "/feature-policy/resources/feature-policy-usb.html".15// expect_feature_available: a callback(data, feature_description) to16// verify if a feature is available or unavailable as expected.17// The file under the path "src" defines what "data" is sent back as a18// postMessage. Inside the callback, some tests (e.g., EXPECT_EQ,19// EXPECT_TRUE, etc) are run accordingly to test a feature's20// availability.21// Example: expect_feature_available_default(data, feature_description).22// feature_name: Optional argument, only provided when testing iframe allow23// attribute. "feature_name" is the feature name of a policy controlled24// feature (https://wicg.github.io/feature-policy/#features).25// See examples at:26// https://github.com/WICG/feature-policy/blob/master/features.md27// allow_attribute: Optional argument, only used for testing fullscreen:28// "allowfullscreen"29function test_feature_availability(30 feature_description, test, src, expect_feature_available, feature_name,31 allow_attribute) {32 let frame = document.createElement('iframe');33 frame.src = src;34 if (typeof feature_name !== 'undefined') {35 frame.allow = frame.allow.concat(";" + feature_name);36 }37 if (typeof allow_attribute !== 'undefined') {38 frame.setAttribute(allow_attribute, true);39 }40 window.addEventListener('message', test.step_func(function handler(evt) {41 if (evt.source === frame.contentWindow) {42 expect_feature_available(evt.data, feature_description);43 document.body.removeChild(frame);44 window.removeEventListener('message', handler);45 test.done();46 }47 }));48 document.body.appendChild(frame);49}50// Default helper functions to test a feature's availability:51function expect_feature_available_default(data, feature_description) {52 assert_true(data.enabled, feature_description);53}54function expect_feature_unavailable_default(data, feature_description) {55 assert_false(data.enabled, feature_description);56}57// This is the same as test_feature_availability() but instead of passing in a58// function to check the result of the message sent back from an iframe, instead59// just compares the result to an expected result passed in.60// Arguments:61// test: test created by testharness. Examples: async_test, promise_test.62// src: the URL to load in an iframe in which to test the feature.63// expected_result: the expected value to compare to the data passed back64// from the src page by postMessage.65// allow_attribute: Optional argument, only provided when an allow66// attribute should be specified on the iframe.67function test_feature_availability_with_post_message_result(68 test, src, expected_result, allow_attribute) {69 var test_result = function(data, feature_description) {70 assert_equals(data, expected_result);71 };72 test_feature_availability(null, test, src, test_result, allow_attribute);73}74// If this page is intended to test the named feature (according to the URL),75// tests the feature availability and posts the result back to the parent.76// Otherwise, does nothing.77function test_feature_in_iframe(feature_name, feature_promise_factory) {78 if (location.hash.endsWith(`#${feature_name}`)) {79 feature_promise_factory().then(80 () => window.parent.postMessage('#OK', '*'),81 (e) => window.parent.postMessage('#' + e.name, '*'));82 }83}84// Returns true if the URL for this page indicates that it is embedded in an85// iframe.86function page_loaded_in_iframe() {87 return location.hash.startsWith('#iframe');88}89// Returns a same-origin (relative) URL suitable for embedding in an iframe for90// testing the availability of the feature.91function same_origin_url(feature_name) {92 // Append #iframe to the URL so we can detect the iframe'd version of the93 // page.94 return location.pathname + '#iframe#' + feature_name;95}96// Returns a cross-origin (absolute) URL suitable for embedding in an iframe for97// testing the availability of the feature.98function cross_origin_url(base_url, feature_name) {99 return base_url + same_origin_url(feature_name);100}101// This function runs all feature policy tests for a particular feature that102// has a default policy of "self". This includes testing:103// 1. Feature usage succeeds by default in the top level frame.104// 2. Feature usage succeeds by default in a same-origin iframe.105// 3. Feature usage fails by default in a cross-origin iframe.106// 4. Feature usage suceeds when an allow attribute is specified on a107// cross-origin iframe.108//109// The same page which called this function will be loaded in the iframe in110// order to test feature usage there. When this function is called in that111// context it will simply run the feature and return a result back via112// postMessage.113//114// Arguments:115// cross_origin: A cross-origin URL base to be used to load the page which116// called into this function.117// feature_name: The name of the feature as it should be specified in an118// allow attribute.119// error_name: If feature usage does not succeed, this is the string120// representation of the error that will be passed in the rejected121// promise.122// feature_promise_factory: A function which returns a promise which tests123// feature usage. If usage succeeds, the promise should resolve. If it124// fails, the promise should reject with an error that can be125// represented as a string.126function run_all_fp_tests_allow_self(127 cross_origin, feature_name, error_name, feature_promise_factory) {128 // This may be the version of the page loaded up in an iframe. If so, just129 // post the result of running the feature promise back to the parent.130 if (page_loaded_in_iframe()) {131 test_feature_in_iframe(feature_name, feature_promise_factory);132 return;133 }134 // Run the various tests.135 // 1. Allowed in top-level frame.136 promise_test(137 () => feature_promise_factory(),138 'Default "' + feature_name +139 '" feature policy ["self"] allows the top-level document.');140 // 2. Allowed in same-origin iframe.141 const same_origin_frame_pathname = same_origin_url(feature_name);142 async_test(143 t => {144 test_feature_availability_with_post_message_result(145 t, same_origin_frame_pathname, '#OK');146 },147 'Default "' + feature_name +148 '" feature policy ["self"] allows same-origin iframes.');149 // 3. Blocked in cross-origin iframe.150 const cross_origin_frame_url = cross_origin_url(cross_origin, feature_name);151 async_test(152 t => {153 test_feature_availability_with_post_message_result(154 t, cross_origin_frame_url, '#' + error_name);155 },156 'Default "' + feature_name +157 '" feature policy ["self"] disallows cross-origin iframes.');158 // 4. Allowed in cross-origin iframe with "allow" attribute.159 async_test(160 t => {161 test_feature_availability_with_post_message_result(162 t, cross_origin_frame_url, '#OK', feature_name);163 },164 'Feature policy "' + feature_name +165 '" can be enabled in cross-origin iframes using "allow" attribute.');166}167// This function runs all feature policy tests for a particular feature that168// has a default policy of "*". This includes testing:169// 1. Feature usage succeeds by default in the top level frame.170// 2. Feature usage succeeds by default in a same-origin iframe.171// 3. Feature usage succeeds by default in a cross-origin iframe.172// 4. Feature usage fails when an allow attribute is specified on a173// cross-origin iframe with a value of "feature-name 'none'".174//175// The same page which called this function will be loaded in the iframe in176// order to test feature usage there. When this function is called in that177// context it will simply run the feature and return a result back via178// postMessage.179//180// Arguments:181// cross_origin: A cross-origin URL base to be used to load the page which182// called into this function.183// feature_name: The name of the feature as it should be specified in an184// allow attribute.185// error_name: If feature usage does not succeed, this is the string186// representation of the error that will be passed in the rejected187// promise.188// feature_promise_factory: A function which returns a promise which tests189// feature usage. If usage succeeds, the promise should resolve. If it190// fails, the promise should reject with an error that can be191// represented as a string.192function run_all_fp_tests_allow_all(193 cross_origin, feature_name, error_name, feature_promise_factory) {194 // This may be the version of the page loaded up in an iframe. If so, just195 // post the result of running the feature promise back to the parent.196 if (page_loaded_in_iframe()) {197 test_feature_in_iframe(feature_name, feature_promise_factory);198 return;199 }200 // Run the various tests.201 // 1. Allowed in top-level frame.202 promise_test(203 () => feature_promise_factory(),204 'Default "' + feature_name +205 '" feature policy ["*"] allows the top-level document.');206 // 2. Allowed in same-origin iframe.207 const same_origin_frame_pathname = same_origin_url(feature_name);208 async_test(209 t => {210 test_feature_availability_with_post_message_result(211 t, same_origin_frame_pathname, '#OK');212 },213 'Default "' + feature_name +214 '" feature policy ["*"] allows same-origin iframes.');215 // 3. Allowed in cross-origin iframe.216 const cross_origin_frame_url = cross_origin_url(cross_origin, feature_name);217 async_test(218 t => {219 test_feature_availability_with_post_message_result(220 t, cross_origin_frame_url, '#OK');221 },222 'Default "' + feature_name +223 '" feature policy ["*"] allows cross-origin iframes.');224 // 4. Blocked in cross-origin iframe with "allow" attribute set to 'none'.225 async_test(226 t => {227 test_feature_availability_with_post_message_result(228 t, cross_origin_frame_url, '#' + error_name,229 feature_name + " 'none'");230 },231 'Feature policy "' + feature_name +232 '" can be disabled in cross-origin iframes using "allow" attribute.');233 // 5. Blocked in same-origin iframe with "allow" attribute set to 'none'.234 async_test(235 t => {236 test_feature_availability_with_post_message_result(237 t, same_origin_frame_pathname, '#' + error_name,238 feature_name + " 'none'");239 },240 'Feature policy "' + feature_name +241 '" can be disabled in same-origin iframes using "allow" attribute.');242}243// This function tests that a given policy allows each feature for the correct244// list of origins specified by the |expected_policy|.245// Arguments:246// expected_policy: A list of {feature, allowlist} pairs where the feature is247// enabled for every origin in the allowlist, in the |policy|.248// policy: Either a document.featurePolicy or an iframe.featurePolicy to be249// tested.250// message: A short description of what policy is being tested.251function test_allowlists(expected_policy, policy, message) {252 for (var allowlist of allowlists) {253 test(function() {254 assert_array_equals(255 policy.getAllowlistForFeature(allowlist.feature),256 allowlist.allowlist);257 }, message + ' for feature ' + allowlist.feature);258 }259}260// This function tests that a subframe's document policy allows a given feature.261// A feature is allowed in a frame either through inherited policy or specified262// by iframe allow attribute.263// Arguments:264// test: test created by testharness. Examples: async_test, promise_test.265// feature: feature name that should be allowed in the frame.266// src: the URL to load in the frame.267// allow: the allow attribute (container policy) of the iframe268function test_allowed_feature_for_subframe(message, feature, src, allow) {269 let frame = document.createElement('iframe');270 if (typeof allow !== 'undefined') {271 frame.allow = allow;272 }273 promise_test(function() {274 assert_feature_policy_supported();275 frame.src = src;276 return new Promise(function(resolve, reject) {277 window.addEventListener('message', function handler(evt) {278 resolve(evt.data);279 }, { once: true });280 document.body.appendChild(frame);281 }).then(function(data) {282 assert_true(data.includes(feature), feature);283 });284 }, message);285}286// This function tests that a subframe's document policy disallows a given287// feature. A feature is allowed in a frame either through inherited policy or288// specified by iframe allow attribute.289// Arguments:290// test: test created by testharness. Examples: async_test, promise_test.291// feature: feature name that should not be allowed in the frame.292// src: the URL to load in the frame.293// allow: the allow attribute (container policy) of the iframe294function test_disallowed_feature_for_subframe(message, feature, src, allow) {295 let frame = document.createElement('iframe');296 if (typeof allow !== 'undefined') {297 frame.allow = allow;298 }299 promise_test(function() {300 assert_feature_policy_supported();301 frame.src = src;302 return new Promise(function(resolve, reject) {303 window.addEventListener('message', function handler(evt) {304 resolve(evt.data);305 }, { once: true });306 document.body.appendChild(frame);307 }).then(function(data) {308 assert_false(data.includes(feature), feature);309 });310 }, message);311}312// This function tests that a subframe with header policy defined on a given313// feature allows and disallows the feature as expected.314// Arguments:315// feature: feature name.316// frame_header_policy: either *, 'self' or 'none', defines the frame317// document's header policy on |feature|.318// src: the URL to load in the frame.319// test_expects: contains 6 expected results of either |feature| is allowed320// or not inside of a local or remote iframe nested inside321// the subframe given the header policy to be either *,322// 'slef', or 'none'.323// test_name: name of the test.324function test_subframe_header_policy(325 feature, frame_header_policy, src, test_expects, test_name) {326 let frame = document.createElement('iframe');327 promise_test(function() {328 assert_feature_policy_supported()329 frame.src = src + '?pipe=sub|header(Feature-Policy,' + feature + ' '330 + frame_header_policy + ';)';331 return new Promise(function(resolve) {332 window.addEventListener('message', function handler(evt) {333 resolve(evt.data);334 });335 document.body.appendChild(frame);336 }).then(function(results) {337 for (var j = 0; j < results.length; j++) {338 var data = results[j];339 function test_result(message, test_expect) {340 if (test_expect) {341 assert_true(data.allowedfeatures.includes(feature), message);342 } else {...
Using AI Code Generation
1import {assert_feature_policy_supported} from '/resources/testdriver-vendor.js';2import {assert_true} from '/resources/testharness.js';3promise_test(async t => {4 const result = await assert_feature_policy_supported('payment', 'self');5 assert_true(result);6}, 'Test if payment feature policy is supported');
Using AI Code Generation
1assert_feature_policy_supported("feature-name", "iframe");2assert_feature_policy_supported("feature-name", "iframe", "allow");3assert_feature_policy_supported("feature-name", "iframe", "allow", "allowfullscreen");4assert_feature_policy_supported("feature-name", "iframe", "allow", "allowfullscreen", "allowpaymentrequest");5assert_feature_policy_supported("feature-name", "iframe", "allow", "allowfullscreen", "allowpaymentrequest", "allowusermedia");6assert_feature_policy_supported("feature-name", "iframe", "allow", "allowfullscreen", "allowpaymentrequest", "allowusermedia", "allowvr");7assert_feature_policy_supported("feature-name", "iframe", "allow", "allowfullscreen", "allowpaymentrequest", "allowusermedia", "allowvr", "allowxr");8assert_feature_policy_supported("feature-name", "iframe", "allow", "allowfullscreen", "allowpaymentrequest", "allowusermedia", "allowvr", "allowxr", "encrypted-media");9assert_feature_policy_supported("feature-name", "iframe", "allow", "allowfullscreen", "allowpaymentrequest", "allowusermedia", "allowvr", "allowxr", "encrypted-media", "fullscreen");10assert_feature_policy_supported("feature-name", "iframe", "allow", "allowfullscreen", "allowpaymentrequest", "allowusermedia", "allowvr", "allowxr", "encrypted-media", "fullscreen", "geolocation");11assert_feature_policy_supported("feature-name", "iframe", "allow", "allowfullscreen", "allowpaymentrequest", "allowusermedia", "allowvr", "allowxr", "encrypted-media", "fullscreen", "geolocation", "microphone");
Using AI Code Generation
1assert_feature_policy_supported("feature_name"); testharness.js2assert_feature_policy_unsupported("feature_name");3function assert_feature_policy_supported(feature_name) e4 assert_true(feature_name in document.featurePolicy.allowedFeatures, "Feature supported by the browser");5}6function ck if the feature is sunupported (feature_name) {7 assert_false(feature_name in document.featurePolicy.allowedFeatures, "Feature not supported by the browser");
Using AI Code Generation
1import {assert_feature_policy_supported} froe browser2assert_feature_policy_supported("feature_name");3assert_feature_policy_unsupported("feature_name");4function assert_feature_policy_supported(feature_name) {5 assert_true(feature_name in document.featurePolicy.allowedFeatures, "Feature supported by the browser");6}7function assert_feature_policy_unsupported(feature_name) {8 assert_false(feature_name in document.featurePolicy.allowedFeatures, "Feature not supported by the browser");9}
Using AI Code Generation
1importScripts('/resources/testharness.js');2test(() => {3 assert_feature_policy_supported('camera');4}, 'camera is supported');5test(() => {6 assert_feature_policy_supported('microphone');7}, 'microphone is supported');8test(() => {9 assert_feature_policy_supported('geolocation');10}, 'geolocation is supported');11test(() => {12 assert_feature_policy_supported('autoplay');13}, 'autoplay is supported');14test(() => {15 assert_feature_policy_supported('fullscreen');16}, 'fullscreen is supported');17test(() => {18 assert_feature_policy_supported('payment');19}, 'payment is supported');20test(() => {21 assert_feature_policy_supported('vibrate');22}, 'vibrate is supported');23test(() => {24 assert_feature_policy_supported('speaker');25}, 'speaker is supported');26test(() => {27 assert_feature_policy_supported('sync-xhr');28}, 'sync-xhr is supported');29import {assert_feature_policy_supported} from '/resources/testdriver-vendor.js';30promise_test(async t => {31 await assert_feature_policy_supported('accelerometer');32}, 'accelerometer is supported');
Using AI Code Generation
1importScripts('/resources/testharness.js');2test(() => {3 assert_feature_policy_supported('camera');4}, 'camera is supported');5test(() => {6 assert_feature_policy_supported('microphone');7}, 'microphone is supported');8test(() => {9 assert_feature_policy_supported('geolocation');10}, 'geolocation is supported');11test(() => {12 assert_feature_policy_supported('autoplay');13}, 'autoplay is supported');14test(() => {15 assert_feature_policy_supported('fullscreen');16}, 'fullscreen is supported');17test(() => {18 assert_feature_policy_supported('payment');19}, 'payment is supported');20test(() => {21 assert_feature_policy_supported('vibrate');22}, 'vibrate is supported');23test(() => {24 assert_feature_policy_supported('speaker');25}, 'speaker is supported');26test(() => {27 assert_feature_policy_supported('sync-xhr');28}, 'sync-xhr is supported');
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!!