Best JavaScript code snippet using wpt
auto-track.test.ts
Source:auto-track.test.ts
...3const sleep = (time: number): Promise<void> =>4 new Promise((resolve) => {5 setTimeout(resolve, time)6 })7async function resolveWhen(8 condition: () => boolean,9 timeout?: number10): Promise<void> {11 return new Promise((resolve, _reject) => {12 if (condition()) {13 resolve()14 return15 }16 const check = () =>17 setTimeout(() => {18 if (condition()) {19 resolve()20 } else {21 check()22 }23 }, timeout)24 check()25 })26}27describe('track helpers', () => {28 describe('trackLink', () => {29 // eslint-disable-next-line @typescript-eslint/no-explicit-any30 let link: any31 let wrap: SVGSVGElement32 let svg: SVGAElement33 let analytics = new Analytics({ writeKey: 'foo' })34 let mockTrack = jest.spyOn(analytics, 'track')35 beforeEach(() => {36 analytics = new Analytics({ writeKey: 'foo' })37 // @ts-ignore38 global.jQuery = require('jquery')39 const jsd = new JSDOM('', {40 runScripts: 'dangerously',41 resources: 'usable',42 })43 // eslint-disable-next-line no-global-assign44 document = jsd.window.document45 jest.spyOn(console, 'error').mockImplementationOnce(() => {})46 document.querySelector('html')!.innerHTML = `47 <html>48 <body>49 <a href='foo.com' id='foo'></a>50 <div id='bar'>51 <div>52 <a href='bar.com'></a>53 </div>54 </div>55 </body>56 </html>`57 link = document.getElementById('foo')58 wrap = document.createElementNS('http://www.w3.org/2000/svg', 'svg')59 svg = document.createElementNS('http://www.w3.org/2000/svg', 'a')60 wrap.appendChild(svg)61 document.body.appendChild(wrap)62 jest.spyOn(window, 'location', 'get').mockReturnValue({63 ...window.location,64 })65 mockTrack = jest.spyOn(analytics, 'track')66 // We need to mock the track function for the .catch() call not to break when testing67 // eslint-disable-next-line @typescript-eslint/unbound-method68 mockTrack.mockImplementation(Analytics.prototype.track)69 })70 it('should respect options object', async () => {71 await analytics.trackLink(72 link!,73 'foo',74 {},75 { context: { ip: '0.0.0.0' } }76 )77 link.click()78 expect(mockTrack).toHaveBeenCalledWith(79 'foo',80 {},81 { context: { ip: '0.0.0.0' } }82 )83 })84 it('should stay on same page with blank href', async () => {85 link.href = ''86 await analytics.trackLink(link!, 'foo')87 link.click()88 expect(mockTrack).toHaveBeenCalled()89 expect(window.location.href).toBe('http://localhost/')90 })91 it('should work with nested link', async () => {92 const nested = document.getElementById('bar')93 await analytics.trackLink(nested, 'foo')94 nested!.click()95 expect(mockTrack).toHaveBeenCalled()96 await resolveWhen(() => window.location.href === 'bar.com')97 expect(window.location.href).toBe('bar.com')98 })99 it('should make a track call', async () => {100 await analytics.trackLink(link!, 'foo')101 link.click()102 expect(mockTrack).toHaveBeenCalled()103 })104 it('should still navigate even if the track call fails', async () => {105 mockTrack.mockClear()106 let rejected = false107 mockTrack.mockImplementationOnce(() => {108 rejected = true109 return Promise.reject(new Error('boo!'))110 })111 const nested = document.getElementById('bar')112 await analytics.trackLink(nested, 'foo')113 nested!.click()114 await resolveWhen(() => rejected)115 await resolveWhen(() => window.location.href === 'bar.com')116 expect(window.location.href).toBe('bar.com')117 })118 it('should still navigate even if the track call times out', async () => {119 mockTrack.mockClear()120 let timedOut = false121 mockTrack.mockImplementationOnce(async () => {122 await sleep(600)123 timedOut = true124 return Promise.resolve() as any125 })126 const nested = document.getElementById('bar')127 await analytics.trackLink(nested, 'foo')128 nested!.click()129 await resolveWhen(() => window.location.href === 'bar.com')130 expect(window.location.href).toBe('bar.com')131 expect(timedOut).toBe(false)132 await resolveWhen(() => timedOut)133 })134 it('should accept a jquery object for an element', async () => {135 const $link = jQuery(link)136 await analytics.trackLink($link, 'foo')137 link.click()138 expect(mockTrack).toBeCalled()139 })140 it('accepts array of elements', async () => {141 const links = [link, link]142 await analytics.trackLink(links, 'foo')143 link.click()144 expect(mockTrack).toHaveBeenCalled()145 })146 it('should send an event and properties', async () => {147 await analytics.trackLink(link, 'event', { property: true })148 link.click()149 expect(mockTrack).toBeCalledWith('event', { property: true }, {})150 })151 it('should accept an event function', async () => {152 function event(el: Element): string {153 return el.nodeName154 }155 await analytics.trackLink(link, event, { foo: 'bar' })156 link.click()157 expect(mockTrack).toBeCalledWith('A', { foo: 'bar' }, {})158 })159 it('should accept a properties function', async () => {160 function properties(el: Record<string, string>): Record<string, string> {161 return { type: el.nodeName }162 }163 await analytics.trackLink(link, 'event', properties)164 link.click()165 expect(mockTrack).toBeCalledWith('event', { type: 'A' }, {})166 })167 it('should load an href on click', async () => {168 link.href = '#test'169 await analytics.trackLink(link, 'foo')170 link.click()171 await resolveWhen(() => window.location.href === '#test')172 expect(global.window.location.href).toBe('#test')173 })174 it('should only navigate after the track call has been completed', async () => {175 link.href = '#test'176 await analytics.trackLink(link, 'foo')177 link.click()178 await resolveWhen(() => mockTrack.mock.calls.length === 1)179 await resolveWhen(() => window.location.href === '#test')180 expect(global.window.location.href).toBe('#test')181 })182 it('should support svg .href attribute', async () => {183 svg.setAttribute('href', '#svg')184 await analytics.trackLink(svg, 'foo')185 const clickEvent = new Event('click')186 svg.dispatchEvent(clickEvent)187 await resolveWhen(() => window.location.href === '#svg')188 expect(global.window.location.href).toBe('#svg')189 })190 it('should fallback to getAttributeNS', async () => {191 svg.setAttributeNS('http://www.w3.org/1999/xlink', 'href', '#svg')192 await analytics.trackLink(svg, 'foo')193 const clickEvent = new Event('click')194 svg.dispatchEvent(clickEvent)195 await resolveWhen(() => window.location.href === '#svg')196 expect(global.window.location.href).toBe('#svg')197 })198 it('should support xlink:href', async () => {199 svg.setAttribute('xlink:href', '#svg')200 await analytics.trackLink(svg, 'foo')201 const clickEvent = new Event('click')202 svg.dispatchEvent(clickEvent)203 await resolveWhen(() => window.location.href === '#svg')204 expect(global.window.location.href).toBe('#svg')205 })206 it('should not load an href for a link with a blank target', async () => {207 link.href = '/base/test/support/mock.html'208 link.target = '_blank'209 await analytics.trackLink(link, 'foo')210 link.click()211 await sleep(300)212 expect(global.window.location.href).not.toBe('#test')213 })214 })215 describe('trackForm', () => {216 // eslint-disable-next-line @typescript-eslint/no-explicit-any217 let form: any...
index.unit.test.js
Source:index.unit.test.js
...19 });20 describe('when called with no arguments', function () {21 let promise;22 beforeEach(function () {23 promise = resolveWhen();24 });25 it('should resolve', function () {26 return promise;27 });28 });29 describe('when called with condition that eventually becomes true', function () {30 let condition = false;31 let promise;32 beforeEach(function () {33 promise = resolveWhen(() => condition);34 setTimeout(() => { condition = true }, 10000);35 });36 it('should resolve once condition is met', function () {37 clock.tick(10000);38 return promise;39 });40 });41 describe('when called with condition that always fails', function () {42 const resolvedIndicator = Symbol('resolvedIndicator');43 describe('and no max option', function () {44 const forever = 600000; // Defining 'forever' as 10 minutes45 let promise;46 beforeEach(function () {47 promise = Promise.race([48 resolveWhen(() => false),49 new Promise(resolve => setTimeout(resolve, forever, resolvedIndicator)),50 ]);51 });52 it('should hang forever', function () {53 clock.tick(forever);54 return promise.then((val) => {55 if (val !== resolvedIndicator) {56 throw Error('Promise should not have resolved');57 }58 }, () => {59 throw Error('Promise should not have rejected');60 });61 });62 });63 describe('and max option', function () {64 const max = 30;65 let promise;66 beforeEach(function () {67 promise = resolveWhen(() => false, { max });68 });69 it('should not reject before max * interval', function () {70 const timeLimit = resolveWhen.defaults.interval * (max - 1);71 const testPromise = Promise.race([72 promise,73 new Promise(resolve => setTimeout(resolve, timeLimit, resolvedIndicator)),74 ]);75 clock.tick(timeLimit);76 return testPromise77 .then((val) => {78 if (val !== resolvedIndicator) {79 throw Error('Promise should not have resolved');80 }81 })...
index.js
Source:index.js
...5 * @param {Object} options6 * @param {Number} options.max - The maximum number of times that `condition` should run7 * @param {Number} options.interval - Time, in milliseconds, to wait between running `condition`8 */9function resolveWhen(condition = () => true, { max, interval = 50 } = {}) {10 return new Promise((resolve, reject) => {11 (function poll(iteration = 1) {12 if (condition()) return resolve();13 if (iteration === max) return reject(Error(`Maximum iterations (${max}) exceeded`));14 setTimeout(poll, interval, iteration + 1);15 }());16 });17}18Object.defineProperty(resolveWhen, 'defaults', {19 value: Object.create(Object.prototype, {20 interval: { value: 50 },21 }),22});23module.exports = resolveWhen;
Using AI Code Generation
1var wpt = require('wpt.js');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) {4 console.log('Error: ' + err);5 } else {6 wpt.resolveWhenTestDone(data.data.testId, function(err, data) {7 if (err) {8 console.log('Error: ' + err);9 } else {10 console.log(data);11 }12 });13 }14});15### getTestStatus(testId, callback)16var wpt = require('wpt.js');17var wpt = new WebPageTest('www.webpagetest.org');18wpt.getTestStatus('140109_7H_1W', function(err, data) {19 if (err) {20 console.log('Error: ' + err);21 } else {22 console.log(data);23 }24});25### getLocations(callback)26var wpt = require('wpt.js');27var wpt = new WebPageTest('www.webpagetest.org');28wpt.getLocations(function(err, data) {29 if (err) {30 console.log('Error: ' + err);31 } else {32 console.log(data);33 }34});35### getLocations(callback)36var wpt = require('wpt.js');37var wpt = new WebPageTest('www.webpagetest.org');38wpt.getLocations(function(err, data) {39 if (err) {40 console.log('Error: ' + err);41 } else {42 console.log(data);43 }44});45### getTesters(callback)46var wpt = require('wpt.js');47var wpt = new WebPageTest('www.webpagetest.org');
Using AI Code Generation
1var wptools = require('wptools');2var options = {3};4var page = wptools.page('Eiffel Tower', options);5page.resolveWhen('infobox', function(err, resp) {6 console.log(resp);7});8page.resolveWhen('coordinates', function(err, resp) {9 console.log(resp);10});11page.resolveWhen('categories', function(err, resp) {12 console.log(resp);13});14page.resolveWhen('links', function(err, resp) {15 console.log(resp);16});17page.resolveWhen('images', function(err, resp) {18 console.log(resp);19});20page.resolveWhen('langlinks', function(err, resp) {21 console.log(resp);22});23page.resolveWhen('externallinks', function(err, resp) {24 console.log(resp);25});26page.resolveWhen('templates', function(err, resp) {27 console.log(resp);28});29page.resolveWhen('sections', function(err, resp) {30 console.log(resp);31});32page.resolveWhen('summary', function(err, resp) {33 console.log(resp);34});35page.resolveWhen('text', function(err, resp) {36 console.log(resp);37});38page.resolveWhen('html', function(err, resp) {39 console.log(resp);40});41page.resolveWhen('references', function(err, resp) {42 console.log(resp);43});44page.resolveWhen('revisions', function(err, resp) {45 console.log(resp);46});47page.resolveWhen('info', function(err, resp) {48 console.log(resp);49});50page.resolveWhen('imageinfo', function(err, resp) {51 console.log(resp);52});53page.resolveWhen('imageusage', function(err, resp) {54 console.log(resp);55});56page.resolveWhen('categorymembers', function(err, resp) {57 console.log(resp);58});59page.resolveWhen('extlinks', function(err, resp) {60 console.log(resp);61});62page.resolveWhen('iwlinks', function(err, resp) {63 console.log(resp);64});65page.resolveWhen('random', function(err, resp) {66 console.log(resp);67});68page.resolveWhen('search', function(err, resp) {69 console.log(resp);70});71page.resolveWhen('nearby', function(err, resp) {72 console.log(resp);73});74page.resolveWhen('geosearch', function(err, resp) {
Using AI Code Generation
1resolveWhen(function() {2 return new Promise(function(resolve, reject) {3 setTimeout(function() {4 resolve(1);5 }, 1000);6 });7}, "Test 1");8resolveWhen(function() {9 return new Promise(function(resolve, reject) {10 setTimeout(function() {11 resolve(2);12 }, 2000);13 });14}, "Test 2");15resolveWhen(function() {16 return new Promise(function(resolve, reject) {17 setTimeout(function() {18 resolve(3);19 }, 3000);20 });21}, "Test 3");22resolveWhen(function() {23 return new Promise(function(resolve, reject) {24 setTimeout(function() {25 resolve(4);26 }, 4000);27 });28}, "Test 4");29resolveWhen(function() {30 return new Promise(function(resolve, reject) {31 setTimeout(function() {32 resolve(5);33 }, 5000);34 });35}, "Test 5");36resolveWhen(function() {37 return new Promise(function(resolve, reject) {38 setTimeout(function() {39 resolve(6);40 }, 6000);41 });42}, "Test 6");43resolveWhen(function() {44 return new Promise(function(resolve, reject) {45 setTimeout(function() {46 resolve(7);47 }, 7000);48 });49}, "Test 7");50resolveWhen(function() {51 return new Promise(function(resolve, reject) {52 setTimeout(function() {53 resolve(8);54 }, 8000);55 });56}, "Test 8");57resolveWhen(function() {58 return new Promise(function(resolve, reject) {59 setTimeout(function() {60 resolve(9);61 }, 9000);62 });63}, "Test 9");64resolveWhen(function() {65 return new Promise(function(resolve, reject) {66 setTimeout(function() {67 resolve(10);68 }, 10000);69 });70}, "Test 10");71resolveWhen(function() {72 return new Promise(function(resolve, reject) {73 setTimeout(function() {74 resolve(11);75 }, 11000);76 });77}, "Test 11");78resolveWhen(function() {79 return new Promise(function(resolve, reject) {80 setTimeout(function() {81 resolve(12);82 }, 12000);83 });84}, "Test 12");85resolveWhen(function() {86 return new Promise(function(resolve, reject) {87 setTimeout(function() {88 resolve(13);89 }, 130
Using AI Code Generation
1const wptools = require('wptools');2const wiki = wptools('Barack Obama');3wiki.resolveWhen('infobox', function(err, infobox) {4 console.log(infobox);5});6const wptools = require('wptools');7const wiki = wptools('Barack Obama');8wiki.resolveWhen('infobox', function(err, infobox) {9 console.log(infobox);10});11const wptools = require('wptools');12const wiki = wptools('Barack Obama');13wiki.resolveWhen('infobox', function(err, infobox) {14 console.log(infobox);15});16const wptools = require('wptools');17const wiki = wptools('Barack Obama');18wiki.resolveWhen('infobox', function(err, infobox) {19 console.log(infobox);20});21const wptools = require('wptools');22const wiki = wptools('Barack Obama');23wiki.resolveWhen('infobox', function(err, infobox) {24 console.log(infobox);25});26const wptools = require('wptools');27const wiki = wptools('Barack Obama');28wiki.resolveWhen('infobox', function(err, infobox) {29 console.log(infobox);30});
Using AI Code Generation
1var wptools = require('wptools');2 if (err) {3 console.log(err);4 } else {5 console.log(response);6 }7});8### .getInfobox(callback)9var wptools = require('wptools');10 if (err) {11 console.log(err);12 } else {13 console.log(response);14 }15});16### .getCategories(callback)17var wptools = require('wptools');18 if (err) {19 console.log(err);20 } else {21 console.log(response);22 }23});24### .getCoordinates(callback)25var wptools = require('wptools');26 if (err) {27 console.log(err);28 } else {29 console.log(response);30 }31});32### .getExtract(callback)33var wptools = require('wptools');
Using AI Code Generation
1var Wpt = require('wpt');2var wpt = new Wpt('A.f0c6a1a8f2c2d7e2a0a0c7e8c1d1e7f1');3 if (err) return console.error(err);4 console.log('Test submitted. Polling until test complete...');5 wpt.pollResults(data.data.testId, 5000, function(err, data) {6 if (err) return console.error(err);7 console.log('Test complete. View your test results at:');8 console.log(data.data.summary);9 });10});
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!!