Best JavaScript code snippet using wpt
featurepolicy.js
Source:featurepolicy.js
...58// expected_result: the expected value to compare to the data passed back59// from the src page by postMessage.60// allow_attribute: Optional argument, only provided when an allow61// attribute should be specified on the iframe.62function test_feature_availability_with_post_message_result(63 test, src, expected_result, allow_attribute) {64 var test_result = function(data, feature_description) {65 assert_equals(data, expected_result);66 };67 test_feature_availability(null, test, src, test_result, allow_attribute);68}69// If this page is intended to test the named feature (according to the URL),70// tests the feature availability and posts the result back to the parent.71// Otherwise, does nothing.72function test_feature_in_iframe(feature_name, feature_promise_factory) {73 if (location.hash.includes(feature_name)) {74 feature_promise_factory().then(75 () => window.parent.postMessage('#OK', '*'),76 (e) => window.parent.postMessage('#' + e.name, '*'));77 }78}79// Returns true if the URL for this page indicates that it is embedded in an80// iframe.81function page_loaded_in_iframe() {82 return location.hash.startsWith('#iframe');83}84// Returns a same-origin (relative) URL suitable for embedding in an iframe for85// testing the availability of the feature.86function same_origin_url(feature_name) {87 // Append #iframe to the URL so we can detect the iframe'd version of the88 // page.89 return location.pathname + '#iframe#' + feature_name;90}91// Returns a cross-origin (absolute) URL suitable for embedding in an iframe for92// testing the availability of the feature.93function cross_origin_url(base_url, feature_name) {94 return base_url + same_origin_url(feature_name);95}96// This function runs all feature policy tests for a particular feature that97// has a default policy of "self". This includes testing:98// 1. Feature usage succeeds by default in the top level frame.99// 2. Feature usage succeeds by default in a same-origin iframe.100// 3. Feature usage fails by default in a cross-origin iframe.101// 4. Feature usage suceeds when an allow attribute is specified on a102// cross-origin iframe.103//104// The same page which called this function will be loaded in the iframe in105// order to test feature usage there. When this function is called in that106// context it will simply run the feature and return a result back via107// postMessage.108//109// Arguments:110// cross_origin: A cross-origin URL base to be used to load the page which111// called into this function.112// feature_name: The name of the feature as it should be specified in an113// allow attribute.114// error_name: If feature usage does not succeed, this is the string115// representation of the error that will be passed in the rejected116// promise.117// feature_promise_factory: A function which returns a promise which tests118// feature usage. If usage succeeds, the promise should resolve. If it119// fails, the promise should reject with an error that can be120// represented as a string.121function run_all_fp_tests_allow_self(122 cross_origin, feature_name, error_name, feature_promise_factory) {123 // This may be the version of the page loaded up in an iframe. If so, just124 // post the result of running the feature promise back to the parent.125 if (page_loaded_in_iframe()) {126 test_feature_in_iframe(feature_name, feature_promise_factory);127 return;128 }129 // Run the various tests.130 // 1. Allowed in top-level frame.131 promise_test(132 () => feature_promise_factory(),133 'Default "' + feature_name +134 '" feature policy ["self"] allows the top-level document.');135 // 2. Allowed in same-origin iframe.136 const same_origin_frame_pathname = same_origin_url(feature_name);137 async_test(138 t => {139 test_feature_availability_with_post_message_result(140 t, same_origin_frame_pathname, '#OK');141 },142 'Default "' + feature_name +143 '" feature policy ["self"] allows same-origin iframes.');144 // 3. Blocked in cross-origin iframe.145 const cross_origin_frame_url = cross_origin_url(cross_origin, feature_name);146 async_test(147 t => {148 test_feature_availability_with_post_message_result(149 t, cross_origin_frame_url, '#' + error_name);150 },151 'Default "' + feature_name +152 '" feature policy ["self"] disallows cross-origin iframes.');153 // 4. Allowed in cross-origin iframe with "allow" attribute.154 async_test(155 t => {156 test_feature_availability_with_post_message_result(157 t, cross_origin_frame_url, '#OK', feature_name);158 },159 'Feature policy "' + feature_name +160 '" can be enabled in cross-origin iframes using "allow" attribute.');161}162// This function runs all feature policy tests for a particular feature that163// has a default policy of "*". This includes testing:164// 1. Feature usage succeeds by default in the top level frame.165// 2. Feature usage succeeds by default in a same-origin iframe.166// 3. Feature usage succeeds by default in a cross-origin iframe.167// 4. Feature usage fails when an allow attribute is specified on a168// cross-origin iframe with a value of "feature-name 'none'".169//170// The same page which called this function will be loaded in the iframe in171// order to test feature usage there. When this function is called in that172// context it will simply run the feature and return a result back via173// postMessage.174//175// Arguments:176// cross_origin: A cross-origin URL base to be used to load the page which177// called into this function.178// feature_name: The name of the feature as it should be specified in an179// allow attribute.180// error_name: If feature usage does not succeed, this is the string181// representation of the error that will be passed in the rejected182// promise.183// feature_promise_factory: A function which returns a promise which tests184// feature usage. If usage succeeds, the promise should resolve. If it185// fails, the promise should reject with an error that can be186// represented as a string.187function run_all_fp_tests_allow_all(188 cross_origin, feature_name, error_name, feature_promise_factory) {189 // This may be the version of the page loaded up in an iframe. If so, just190 // post the result of running the feature promise back to the parent.191 if (page_loaded_in_iframe()) {192 test_feature_in_iframe(feature_name, feature_promise_factory);193 return;194 }195 // Run the various tests.196 // 1. Allowed in top-level frame.197 promise_test(198 () => feature_promise_factory(),199 'Default "' + feature_name +200 '" feature policy ["*"] allows the top-level document.');201 // 2. Allowed in same-origin iframe.202 const same_origin_frame_pathname = same_origin_url(feature_name);203 async_test(204 t => {205 test_feature_availability_with_post_message_result(206 t, same_origin_frame_pathname, '#OK');207 },208 'Default "' + feature_name +209 '" feature policy ["*"] allows same-origin iframes.');210 // 3. Allowed in cross-origin iframe.211 const cross_origin_frame_url = cross_origin_url(cross_origin, feature_name);212 async_test(213 t => {214 test_feature_availability_with_post_message_result(215 t, cross_origin_frame_url, '#OK');216 },217 'Default "' + feature_name +218 '" feature policy ["*"] allows cross-origin iframes.');219 // 4. Blocked in cross-origin iframe with "allow" attribute set to 'none'.220 async_test(221 t => {222 test_feature_availability_with_post_message_result(223 t, cross_origin_frame_url, '#' + error_name,224 feature_name + " 'none'");225 },226 'Feature policy "' + feature_name +227 '" can be disabled in cross-origin iframes using "allow" attribute.');...
Using AI Code Generation
1async_test(async t => {2 const result = await wpt_test_driver.test_feature_availability_with_post_message_result(3 "test_feature_availability_with_post_message_result");4 assert_equals(result, "test_feature_availability_with_post_message_result");5 t.done();6}, "test_feature_availability_with_post_message_result");7import json8import os9import sys10import time11import uuid12from collections import namedtuple13from six.moves import BaseHTTPServer14from six.moves import socketserver15from blinkpy.common.host import Host16from blinkpy.common.net.buildbot import Build17from blinkpy.common.net.git_cl import GitCL18from blinkpy.common.net.layout_test_results import LayoutTestResults19from blinkpy.common.system.executive import ScriptError20from blinkpy.common.system.log_testing import LoggingTestCase21from blinkpy.web_tests.models.test_expectations import TestExpectations22from blinkpy.web_tests.port.test import TestPort23from blinkpy.w3c.wpt_manifest import WPTManifest24from blinkpy.w3c.wpt_test_expectations_updater import WPTTestExpectationsUpdater25from blinkpy.w3c.wpt_test_driver import WPTTestDriver26from blinkpy.w3c.wpt_test_finder import WPTTestFinder
Using AI Code Generation
1test_feature_availability_with_post_message_result("test_feature_availability_with_post_message_result", "test_feature_availability_with_post_message_result", 1, 1, 1, 1, 1, 1);2test_feature_availability_with_post_message_result("test_feature_availability_with_post_message_result", "test_feature_availability_with_post_message_result", 1, 1, 1, 1, 1, 1);3test_feature_availability_with_post_message_result("test_feature_availability_with_post_message_result", "test_feature_availability_with_post_message_result", 1, 1, 1, 1, 1, 1);4test_feature_availability_with_post_message_result("test_feature_availability_with_post_message_result", "test_feature_availability_with_post_message_result", 1, 1, 1, 1, 1, 1);5test_feature_availability_with_post_message_result("test_feature_availability_with_post_message_result", "test_feature_availability_with_post_message_result", 1, 1, 1, 1, 1, 1);6test_feature_availability_with_post_message_result("test_feature_availability_with_post_message_result", "test_feature_availability_with_post_message_result", 1, 1, 1, 1, 1, 1);7test_feature_availability_with_post_message_result("test_feature_availability_with_post_message_result", "test_feature_availability_with_post_message_result", 1, 1, 1, 1, 1, 1);
Using AI Code Generation
1var wptb = new WebPlatformTestBridge();2var test = wptb.test_feature_availability_with_post_message_result('test', 'test');3test.then(function(result) {4 console.log(result);5});6var result = {supported: true, name: 'test', message: 'test'};7window.parent.postMessage(result, '*');8var result = {supported: false, name: 'test', message: 'test'};9window.parent.postMessage(result, '*');10var wptb = new WebPlatformTestBridge();11var test = wptb.test_feature_availability_with_post_message_result('test', 'test');12test.then(function(result) {13 console.log(result);14});15var wptb = new WebPlatformTestBridge();16var test = wptb.test_feature_availability_with_post_message('test', 'test', {17});18test.then(function(result) {19 console.log(result);20});21var wptb = new WebPlatformTestBridge();22var test = wptb.test_feature_availability_with_post_message('test', 'test', {
Using AI Code Generation
1var wptb = new WebPlatformTestBridge();2wptb.test_feature_availability_with_post_message_result("feature_name", "test_name", "test_id", "test_description");3var wptb = new WebPlatformTestBridge();4wptb.test_feature_availability_with_post_message_result("feature_name", "test_name", "test_id", "test_description");5var wptb = new WebPlatformTestBridge();6wptb.test_feature_availability_with_post_message_result("feature_name", "test_name", "test_id", "test_description");7var wptb = new WebPlatformTestBridge();8wptb.test_feature_availability_with_post_message_result("feature_name", "test_name", "test_id", "test_description");9var wptb = new WebPlatformTestBridge();10wptb.test_feature_availability_with_post_message_result("feature_name", "test_name", "test_id", "test_description");11var wptb = new WebPlatformTestBridge();12wptb.test_feature_availability_with_post_message_result("feature_name", "test_name", "test_id", "test_description");13var wptb = new WebPlatformTestBridge();14wptb.test_feature_availability_with_post_message_result("feature_name", "
Using AI Code Generation
1var test = require('wptdriver').test;2var driver = require('wptdriver').driver;3test('test_feature_availability_with_post_message_result', function (test) {4 driver.test_feature_availability_with_post_message_result(function (result) {5 test.assert(result);6 test.done();7 });8});9var test = require('wptdriver').test;10var driver = require('wptdriver').driver;11test('test_feature_availability_with_get_user_media_result', function (test) {12 driver.test_feature_availability_with_get_user_media_result(function (result) {13 test.assert(result);14 test.done();15 });16});17var test = require('wptdriver').test;18var driver = require('wptdriver').driver;19test('test_feature_availability_with_get_user_media_result', function (test) {20 driver.test_feature_availability_with_get_user_media_result(function (result) {21 test.assert(result);22 test.done();23 });24});25var test = require('wptdriver').test;26var driver = require('wptdriver').driver;27test('test_feature_availability_with_get_user_media_result', function (test) {28 driver.test_feature_availability_with_get_user_media_result(function (result) {29 test.assert(result);30 test.done();31 });32});
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!!