Best JavaScript code snippet using wpt
cache.window.js
Source:cache.window.js
1// META: script=/common/get-host-info.sub.js2// META: script=/common/utils.js3// META: script=/common/dispatcher/dispatcher.js4// META: script=./resources/common.js5// With COEP:credentialless, requesting a resource without credentials MUST NOT6// return a response requested with credentials. This would be a security7// issue, since COEP:credentialless can be used to enable crossOriginIsolation.8//9// The test the behavior of the HTTP cache:10// 1. b.com stores cookie.11// 2. a.com(COEP:unsafe-none): request b.com's resource.12// 3. a.com(COEP:credentialless): request b.com's resource.13//14// The first time, the resource is requested with credentials. The response is15// served with Cache-Control: max-age=31536000. It enters the cache.16// The second time, the resource is requested without credentials. The response17// in the cache must not be returned.18const cookie_key = "coep_cache_key";19const cookie_value = "coep_cache_value";20const same_origin = get_host_info().HTTPS_ORIGIN;21const cross_origin = get_host_info().HTTPS_REMOTE_ORIGIN;22const GetCookie = (response) => {23 return parseCookies(JSON.parse(response))[cookie_key];24}25// "same_origin" document with COEP:unsafe-none.26const w_coep_none_token = token();27const w_coep_none_url = same_origin + executor_path + coep_none +28 `&uuid=${w_coep_none_token}`29const w_coep_none = window.open(w_coep_none_url);30add_completion_callback(() => w_coep_none.close());31// "same_origin" document with COEP:credentialles.32const w_coep_credentialless_token = token();33const w_coep_credentialless_url = same_origin + executor_path +34 coep_credentialless + `&uuid=${w_coep_credentialless_token}`35const w_coep_credentialless = window.open(w_coep_credentialless_url);36add_completion_callback(() => w_coep_credentialless.close());37const this_token = token();38// A request toward a "cross-origin" cacheable response.39const request_token = token();40const request_url = cacheableShowRequestHeaders(cross_origin, request_token);41promise_setup(async test => {42 await setCookie(cross_origin, cookie_key, cookie_value + cookie_same_site_none);43}, "Set cookie");44// The "same-origin" COEP:unsafe-none document fetchs a "cross-origin"45// resource. The request is sent with credentials.46promise_setup(async test => {47 send(w_coep_none_token, `48 await fetch("${request_url}", {49 mode : "no-cors",50 credentials: "include",51 });52 send("${this_token}", "Resource fetched");53 `);54 assert_equals(await receive(this_token), "Resource fetched");55 assert_equals(await receive(request_token).then(GetCookie), cookie_value);56}, "Cache a response requested with credentials");57// The "same-origin" COEP:credentialless document fetches the same resource58// without credentials. The HTTP cache must not be used. Instead a second59// request must be made without credentials.60promise_test(async test => {61 send(w_coep_credentialless_token, `62 await fetch("${request_url}", {63 mode : "no-cors",64 credentials: "include",65 });66 send("${this_token}", "Resource fetched");67 `);68 assert_equals(await receive(this_token), "Resource fetched");69 test.step_timeout(test.unreached_func("The HTTP cache has been used"), 1500);70 assert_equals(await receive(request_token).then(GetCookie), undefined);...
cache.tentative.window.js
Source:cache.tentative.window.js
1// META: script=/common/get-host-info.sub.js2// META: script=/common/utils.js3// META: script=/common/dispatcher/dispatcher.js4// META: script=./resources/common.js5// With COEP:credentialless, requesting a resource without credentials MUST NOT6// return a response requested with credentials. This would be a security7// issue, since COEP:credentialless can be used to enable crossOriginIsolation.8//9// The test the behavior of the HTTP cache:10// 1. b.com stores cookie.11// 2. a.com(COEP:unsafe-none): request b.com's resource.12// 3. a.com(COEP:credentialless): request b.com's resource.13//14// The first time, the resource is requested with credentials. The response is15// served with Cache-Control: max-age=31536000. It enters the cache.16// The second time, the resource is requested without credentials. The response17// in the cache must not be returned.18const cookie_key = "coep_cache_key";19const cookie_value = "coep_cache_value";20const same_origin = get_host_info().HTTPS_ORIGIN;21const cross_origin = get_host_info().HTTPS_REMOTE_ORIGIN;22const GetCookie = (response) => {23 return parseCookies(JSON.parse(response))[cookie_key];24}25// "same_origin" document with COEP:unsafe-none.26const w_coep_none_token = token();27const w_coep_none_url = same_origin + executor_path + coep_none +28 `&uuid=${w_coep_none_token}`29const w_coep_none = window.open(w_coep_none_url);30add_completion_callback(() => w_coep_none.close());31// "same_origin" document with COEP:credentialles.32const w_coep_credentialless_token = token();33const w_coep_credentialless_url = same_origin + executor_path +34 coep_credentialless + `&uuid=${w_coep_credentialless_token}`35const w_coep_credentialless = window.open(w_coep_credentialless_url);36add_completion_callback(() => w_coep_credentialless.close());37const this_token = token();38// A request toward a "cross-origin" cacheable response.39const request_token = token();40const request_url = cacheableShowRequestHeaders(cross_origin, request_token);41promise_setup(async test => {42 await setCookie(cross_origin, cookie_key, cookie_value + cookie_same_site_none);43}, "Set cookie");44// The "same-origin" COEP:unsafe-none document fetchs a "cross-origin"45// resource. The request is sent with credentials.46promise_setup(async test => {47 send(w_coep_none_token, `48 await fetch("${request_url}", {49 mode : "no-cors",50 credentials: "include",51 });52 send("${this_token}", "Resource fetched");53 `);54 assert_equals(await receive(this_token), "Resource fetched");55 assert_equals(await receive(request_token).then(GetCookie), cookie_value);56}, "Cache a response requested with credentials");57// The "same-origin" COEP:credentialless document fetches the same resource58// without credentials. The HTTP cache must not be used. Instead a second59// request must be made without credentials.60promise_test(async test => {61 send(w_coep_credentialless_token, `62 await fetch("${request_url}", {63 mode : "no-cors",64 credentials: "include",65 });66 send("${this_token}", "Resource fetched");67 `);68 assert_equals(await receive(this_token), "Resource fetched");69 test.step_timeout(test.unreached_func("The HTTP cache has been used"), 1500);70 assert_equals(await receive(request_token).then(GetCookie), undefined);...
Using AI Code Generation
1var capabilities = {2 'goog:chromeOptions' : {3 }4};5var driver = new webdriver.Builder().withCapabilities(capabilities).build();6.then(function() {7 return driver.wait(webdriver.until.elementLocated(webdriver.By.id('result')), 3000);8}).then(function() {9 return driver.findElement(webdriver.By.id('result')).getText();10}).then(function(text) {11 console.log(text);12}).then(function() {13 return driver.quit();14}).catch(function(error) {15 console.error(error);16});
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!!