Best JavaScript code snippet using wpt
reporting-common.js
Source:reporting-common.js
1const executor_path = "/common/dispatcher/executor.html?pipe=";2const coep_header = '|header(Cross-Origin-Embedder-Policy,require-corp)';3const isWPTSubEnabled = "{{GET[pipe]}}".includes("sub");4const getReportEndpointURL = (reportID) =>5 `/reporting/resources/report.py?reportID=${reportID}`;6const reportEndpoint = {7 name: "coop-report-endpoint",8 reportID: isWPTSubEnabled ? "{{GET[report_id]}}" : token(),9 reports: []10};11const reportOnlyEndpoint = {12 name: "coop-report-only-endpoint",13 reportID: isWPTSubEnabled ? "{{GET[report_only_id]}}" : token(),14 reports: []15};16const popupReportEndpoint = {17 name: "coop-popup-report-endpoint",18 reportID: token(),19 reports: []20};21const popupReportOnlyEndpoint = {22 name: "coop-popup-report-only-endpoint",23 reportID: token(),24 reports: []25};26const redirectReportEndpoint = {27 name: "coop-redirect-report-endpoint",28 reportID: token(),29 reports: []30};31const redirectReportOnlyEndpoint = {32 name: "coop-redirect-report-only-endpoint",33 reportID: token(),34 reports: []35};36const reportEndpoints = [37 reportEndpoint,38 reportOnlyEndpoint,39 popupReportEndpoint,40 popupReportOnlyEndpoint,41 redirectReportEndpoint,42 redirectReportOnlyEndpoint43];44// Allows RegExps to be pretty printed when printing unmatched expected reports.45Object.defineProperty(RegExp.prototype, "toJSON", {46 value: RegExp.prototype.toString47});48function wait(ms) {49 return new Promise(resolve => step_timeout(resolve, ms));50}51// Check whether a |report| is a "opener breakage" COOP report.52function isCoopOpenerBreakageReport(report) {53 if (report.type != "coop")54 return false;55 if (report.body.type != "navigation-from-response" &&56 report.body.type != "navigation-to-response") {57 return false;58 }59 return true;60}61async function clearReportsOnServer(host) {62 const res = await fetch(63 '/reporting/resources/report.py', {64 method: "POST",65 body: JSON.stringify({66 op: "DELETE",67 reportIDs: reportEndpoints.map(endpoint => endpoint.reportID)68 })69 });70 assert_equals(res.status, 200, "reports cleared");71}72async function pollReports(endpoint) {73 const res = await fetch(getReportEndpointURL(endpoint.reportID),74 { cache: 'no-store' });75 if (res.status !== 200) {76 return;77 }78 for (const report of await res.json()) {79 if (isCoopOpenerBreakageReport(report))80 endpoint.reports.push(report);81 }82}83// Recursively check that all members of expectedReport are present or matched84// in report.85// Report may have members not explicitly expected by expectedReport.86function isObjectAsExpected(report, expectedReport) {87 if (( report === undefined || report === null88 || expectedReport === undefined || expectedReport === null )89 && report !== expectedReport ) {90 return false;91 }92 if (expectedReport instanceof RegExp && typeof report === "string") {93 return expectedReport.test(report);94 }95 // Perform this check now, as RegExp and strings above have different typeof.96 if (typeof report !== typeof expectedReport)97 return false;98 if (typeof expectedReport === 'object') {99 return Object.keys(expectedReport).every(key => {100 return isObjectAsExpected(report[key], expectedReport[key]);101 });102 }103 return report == expectedReport;104}105async function checkForExpectedReport(expectedReport) {106 return new Promise( async (resolve, reject) => {107 const polls = 5;108 const waitTime = 200;109 for (var i=0; i < polls; ++i) {110 pollReports(expectedReport.endpoint);111 for (var j=0; j<expectedReport.endpoint.reports.length; ++j){112 if (isObjectAsExpected(expectedReport.endpoint.reports[j],113 expectedReport.report)){114 expectedReport.endpoint.reports.splice(j,1);115 resolve();116 return;117 }118 };119 await wait(waitTime);120 }121 reject(122 replaceTokensInReceivedReport(123 "No report matched the expected report for endpoint: "124 + expectedReport.endpoint.name125 + ", expected report: " + JSON.stringify(expectedReport.report)126 + ", within available reports: "127 + JSON.stringify(expectedReport.endpoint.reports)128 ));129 });130}131function replaceFromRegexOrString(str, match, value) {132 if (str instanceof RegExp) {133 return RegExp(str.source.replace(match, value));134 }135 return str.replace(match, value);136}137// Replace generated values in regexes and strings of an expected report:138// EXECUTOR_UUID: the uuid generated with token().139function replaceValuesInExpectedReport(expectedReport, executorUuid) {140 if (expectedReport.report.body !== undefined) {141 if (expectedReport.report.body.nextResponseURL !== undefined) {142 expectedReport.report.body.nextResponseURL = replaceFromRegexOrString(143 expectedReport.report.body.nextResponseURL, "EXECUTOR_UUID",144 executorUuid);145 }146 if (expectedReport.report.body.previousResponseURL !== undefined) {147 expectedReport.report.body.previousResponseURL = replaceFromRegexOrString(148 expectedReport.report.body.previousResponseURL, "EXECUTOR_UUID",149 executorUuid);150 }151 if (expectedReport.report.body.referrer !== undefined) {152 expectedReport.report.body.referrer = replaceFromRegexOrString(153 expectedReport.report.body.referrer, "EXECUTOR_UUID",154 executorUuid);155 }156 }157 if (expectedReport.report.url !== undefined) {158 expectedReport.report.url = replaceFromRegexOrString(159 expectedReport.report.url, "EXECUTOR_UUID", executorUuid);160 }161 return expectedReport;162}163function replaceTokensInReceivedReport(str) {164 return str.replace(/.{8}-.{4}-.{4}-.{4}-.{12}/g, `(uuid)`);165}166// Run a test (such as coop_coep_test from ./common.js) then check that all167// expected reports are present.168async function reportingTest(testFunction, executorToken, expectedReports) {169 await new Promise(testFunction);170 expectedReports = Array.from(171 expectedReports,172 report => replaceValuesInExpectedReport(report, executorToken) );173 await Promise.all(Array.from(expectedReports, checkForExpectedReport));174}175function getReportEndpoints(host) {176 result = "";177 reportEndpoints.forEach(178 reportEndpoint => {179 let reportToJSON = {180 'group': `${reportEndpoint.name}`,181 'max_age': 3600,182 'endpoints': [183 {184 'url': `${host}/reporting/resources/report.py?reportID=${reportEndpoint.reportID}`185 },186 ]187 };188 result += JSON.stringify(reportToJSON)189 .replace(/,/g, '\\,')190 .replace(/\(/g, '\\\(')191 .replace(/\)/g, '\\\)=')192 + "\\,";193 }194 );195 return result.slice(0, -2);196}197function navigationReportingTest(testName, host, coop, coep, coopRo, coepRo,198 expectedReports ){199 const executorToken = token();200 const callbackToken = token();201 promise_test(async t => {202 await reportingTest( async resolve => {203 const openee_url = host.origin + executor_path +204 `|header(report-to,${encodeURIComponent(getReportEndpoints(host.origin))})` +205 `|header(Cross-Origin-Opener-Policy,${encodeURIComponent(coop)})` +206 `|header(Cross-Origin-Embedder-Policy,${encodeURIComponent(coep)})` +207 `|header(Cross-Origin-Opener-Policy-Report-Only,${encodeURIComponent(coopRo)})` +208 `|header(Cross-Origin-Embedder-Policy-Report-Only,${encodeURIComponent(coepRo)})`+209 `&uuid=${executorToken}`;210 const openee = window.open(openee_url);211 const uuid = token();212 t.add_cleanup(() => send(uuid, "window.close()"));213 // 1. Make sure the new document is loaded.214 send(executorToken, `215 send("${callbackToken}", "Ready");216 `);217 let reply = await receive(callbackToken);218 assert_equals(reply, "Ready");219 resolve();220 }, executorToken, expectedReports);221 }, `coop reporting test ${testName} to ${host.name} with ${coop}, ${coep}, ${coopRo}, ${coepRo}`);222}223// Run an array of reporting tests then verify there's no reports that were not224// expected.225// Tests' elements contain: host, coop, coep, coop-report-only,226// coep-report-only, expectedReports.227// See isObjectAsExpected for explanations regarding the matching behavior.228async function runNavigationReportingTests(testName, tests) {229 await clearReportsOnServer();230 tests.forEach(test => {231 navigationReportingTest(testName, ...test);232 });233 verifyRemainingReports();234}235function verifyRemainingReports() {236 promise_test(t => {237 return Promise.all(reportEndpoints.map(async (endpoint) => {238 await pollReports(endpoint);239 assert_equals(endpoint.reports.length, 0, `${endpoint.name} should be empty`);240 }));241 }, "verify remaining reports");242}243const receiveReport = async function(uuid, type) {244 while(true) {245 let reports = await Promise.race([246 receive(uuid),247 new Promise(resolve => {248 step_timeout(resolve, 1000, "timeout");249 })250 ]);251 if (reports == "timeout")252 return "timeout";253 reports = JSON.parse(reports);254 for(report of reports) {255 if (report?.body?.type == type)256 return report;257 }258 }259}260// Build a set of headers to tests the reporting API. This defines a set of261// matching 'Report-To', 'Cross-Origin-Opener-Policy' and262// 'Cross-Origin-Opener-Policy-Report-Only' headers.263const reportToHeaders = function(uuid) {264 const report_endpoint_url = dispatcher_path + `?uuid=${uuid}`;265 let reportToJSON = {266 'group': `${uuid}`,267 'max_age': 3600,268 'endpoints': [269 {'url': report_endpoint_url.toString()},270 ]271 };272 reportToJSON = JSON.stringify(reportToJSON)273 .replace(/,/g, '\\,')274 .replace(/\(/g, '\\\(')275 .replace(/\)/g, '\\\)=');276 return {277 header: `|header(report-to,${reportToJSON})`,278 coopSameOriginHeader: `|header(Cross-Origin-Opener-Policy,same-origin%3Breport-to="${uuid}")`,279 coopSameOriginAllowPopupsHeader: `|header(Cross-Origin-Opener-Policy,same-origin-allow-popups%3Breport-to="${uuid}")`,280 coopReportOnlySameOriginHeader: `|header(Cross-Origin-Opener-Policy-Report-Only,same-origin%3Breport-to="${uuid}")`,281 coopReportOnlySameOriginAllowPopupsHeader: `|header(Cross-Origin-Opener-Policy-Report-Only,same-origin-allow-popups%3Breport-to="${uuid}")`,282 };...
Using AI Code Generation
1var wpt = require('wpt.js');2var isWptSubEnabled = wpt.isWptSubEnabled();3console.log(isWptSubEnabled);4var wpt = require('wpt.js');5var isWptSubEnabled = wpt.isWptSubEnabled();6var wpt = require('wpt.js');7var isWptSubEnabled = wpt.isWptSubEnabled();8var wpt = require('wpt.js');9var isWptSubEnabled = wpt.isWptSubEnabled();10var wpt = require('wpt.js');11var isWptSubEnabled = wpt.isWptSubEnabled();12var wpt = require('wpt.js');13var isWptSubEnabled = wpt.isWptSubEnabled();14var wpt = require('wpt.js');15var isWptSubEnabled = wpt.isWptSubEnabled();16var wpt = require('wpt.js');17var isWptSubEnabled = wpt.isWptSubEnabled();18var wpt = require('wpt.js');19var isWptSubEnabled = wpt.isWptSubEnabled();20var wpt = require('wpt.js');21var isWptSubEnabled = wpt.isWptSubEnabled();22var wpt = require('wpt.js');23var isWptSubEnabled = wpt.isWptSubEnabled();24var wpt = require('wpt.js');25var isWptSubEnabled = wpt.isWptSubEnabled();26var wpt = require('wpt.js
Using AI Code Generation
1var wptService = Components.classes["@mozilla.org/wpt/service;1"].getService(Components.interfaces.nsIWPTService);2var wptEnabled = wptService.isWPTSubEnabled();3var wptService = Components.classes["@mozilla.org/wpt/service;1"].getService(Components.interfaces.nsIWPTService);4wptService.enableWPT();5var wptService = Components.classes["@mozilla.org/wpt/service;1"].getService(Components.interfaces.nsIWPTService);6wptService.disableWPT();7var wptService = Components.classes["@mozilla.org/wpt/service;1"].getService(Components.interfaces.nsIWPTService);8var wptEnabled = wptService.isWPTEnabled();
Using AI Code Generation
1var wpt = require('wpt.js');2var wptSubEnabled = wpt.isWPTSubEnabled();3console.log(wptSubEnabled);4var wpt = require('wpt.js');5var wptSubEnabled = wpt.isWPTSubEnabled();6console.log(wptSubEnabled);7var wpt = require('wpt.js');8var wptSubEnabled = wpt.isWPTSubEnabled();9console.log(wptSubEnabled);10var wpt = require('wpt.js');11var wptSubEnabled = wpt.isWPTSubEnabled();12console.log(wptSubEnabled);13var wpt = require('wpt.js');14var wptSubEnabled = wpt.isWPTSubEnabled();15console.log(wptSubEnabled);16var wpt = require('wpt.js');17var wptSubEnabled = wpt.isWPTSubEnabled();18console.log(wptSubEnabled);19var wpt = require('wpt.js');20var wptSubEnabled = wpt.isWPTSubEnabled();21console.log(wptSubEnabled);22var wpt = require('wpt.js');23var wptSubEnabled = wpt.isWPTSubEnabled();24console.log(wptSubEnabled);25var wpt = require('wpt.js');26var wptSubEnabled = wpt.isWPTSubEnabled();27console.log(wptSubEnabled);
Using AI Code Generation
1var wptService = require("wptService");2var isSubEnabled = wptService.isWPTSubEnabled();3var isWPTSubEnabled = function() {4 var isSubEnabled = false;5 return isSubEnabled;6}7exports.isWPTSubEnabled = isWPTSubEnabled;8var isWPTSubEnabled = function() {9 var isSubEnabled = false;10 return isSubEnabled;11}12exports.isWPTSubEnabled = isWPTSubEnabled;13var isWPTSubEnabled = function() {14 var isSubEnabled = false;15 return isSubEnabled;16}17exports.isWPTSubEnabled = isWPTSubEnabled;18var isWPTSubEnabled = function() {19 var isSubEnabled = false;20 return isSubEnabled;21}22exports.isWPTSubEnabled = isWPTSubEnabled;23var isWPTSubEnabled = function() {24 var isSubEnabled = false;25 return isSubEnabled;26}27exports.isWPTSubEnabled = isWPTSubEnabled;28var isWPTSubEnabled = function() {29 var isSubEnabled = false;30 return isSubEnabled;31}32exports.isWPTSubEnabled = isWPTSubEnabled;33var isWPTSubEnabled = function() {34 var isSubEnabled = false;
Using AI Code Generation
1var wpt = require('wpt');2var isWPTSubEnabled = wpt.isWPTSubEnabled();3if (isWPTSubEnabled) {4 console.log('WPT Subscriber is enabled');5} else {6 console.log('WPT Subscriber is not enabled');7}
Using AI Code Generation
1 var editor = CKEDITOR.instances.editor1;2 var isSubEnabled = editor.plugins.wptextpattern.isWPTSubEnabled();3editor.config.wptextpattern = {4 {5 match: /~(.*)~/g,6 }7 };8editor.config.wptextpattern = {9 {10 match: /~(.*)~/g,11 }12 };13editor.config.wptextpattern = {14 {15 match: /~(.*)~/g,16 }17 };18editor.config.wptextpattern = {19 {20 match: /~(.*)~/g,21 }22 };23editor.config.wptextpattern = {24 {25 match: /~(.*)~/g,26 }27 };28editor.config.wptextpattern = {29 {30 match: /~(.*)~/g,31 }32 };33editor.config.wptextpattern = {34 {35 match: /~(.*)~/g,36 }37 };38editor.config.wptextpattern = {39 {40 match: /~(.*)~/g,41 }42 };
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!!