Best JavaScript code snippet using wpt
testcommon.js
Source:testcommon.js
1/*2Distributed under both the W3C Test Suite License [1] and the W3C33-clause BSD License [2]. To contribute to a W3C Test Suite, see the4policies and contribution forms [3].5[1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license6[2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license7[3] http://www.w3.org/2004/10/27-testcases8 */9'use strict';10const MS_PER_SEC = 1000;11// The recommended minimum precision to use for time values[1].12//13// [1] https://w3c.github.io/web-animations/#precision-of-time-values14const TIME_PRECISION = 0.0005; // ms15// Allow implementations to substitute an alternative method for comparing16// times based on their precision requirements.17if (!window.assert_times_equal) {18 window.assert_times_equal = (actual, expected, description) => {19 assert_approx_equals(actual, expected, TIME_PRECISION, description);20 };21}22// creates div element, appends it to the document body and23// removes the created element during test cleanup24function createDiv(test, doc) {25 return createElement(test, 'div', doc);26}27// creates element of given tagName, appends it to the document body and28// removes the created element during test cleanup29// if tagName is null or undefined, returns div element30function createElement(test, tagName, doc) {31 if (!doc) {32 doc = document;33 }34 const element = doc.createElement(tagName || 'div');35 doc.body.appendChild(element);36 test.add_cleanup(() => {37 element.remove();38 });39 return element;40}41// Creates a style element with the specified rules, appends it to the document42// head and removes the created element during test cleanup.43// |rules| is an object. For example:44// { '@keyframes anim': '' ,45// '.className': 'animation: anim 100s;' };46// or47// { '.className1::before': 'content: ""; width: 0px; transition: all 10s;',48// '.className2::before': 'width: 100px;' };49// The object property name could be a keyframes name, or a selector.50// The object property value is declarations which are property:value pairs51// split by a space.52function createStyle(test, rules, doc) {53 if (!doc) {54 doc = document;55 }56 const extraStyle = doc.createElement('style');57 doc.head.appendChild(extraStyle);58 if (rules) {59 const sheet = extraStyle.sheet;60 for (const selector in rules) {61 sheet.insertRule(`${selector}{${rules[selector]}}`,62 sheet.cssRules.length);63 }64 }65 test.add_cleanup(() => {66 extraStyle.remove();67 });68}69// Create a pseudo element70function createPseudo(test, type) {71 createStyle(test, { '@keyframes anim': '',72 [`.pseudo::${type}`]: 'animation: anim 10s; ' +73 'content: \'\';' });74 const div = createDiv(test);75 div.classList.add('pseudo');76 const anims = document.getAnimations();77 assert_true(anims.length >= 1);78 const anim = anims[anims.length - 1];79 assert_equals(anim.effect.target.parentElement, div);80 assert_equals(anim.effect.target.type, `::${type}`);81 anim.cancel();82 return anim.effect.target;83}84// Cubic bezier with control points (0, 0), (x1, y1), (x2, y2), and (1, 1).85function cubicBezier(x1, y1, x2, y2) {86 const xForT = t => {87 const omt = 1-t;88 return 3 * omt * omt * t * x1 + 3 * omt * t * t * x2 + t * t * t;89 };90 const yForT = t => {91 const omt = 1-t;92 return 3 * omt * omt * t * y1 + 3 * omt * t * t * y2 + t * t * t;93 };94 const tForX = x => {95 // Binary subdivision.96 let mint = 0, maxt = 1;97 for (let i = 0; i < 30; ++i) {98 const guesst = (mint + maxt) / 2;99 const guessx = xForT(guesst);100 if (x < guessx) {101 maxt = guesst;102 } else {103 mint = guesst;104 }105 }106 return (mint + maxt) / 2;107 };108 return x => {109 if (x == 0) {110 return 0;111 }112 if (x == 1) {113 return 1;114 }115 return yForT(tForX(x));116 };117}118function stepEnd(nsteps) {119 return x => Math.floor(x * nsteps) / nsteps;120}121function stepStart(nsteps) {122 return x => {123 const result = Math.floor(x * nsteps + 1.0) / nsteps;124 return (result > 1.0) ? 1.0 : result;125 };126}127function framesTiming(nframes) {128 return x => {129 const result = Math.floor(x * nframes) / (nframes - 1);130 return (result > 1.0 && x <= 1.0) ? 1.0 : result;131 };132}133function waitForAnimationFrames(frameCount) {134 return new Promise(resolve => {135 function handleFrame() {136 if (--frameCount <= 0) {137 resolve();138 } else {139 window.requestAnimationFrame(handleFrame); // wait another frame140 }141 }142 window.requestAnimationFrame(handleFrame);143 });144}145// Continually calls requestAnimationFrame until |minDelay| has elapsed146// as recorded using document.timeline.currentTime (i.e. frame time not147// wall-clock time).148function waitForAnimationFramesWithDelay(minDelay) {149 const startTime = document.timeline.currentTime;150 return new Promise(resolve => {151 (function handleFrame() {152 if (document.timeline.currentTime - startTime >= minDelay) {153 resolve();154 } else {155 window.requestAnimationFrame(handleFrame);156 }157 }());158 });159}160// Returns 'matrix()' or 'matrix3d()' function string generated from an array.161function createMatrixFromArray(array) {162 return (array.length == 16 ? 'matrix3d' : 'matrix') + `(${array.join()})`;163}164// Returns 'matrix3d()' function string equivalent to165// 'rotate3d(x, y, z, radian)'.166function rotate3dToMatrix3d(x, y, z, radian) {167 return createMatrixFromArray(rotate3dToMatrix(x, y, z, radian));168}169// Returns an array of the 4x4 matrix equivalent to 'rotate3d(x, y, z, radian)'.170// https://www.w3.org/TR/css-transforms-1/#Rotate3dDefined171function rotate3dToMatrix(x, y, z, radian) {172 const sc = Math.sin(radian / 2) * Math.cos(radian / 2);173 const sq = Math.sin(radian / 2) * Math.sin(radian / 2);174 // Normalize the vector.175 const length = Math.sqrt(x*x + y*y + z*z);176 x /= length;177 y /= length;178 z /= length;179 return [180 1 - 2 * (y*y + z*z) * sq,181 2 * (x * y * sq + z * sc),182 2 * (x * z * sq - y * sc),183 0,184 2 * (x * y * sq - z * sc),185 1 - 2 * (x*x + z*z) * sq,186 2 * (y * z * sq + x * sc),187 0,188 2 * (x * z * sq + y * sc),189 2 * (y * z * sq - x * sc),190 1 - 2 * (x*x + y*y) * sq,191 0,192 0,193 0,194 0,195 1196 ];197}198// Compare matrix string like 'matrix(1, 0, 0, 1, 100, 0)' with tolerances.199function assert_matrix_equals(actual, expected, description) {200 const matrixRegExp = /^matrix(?:3d)*\((.+)\)/;201 assert_regexp_match(actual, matrixRegExp,202 'Actual value is not a matrix')203 assert_regexp_match(expected, matrixRegExp,204 'Expected value is not a matrix');205 const actualMatrixArray =206 actual.match(matrixRegExp)[1].split(',').map(Number);207 const expectedMatrixArray =208 expected.match(matrixRegExp)[1].split(',').map(Number);209 assert_equals(actualMatrixArray.length, expectedMatrixArray.length,210 `dimension of the matrix: ${description}`);211 for (let i = 0; i < actualMatrixArray.length; i++) {212 assert_approx_equals(actualMatrixArray[i], expectedMatrixArray[i], 0.0001,213 `expected ${expected} but got ${actual}: ${description}`);214 }...
Using AI Code Generation
1var wptools = require('wptools');2var wiki = wptools.page('India');3wiki.get(function(err, info) {4 if (err) {5 console.log(err);6 } else {7 console.log(info.matrixRegExp('area_rank'));8 }9});10[ { area_rank: '3rd' } ]11### .tableRegExp()12var wptools = require('wptools');13var wiki = wptools.page('India');14wiki.get(function(err, info) {15 if (err) {16 console.log(err);17 } else {18 console.log(info.tableRegExp('area_rank'));19 }20});21[ { area_rank: '3rd' } ]22### .table()23var wptools = require('wptools');24var wiki = wptools.page('India');25wiki.get(function(err, info) {26 if (err) {27 console.log(err);28 } else {29 console.log(info.table('area_rank'));30 }31});32[ { area_rank: '3rd' } ]33### .infobox()34var wptools = require('wptools');35var wiki = wptools.page('India');36wiki.get(function(err, info) {37 if (err) {38 console.log(err);39 } else {40 console.log(info.infobox('area_rank'));41 }42});43{ area_rank: '3rd' }44### .links()45var wptools = require('wptools');46var wiki = wptools.page('India');47wiki.get(function(err
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Albert Einstein');3page.get(function(err, data) {4 if (err) {5 console.log('ERROR: ' + err);6 } else {7 var matrix = data.infobox.matrix;8 if (matrix) {9 var re = new RegExp('physics', 'i');10 console.log(matrix.regExp(re));11 }12 }13});14{ '0': 'physics' }
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Albert Einstein');3page.get(function(err, resp) {4 var infobox = resp['infobox'];5 var key = 'alma_mater';6 var result = infobox.matrixRegExp(key, /.*\|(.*)/);7 console.log(result);8});9var wptools = require('wptools');10var page = wptools.page('Albert Einstein');11page.get(function(err, resp) {12 var infobox = resp['infobox'];13 var key = 'alma_mater';14 var result = infobox.matrix(key);15 console.log(result);16});17var wptools = require('wptools');18var page = wptools.page('Albert Einstein');19page.get(function(err, resp) {20 var infobox = resp['infobox'];21 var key = 'alma_mater';22 var result = infobox.matrix(key, 1);23 console.log(result);24});25var wptools = require('wptools');26var page = wptools.page('Albert Einstein');27page.get(function(err, resp) {28 var infobox = resp['infobox'];29 var key = 'alma_mater';30 var result = infobox.matrix(key, 2);31 console.log(result);32});33var wptools = require('wptools');34var page = wptools.page('Albert Einstein');35page.get(function(err, resp) {36 var infobox = resp['infobox'];37 var key = 'alma_mater';38 var result = infobox.matrix(key, 3);39 console.log(result);40});41var wptools = require('wptools');42var page = wptools.page('Albert Einstein');
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Barack Obama');3page.get(function(err, page) {4 console.log(page.data.infobox.matrixRegExp(/(education|occupation)/));5});6Infobox.prototype.matrixRegExp = function(re) {7 var matrix = {};8 var data = this.data;9 var key = null;10 var value = null;11 var match = false;12 for (key in data) {13 match = re.exec(key);14 if (match) {15 value = data[key];16 matrix[key] = value;17 }18 }19 return matrix;20};21Page.prototype.get = function(callback) {22 var self = this;23 var url = this.url;24 var data = this.data;25 var options = this.options;26 var method = this.method;27 var infobox = new Infobox();28 request[method](url, options, function(err, response, body) {29 if (err) {30 return callback(err);31 }32 if (response.statusCode === 200) {33 data = parse(body, url);34 infobox.data = data.infobox;35 self.data = data;36 self.infobox = infobox;37 return callback(null, self);38 } else {39 return callback(new Error('HTTP status code ' + response.statusCode));40 }41 });42};43function parse(body, url) {44 var data = {};45 var infobox = {};46 var html = body;47 var $ = cheerio.load(html);48 var $infobox = $('table.infobox');49 var $table = $('table.wikitable');50 var $content = $('#mw-content-text');51 var $h2 = $content.find('h2');52 var $p = $content.find('p');53 var $h3 = $content.find('h3');54 var $li = $content.find('li');55 var $a = $content.find('a');56 var $img = $content.find('img');57 var $infobox_tr = $infobox.find('tr');
Using AI Code Generation
1var wptools = require('wptools');2var re = wptools.matrixRegExp('foo', 'bar');3console.log(re);4var wptools = require('wptools');5var re = wptools.matrixRegExp('foo', 'bar');6console.log(re);7var wptools = require('wptools');8var re = wptools.matrixRegExp('foo', 'bar');9console.log(re);10var wptools = require('wptools');11var re = wptools.matrixRegExp('foo', 'bar');12console.log(re);13var wptools = require('wptools');14var re = wptools.matrixRegExp('foo', 'bar');15console.log(re);16var wptools = require('wptools');17var re = wptools.matrixRegExp('foo', 'bar');18console.log(re);19var wptools = require('wptools');20var re = wptools.matrixRegExp('foo', 'bar');21console.log(re);
Using AI Code Generation
1var wptools = require('wptools');2var result = wptools.matrixRegExp('My name is {{PAGENAME}} and I am {{AGE}} years old. I live in {{CITY}}', {3});4console.log(result);5var wptools = require('wptools');6var result = wptools.matrixReplace('My name is {{PAGENAME}} and I am {{AGE}} years old. I live in {{CITY}}', {7});8console.log(result);9var wptools = require('wptools');10var result = wptools.matrixReplaceAll('My name is {{PAGENAME}} and I am {{AGE}} years old. I live in {{CITY}} {{CITY}}', {11});12console.log(result);
Using AI Code Generation
1var wptools = require('wptools');2var matrixRegExp = wptools.matrixRegExp;3var text = 'A test string with a matrix in it [[Matrix:1|2|3|4|5|6|7|8|9]]';4var matrix = matrixRegExp.exec(text);5console.log(matrix);6var wptools = require('wptools');7var matrixRegExp = wptools.matrixRegExp;8var text = 'A test string with a matrix in it [[Matrix:1|2|3|4|5|6|7|8|9]]';9var matrix = matrixRegExp.exec(text);10console.log(matrix);11var wptools = require('wptools');12var matrixRegExp = wptools.matrixRegExp;13var text = 'A test string with a matrix in it [[Matrix:1|2|3|4|5|6|7|8|9]]';14var matrix = matrixRegExp.exec(text);15console.log(matrix);16var wptools = require('wptools');
Using AI Code Generation
1var wptools = require('wptools');2var regEx = /(\d+)\s*([a-z]+)\s*([a-z]+)/i;3var testString = "1st line 2nd line 3rd line 4th line";4var result = wptools.matrixRegExp(testString, regEx);5console.log(result);6var regEx = /(\d+)\s*([a-z]+)\s*([a-z]+)\s*([a-z]+)/i;7var testString = "1st line 2nd line 3rd line 4th line";8var result = regEx.exec(testString);9console.log(result);
Using AI Code Generation
1var Wiki = require('wptools');2var wiki = new Wiki();3wiki.page('Matrix').then(function(page){4 console.log(page.matrixRegExp(/(S|s)tar (T|t)rek/));5});6var Wiki = require('wptools');7var wiki = new Wiki();8wiki.page('Matrix').then(function(page){9 console.log(page.matrixRegExp(/(S|s)tar (T|t)rek/, 1));10});11var Wiki = require('wptools');12var wiki = new Wiki();13wiki.page('Matrix').then(function(page){14 console.log(page.matrixRegExp(/(S|s)tar (T|t)rek/, 1, 0));15});16var Wiki = require('wptools');17var wiki = new Wiki();18wiki.page('Matrix').then(function(page){19 console.log(page.matrixRegExp(/(S|s)tar (T|t)rek/, 1, 1));20});21var Wiki = require('wptools');22var wiki = new Wiki();23wiki.page('Matrix').then(function(page){24 console.log(page.matrixRegExp(/(S|s)tar (T|t)rek/, 1, 2));25});
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!!