How to use iframe_token method in wpt

Best JavaScript code snippet using wpt

iframe-test.js

Source:iframe-test.js Github

copy

Full Screen

1// To use the functions below, be sure to include the following files in your2// test:3// - "/common/get-host-info.sub.js" to get the different origin values.4// - "common.js" to have the origins easily available.5// - "/common/dispatcher/dispatcher.js" for cross-origin messaging.6// - "/common/utils.js" for token().7function getBaseExecutorPath(origin) {8 return origin + '/common/dispatcher/executor.html';9}10function getHeadersPipe(headers) {11 const coop_header = headers.coop ?12 `|header(Cross-Origin-Opener-Policy,${encodeURIComponent(headers.coop)})` : '';13 const coep_header = headers.coep ?14 `|header(Cross-Origin-Embedder-Policy,${encodeURIComponent(headers.coep)})` : '';15 return coop_header + coep_header;16}17function getExecutorPath(uuid, origin, headers) {18 return getBaseExecutorPath(origin) +19 `?uuid=${uuid}` +20 `&pipe=${getHeadersPipe(headers)}`;21}22function evaluate(target_token, script) {23 const reply_token = token();24 send(target_token, `send('${reply_token}', ${script});`);25 return receive(reply_token);26}27// Return true if an opened iframe can access |property| on a stored28// window.popup object without throwing an error.29function iframeCanAccessProperty(iframe_token, property) {30 const reply_token = token();31 send(iframe_token,32 `try {33 const unused = window.popup['${property}'];34 send('${reply_token}', 'true')35 } catch (errors) {36 send('${reply_token}', 'false')37 }`);38 return receive(reply_token);39}40// Returns the script necessary to open a popup, given the method in41// `popup_via`. Supported methods are 'window_open' that leverages42// window.open(), 'anchor' that creates an <a> HTML element and clicks on it,43// and 'form' that creates a form and submits it.44function popupOpeningScript(popup_via, popup_url, popup_origin, headers,45 popup_token) {46 if (popup_via === 'window_open')47 return `window.popup = window.open('${popup_url}', '${popup_token}');`;48 if (popup_via === 'anchor') {49 return `50 const anchor = document.createElement('a');51 anchor.href = '${popup_url}';52 anchor.rel = "opener";53 anchor.target = '${popup_token}';54 anchor.innerText = "anchor";55 document.body.appendChild(anchor);56 anchor.click();57 `;58 }59 if (popup_via === "form") {60 return `61 const form = document.createElement("form");62 form.action = '${getBaseExecutorPath(popup_origin.origin)}';63 form.target = '${popup_token}';64 form.method = 'GET';65 const add_param = (name, value) => {66 const input = document.createElement("input");67 input.name = name;68 input.value = value;69 form.appendChild(input);70 };71 add_param("uuid", "${popup_token}");72 add_param("pipe", "${getHeadersPipe(headers)}");73 document.body.appendChild(form);74 form.submit();75 `;76 }77 assert_not_reached('Unrecognized popup opening method.');78}79// Verifies that a popup with origin `popup_origin` and headers `headers` has80// the expected `opener_state` after being opened from an iframe with origin81// `iframe_origin`.82function iframe_test(description, iframe_origin, popup_origin, headers,83 expected_opener_state) {84 for (const popup_via of ['window_open', 'anchor','form']) {85 promise_test(async t => {86 const iframe_token = token();87 const popup_token = token();88 const reply_token = token();89 const frame = document.createElement("iframe");90 const iframe_url = getExecutorPath(91 iframe_token,92 iframe_origin.origin,93 {});94 frame.src = iframe_url;95 document.body.append(frame);96 send(iframe_token, `send('${reply_token}', 'Iframe loaded');`);97 assert_equals(await receive(reply_token), 'Iframe loaded');98 const popup_url = getExecutorPath(99 popup_token,100 popup_origin.origin,101 headers);102 // We open popup and then ping it, it will respond after loading.103 send(iframe_token, popupOpeningScript(popup_via, popup_url, popup_origin,104 headers, popup_token));105 send(popup_token, `send('${reply_token}', 'Popup loaded');`);106 assert_equals(await receive(reply_token), 'Popup loaded');107 // Make sure the popup and the iframe are removed once the test has run,108 // keeping a clean state.109 add_completion_callback(() => {110 frame.remove();111 send(popup_token, `close()`);112 });113 // Give some time for things to settle across processes etc. before114 // proceeding with verifications.115 await new Promise(resolve => { t.step_timeout(resolve, 500); });116 // Verify that the opener is in the state we expect it to be in.117 switch (expected_opener_state) {118 case 'preserved': {119 assert_equals(120 await evaluate(popup_token, 'opener != null'), "true",121 'Popup has an opener?');122 assert_equals(123 await evaluate(popup_token, `name === '${popup_token}'`), "true",124 'Popup has a name?');125 // When the popup was created using window.open, we've kept a handle126 // and we can do extra verifications.127 if (popup_via === 'window_open') {128 assert_equals(129 await evaluate(iframe_token, 'popup.closed'), "false",130 'Popup appears closed from iframe?');131 assert_equals(132 await iframeCanAccessProperty(iframe_token, "document"),133 popup_origin === iframe_origin ? "true" : "false",134 'Iframe has dom access to the popup?');135 assert_equals(136 await iframeCanAccessProperty(iframe_token, "frames"), "true",137 'Iframe has cross origin access to the popup?');138 }139 break;140 }141 case 'restricted': {142 assert_equals(143 await evaluate(popup_token, 'opener != null'), "true",144 'Popup has an opener?');145 assert_equals(146 await evaluate(popup_token, `name === '${popup_token}'`), "true",147 'Popup has a name?');148 // When the popup was created using window.open, we've kept a handle149 // and we can do extra verifications.150 if (popup_via === 'window_open') {151 assert_equals(152 await evaluate(iframe_token, 'popup.closed'), "false",153 'Popup appears closed from iframe?');154 assert_equals(155 await iframeCanAccessProperty(iframe_token, "document"), "false",156 'Iframe has dom access to the popup?');157 assert_equals(158 await iframeCanAccessProperty(iframe_token, "frames"), "false",159 'Iframe has cross origin access to the popup?');160 assert_equals(161 await iframeCanAccessProperty(iframe_token, "closed"), "true",162 'Iframe has limited cross origin access to the popup?');163 }164 break;165 }166 case 'severed': {167 assert_equals(await evaluate(popup_token, 'opener != null'), "false",168 'Popup has an opener?');169 assert_equals(170 await evaluate(popup_token, `name === '${popup_token}'`), "false",171 'Popup has a name?');172 // When the popup was created using window.open, we've kept a handle173 // and we can do extra verifications.174 if (popup_via === 'window_open') {175 assert_equals(await evaluate(iframe_token, 'popup.closed'), "true",176 'Popup appears closed from iframe?');177 }178 break;179 }180 case 'noopener': {181 assert_equals(await evaluate(popup_token, 'opener != null'), "false",182 'Popup has an opener?');183 assert_equals(184 await evaluate(popup_token, `name === '${popup_token}'`), "false",185 'Popup has a name?');186 // When the popup was created using window.open, we've kept a handle187 // and we can do extra verifications.188 if (popup_via === 'window_open') {189 assert_equals(190 await evaluate(iframe_token, 'popup == null'), "true",191 'Popup handle is null in iframe?');192 }193 break;194 }195 default:196 assert_not_reached('Unrecognized opener state: ' +197 expected_opener_state);198 }199 }, `${description} with ${popup_via}`);200 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1function get_token() {2 var request = new XMLHttpRequest();3 request.onload = function() {4 var data = JSON.parse(request.responseText);5 var token = data.token;6 get_user_data(token);7 };8 request.send();9}10function get_user_data(token) {11 var request = new XMLHttpRequest();12 request.setRequestHeader("Authorization", "Bearer " + token);13 request.onload = function() {14 var data = JSON.parse(request.responseText);15 console.log(data);16 };17 request.send();18}

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful