Best JavaScript code snippet using wpt
run-single-wpt.js
Source:run-single-wpt.js
1"use strict";2/* eslint-disable no-console */3const path = require("path");4const { URL } = require("url");5const { specify } = require("mocha-sugar-free");6const { inBrowserContext } = require("./util.js");7const { JSDOM, VirtualConsole } = require("jsdom");8const ResourceLoader = require("jsdom/lib/jsdom/browser/resources/resource-loader");9const {10 computeAccessibleName,11 computeAccessibleDescription,12} = require("../../dist/");13const reporterPathname = "/resources/testharnessreport.js";14module.exports = (urlPrefixFactory) => {15 if (inBrowserContext()) {16 return () => {17 // TODO: browser support for running WPT18 };19 }20 return (testPath, title = testPath, expectFail) => {21 specify({22 title,23 expectPromise: true,24 // WPT also takes care of timeouts (maximum 60 seconds), this is an extra failsafe:25 timeout: 1000,26 slow: 10000,27 skipIfBrowser: true,28 fn() {29 return createJSDOM(urlPrefixFactory(), testPath, expectFail);30 },31 });32 };33};34class CustomResourceLoader extends ResourceLoader {35 constructor() {36 super({ strictSSL: false });37 }38 fetch(urlString, options) {39 const url = new URL(urlString);40 if (url.pathname === reporterPathname) {41 return Promise.resolve(Buffer.from("window.shimTest();", "utf-8"));42 } else if (url.pathname.startsWith("/resources/")) {43 // When running to-upstream tests, the server doesn't have a /resources/ directory.44 // So, always go to the one in ./tests.45 // The path replacement accounts for a rewrite performed by the WPT server:46 // https://github.com/w3c/web-platform-tests/blob/master/tools/serve/serve.py#L27147 const filePath = path48 .resolve(__dirname, "../wpt" + url.pathname)49 .replace(50 "/resources/WebIDLParser.js",51 "/resources/webidl2/lib/webidl2.js"52 );53 return super.fetch(`file://${filePath}`, options);54 } else if (url.pathname === "/wai-aria/scripts/ATTAcomm.js") {55 const filePath = path.resolve(__dirname, "./ATTAcomm.js");56 return super.fetch(`file://${filePath}`, options);57 }58 return super.fetch(urlString, options);59 }60}61function createJSDOM(urlPrefix, testPath, expectFail) {62 const unhandledExceptions = [];63 const doneErrors = [];64 let allowUnhandledExceptions = false;65 const virtualConsole = new VirtualConsole().sendTo(console, {66 omitJSDOMErrors: true,67 });68 virtualConsole.on("jsdomError", (e) => {69 if (e.type === "unhandled exception" && !allowUnhandledExceptions) {70 unhandledExceptions.push(e);71 // Some failing tests make a lot of noise.72 // There's no need to log these messages73 // for errors we're already aware of.74 if (!expectFail) {75 console.error(e.detail.stack);76 }77 }78 });79 return JSDOM.fromURL(urlPrefix + testPath, {80 runScripts: "dangerously",81 virtualConsole,82 resources: new CustomResourceLoader(),83 pretendToBeVisual: true,84 storageQuota: 100000, // Filling the default quota takes about a minute between two WPTs85 }).then((dom) => {86 const { window } = dom;87 return new Promise((resolve, reject) => {88 const errors = [];89 window.computeAccessibleName = computeAccessibleName;90 window.computeAccessibleDescription = computeAccessibleDescription;91 window.shimTest = () => {92 const oldSetup = window.setup;93 window.setup = () => {94 // noop, otherwise failing tests just slowly timeout95 };96 window.add_result_callback((test) => {97 if (test.status === 1) {98 errors.push(99 `Failed in "${test.name}": \n${test.message}\n\n${test.stack}`100 );101 } else if (test.status === 2) {102 errors.push(103 `Timeout in "${test.name}": \n${test.message}\n\n${test.stack}`104 );105 } else if (test.status === 3) {106 errors.push(107 `Uncompleted test "${test.name}": \n${test.message}\n\n${test.stack}`108 );109 }110 });111 window.add_completion_callback((tests, harnessStatus) => {112 // This needs to be delayed since some tests do things even after calling done().113 process.nextTick(() => {114 window.close();115 });116 if (harnessStatus.status === 2) {117 errors.push(118 new Error(`test harness should not timeout: ${testPath}`)119 );120 }121 errors.push(...doneErrors);122 errors.push(...unhandledExceptions);123 if (errors.length === 0 && expectFail) {124 reject(125 new Error(`126 Hey, did you fix a bug? This test used to be failing, but during127 this run there were no errors. If you have fixed the issue covered128 by this test, you can edit the "to-run.yaml" file and remove the line129 containing this test. Thanks!130 `)131 );132 } else if (errors.length === 1 && !expectFail) {133 reject(new Error(errors[0]));134 } else if (errors.length && !expectFail) {135 reject(136 new Error(137 `${errors.length} errors in test:\n\n${errors.join("\n")}`138 )139 );140 } else {141 resolve();142 }143 });144 };145 });146 });...
ATTAcomm.js
Source:ATTAcomm.js
1const implementation = "ATK";2const descriptionPropertyName = {3 ATK: "description",4};5const namePropertyName = {6 ATK: "name",7};8/**9 * Shim of wpt/wai-aria/scripts/ATTAcomm or rather10 * a minimal implementation of https://spec-ops.github.io/atta-api/11 * to pass wpt/accname12 */13class ATTAcomm {14 constructor({ steps, title }) {15 test(() => {16 const element = document.getElementById("test");17 for (const step of steps) {18 const { test } = step;19 const assertions = test[implementation];20 for (const assertion of assertions) {21 const [matcher, name, equality, expected] = assertion;22 if (matcher === "property") {23 if (name === namePropertyName[implementation]) {24 const actual = computeAccessibleName(element);25 if (equality === "is") {26 assert_equals(actual, expected);27 continue;28 }29 } else if (name === descriptionPropertyName[implementation]) {30 const actual = computeAccessibleDescription(element);31 if (equality === "is") {32 assert_equals(actual, expected);33 continue;34 }35 }36 }37 throw new Error(`Don't know how to handle this assertion`);38 }39 }40 done();41 }, title);42 }43}...
Using AI Code Generation
1var wpt = require('webpagetest');2var client = wpt('www.webpagetest.org');3var options = {4};5client.runTest(url, options, function(err, data) {6 console.log(data);7});
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var path = require('path');4var async = require('async');5var request = require('request');6var wiki = new wptools.page('Lionel Messi');7wiki.info(function(err, resp) {8 console.log(resp);9});
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.5a3e5e9f5f6b1a6d2a6a1b7f8c6b5a7');3var wpt = require('webpagetest');4var wpt = new WebPageTest('www.webpagetest.org', 'A.5a3e5e9f5f6b1a6d2a6a1b7f8c6b5a7');5var wpt = require('webpagetest');6var wpt = new WebPageTest('www.webpagetest.org', 'A.5a3e5e9f5f6b1a6d2a6a1b7f8c6b5a7');7var wpt = require('webpagetest');8var wpt = new WebPageTest('www.webpagetest.org', 'A.5a3e5e9f5f6b1a6d2a6a1b7f8c6b5a7');9var wpt = require('webpagetest');10var wpt = new WebPageTest('www.webpagetest.org', 'A.5a3e5e9f5f6b1a6d2a6a1b7f8c6b5a7');11var wpt = require('webpagetest');12var wpt = new WebPageTest('www.webpagetest.org', 'A.5a3e5e9f5f6b1a6d2a6a1b7f8c6b5a7');
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!!