Best JavaScript code snippet using wpt
utils.sub.js
Source:utils.sub.js
1/**2 * Utilities for initiating prefetch via speculation rules.3 */4// Resolved URL to find this script.5const SR_PREFETCH_UTILS_URL = new URL(document.currentScript.src, document.baseURI);6// Hostname for cross origin urls.7const PREFETCH_PROXY_BYPASS_HOST = "{{hosts[alt][]}}";8class PrefetchAgent extends RemoteContext {9 constructor(uuid, t) {10 super(uuid);11 this.t = t;12 }13 getExecutorURL(options = {}) {14 let {hostname, protocol, ...extra} = options;15 let params = new URLSearchParams({uuid: this.context_id, ...extra});16 let url = new URL(`executor.sub.html?${params}`, SR_PREFETCH_UTILS_URL);17 if(hostname !== undefined) {18 url.hostname = hostname;19 }20 if(protocol !== undefined) {21 url.protocol = protocol;22 url.port = protocol === "https" ? "{{ports[https][0]}}" : "{{ports[http][0]}}";23 }24 return url;25 }26 // Requests prefetch via speculation rules.27 //28 // In the future, this should also use browser hooks to force the prefetch to29 // occur despite heuristic matching, etc., and await the completion of the30 // prefetch.31 async forceSinglePrefetch(url, extra = {}) {32 await this.execute_script((url, extra) => {33 insertSpeculationRules({ prefetch: [{source: 'list', urls: [url], ...extra}] });34 }, [url, extra]);35 return new Promise(resolve => this.t.step_timeout(resolve, 2000));36 }37 async navigate(url) {38 await this.execute_script((url) => {39 window.executor.suspend(() => {40 location.href = url;41 });42 }, [url]);43 assert_equals(44 await this.execute_script(() => location.href),45 url.toString(),46 "expected navigation to reach destination URL");47 await this.execute_script(() => {});48 }49 async getRequestHeaders() {50 return this.execute_script(() => requestHeaders);51 }52}53// Produces n URLs with unique UUIDs which will record when they are prefetched.54function getPrefetchUrlList(n) {55 let urls = [];56 for (let i=0; i<n; i++) {57 let params = new URLSearchParams({uuid: token()});58 urls.push(new URL(`prefetch.py?${params}`, SR_PREFETCH_UTILS_URL));59 }60 return urls;61}62async function isUrlPrefetched(url) {63 let response = await fetch(url);64 assert_true(response.ok);65 return response.json();66}67// Must also include /common/utils.js and /common/dispatcher/dispatcher.js to use this.68async function spawnWindow(t, extra = {}) {69 let agent = new PrefetchAgent(token(), t);70 let w = window.open(agent.getExecutorURL(), extra);71 t.add_cleanup(() => w.close());72 return agent;73}74function insertSpeculationRules(body) {75 let script = document.createElement('script');76 script.type = 'speculationrules';77 script.textContent = JSON.stringify(body);78 document.head.appendChild(script);79}80function assert_prefetched (requestHeaders, description) {81 assert_in_array(requestHeaders.purpose, ["", "prefetch"], "The vendor-specific header Purpose, if present, must be 'prefetch'.");82 assert_equals(requestHeaders.sec_purpose, "prefetch", description);83}84function assert_not_prefetched (requestHeaders, description){85 assert_equals(requestHeaders.purpose, "", description);86 assert_equals(requestHeaders.sec_purpose, "", description);...
Using AI Code Generation
1var assert_prefetched = require('./assert_prefetched.js');2var assert = require('assert');3var webdriver = require('selenium-webdriver');4var By = webdriver.By;5var until = webdriver.until;6var driver = new webdriver.Builder()7 .forBrowser('chrome')8 .build();9driver.findElement(By.name('q')).sendKeys('webdriver');10driver.findElement(By.name('btnK')).click();11driver.wait(until.titleIs('webdriver - Google Search'), 1000);12driver.quit();13var assert = require('assert');14var webdriver = require('selenium-webdriver');15var By = webdriver.By;16var until = webdriver.until;17module.exports = function(driver, url) {18 driver.get(url);19 driver.wait(until.elementLocated(By.tagName('body')), 1000);20 var body = driver.findElement(By.tagName('body'));21 body.getAttribute('innerHTML').then(function(value) {22 assert.equal(value, 'prefetched');23 });24};
Using AI Code Generation
1async_test(function(test) {2 var iframe = document.createElement("iframe");3 iframe.src = "/common/blank.html";4 iframe.onload = test.step_func(function() {5 var link = iframe.contentDocument.createElement("link");6 link.rel = "prefetch";7 iframe.contentDocument.head.appendChild(link);8 setTimeout(test.step_func(function() {9 assert_prefetched(link.href, test.step_func(function(result) {10 assert_true(result, "Resource is prefetched");11 test.done();12 }));13 }), 1000);14 });15 document.body.appendChild(iframe);16}, "Test to check if the resource is prefetched or not");17async_test(function(test) {18 var iframe = document.createElement("iframe");19 iframe.src = "/common/blank.html";20 iframe.onload = test.step_func(function() {21 var link = iframe.contentDocument.createElement("link");22 link.rel = "prefetch";23 iframe.contentDocument.head.appendChild(link);24 setTimeout(test.step_func(function() {25 assert_prefetched(link.href, test.step_func(function(result) {26 assert_false(result, "Resource is not prefetched");27 test.done();28 }));29 }), 1000);30 });31 document.body.appendChild(iframe);32}, "Test to check if the resource is prefetched or not");33async_test(function(test
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!!