Best JavaScript code snippet using puppeteer
coverage.spec.js
Source:coverage.spec.js
...18 const {it, fit, xit} = testRunner;19 const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;20 describe('JSCoverage', function() {21 it('should work', async function({page, server}) {22 await page.coverage.startJSCoverage();23 await page.goto(server.PREFIX + '/jscoverage/simple.html', {waitUntil: 'networkidle0'});24 const coverage = await page.coverage.stopJSCoverage();25 expect(coverage.length).toBe(1);26 expect(coverage[0].url).toContain('/jscoverage/simple.html');27 expect(coverage[0].ranges).toEqual([28 { start: 0, end: 17 },29 { start: 35, end: 61 },30 ]);31 });32 it('should report sourceURLs', async function({page, server}) {33 await page.coverage.startJSCoverage();34 await page.goto(server.PREFIX + '/jscoverage/sourceurl.html');35 const coverage = await page.coverage.stopJSCoverage();36 expect(coverage.length).toBe(1);37 expect(coverage[0].url).toBe('nicename.js');38 });39 it('should ignore eval() scripts by default', async function({page, server}) {40 await page.coverage.startJSCoverage();41 await page.goto(server.PREFIX + '/jscoverage/eval.html');42 const coverage = await page.coverage.stopJSCoverage();43 expect(coverage.length).toBe(1);44 });45 it('shouldn\'t ignore eval() scripts if reportAnonymousScripts is true', async function({page, server}) {46 await page.coverage.startJSCoverage({reportAnonymousScripts: true});47 await page.goto(server.PREFIX + '/jscoverage/eval.html');48 const coverage = await page.coverage.stopJSCoverage();49 expect(coverage.find(entry => entry.url.startsWith('debugger://'))).not.toBe(null);50 expect(coverage.length).toBe(2);51 });52 it('should ignore pptr internal scripts if reportAnonymousScripts is true', async function({page, server}) {53 await page.coverage.startJSCoverage({reportAnonymousScripts: true});54 await page.goto(server.EMPTY_PAGE);55 await page.evaluate('console.log("foo")');56 await page.evaluate(() => console.log('bar'));57 const coverage = await page.coverage.stopJSCoverage();58 expect(coverage.length).toBe(0);59 });60 it('should report multiple scripts', async function({page, server}) {61 await page.coverage.startJSCoverage();62 await page.goto(server.PREFIX + '/jscoverage/multiple.html');63 const coverage = await page.coverage.stopJSCoverage();64 expect(coverage.length).toBe(2);65 coverage.sort((a, b) => a.url.localeCompare(b.url));66 expect(coverage[0].url).toContain('/jscoverage/script1.js');67 expect(coverage[1].url).toContain('/jscoverage/script2.js');68 });69 it('should report right ranges', async function({page, server}) {70 await page.coverage.startJSCoverage();71 await page.goto(server.PREFIX + '/jscoverage/ranges.html');72 const coverage = await page.coverage.stopJSCoverage();73 expect(coverage.length).toBe(1);74 const entry = coverage[0];75 expect(entry.ranges.length).toBe(1);76 const range = entry.ranges[0];77 expect(entry.text.substring(range.start, range.end)).toBe(`console.log('used!');`);78 });79 it('should report scripts that have no coverage', async function({page, server}) {80 await page.coverage.startJSCoverage();81 await page.goto(server.PREFIX + '/jscoverage/unused.html');82 const coverage = await page.coverage.stopJSCoverage();83 expect(coverage.length).toBe(1);84 const entry = coverage[0];85 expect(entry.url).toContain('unused.html');86 expect(entry.ranges.length).toBe(0);87 });88 it('should work with conditionals', async function({page, server}) {89 await page.coverage.startJSCoverage();90 await page.goto(server.PREFIX + '/jscoverage/involved.html');91 const coverage = await page.coverage.stopJSCoverage();92 expect(JSON.stringify(coverage, null, 2).replace(/:\d{4}\//g, ':<PORT>/')).toBeGolden('jscoverage-involved.txt');93 });94 describe('resetOnNavigation', function() {95 it('should report scripts across navigations when disabled', async function({page, server}) {96 await page.coverage.startJSCoverage({resetOnNavigation: false});97 await page.goto(server.PREFIX + '/jscoverage/multiple.html');98 await page.goto(server.EMPTY_PAGE);99 const coverage = await page.coverage.stopJSCoverage();100 expect(coverage.length).toBe(2);101 });102 it('should NOT report scripts across navigations when enabled', async function({page, server}) {103 await page.coverage.startJSCoverage(); // Enabled by default.104 await page.goto(server.PREFIX + '/jscoverage/multiple.html');105 await page.goto(server.EMPTY_PAGE);106 const coverage = await page.coverage.stopJSCoverage();107 expect(coverage.length).toBe(0);108 });109 });110 // @see https://crbug.com/990945111 xit('should not hang when there is a debugger statement', async function({page, server}) {112 await page.coverage.startJSCoverage();113 await page.goto(server.EMPTY_PAGE);114 await page.evaluate(() => {115 debugger; // eslint-disable-line no-debugger116 });117 await page.coverage.stopJSCoverage();118 });119 });120 describe('CSSCoverage', function() {121 it('should work', async function({page, server}) {122 await page.coverage.startCSSCoverage();123 await page.goto(server.PREFIX + '/csscoverage/simple.html');124 const coverage = await page.coverage.stopCSSCoverage();125 expect(coverage.length).toBe(1);126 expect(coverage[0].url).toContain('/csscoverage/simple.html');...
coverage.js
Source:coverage.js
...20 await helper.end()21})22test.serial('JSCoverage should work', async t => {23 const { page, server } = t.context24 await page.coverage.startJSCoverage()25 await page.goto(server.PREFIX + '/jscoverage/simple.html', {26 waitUntil: 'networkidle0'27 })28 const coverage = await page.coverage.stopJSCoverage()29 t.is(coverage.length, 1)30 t.true(coverage[0].url.includes('/jscoverage/simple.html'))31 t.deepEqual(coverage[0].ranges, [32 { start: 0, end: 17 },33 { start: 35, end: 61 }34 ])35})36test.serial('JSCoverage should report sourceURLs', async t => {37 const { page, server } = t.context38 await page.coverage.startJSCoverage()39 await page.goto(server.PREFIX + '/jscoverage/sourceurl.html')40 const coverage = await page.coverage.stopJSCoverage()41 t.is(coverage.length, 1)42 t.is(coverage[0].url, 'nicename.js')43})44test.serial('JSCoverage should ignore eval() scripts by default', async t => {45 const { page, server } = t.context46 await page.coverage.startJSCoverage()47 await page.goto(server.PREFIX + '/jscoverage/eval.html')48 const coverage = await page.coverage.stopJSCoverage()49 t.is(coverage.length, 1)50})51test.serial(52 "JSCoverage shouldn't ignore eval() scripts if reportAnonymousScripts is true",53 async t => {54 const { page, server } = t.context55 await page.coverage.startJSCoverage({ reportAnonymousScripts: true })56 await page.goto(server.PREFIX + '/jscoverage/eval.html')57 const coverage = await page.coverage.stopJSCoverage()58 t.truthy(coverage.find(entry => entry.url.startsWith('debugger://')))59 t.is(coverage.length, 2)60 }61)62test.serial.skip(63 'JSCoverage should ignore pptr internal scripts if reportAnonymousScripts is true',64 async t => {65 const { page, server } = t.context66 await page.coverage.startJSCoverage({ reportAnonymousScripts: true })67 await page.goto(server.EMPTY_PAGE)68 await page.evaluate('console.log("foo")')69 await page.evaluate(() => console.log('bar'))70 const coverage = await page.coverage.stopJSCoverage()71 t.is(coverage.length, 0)72 }73)74test.serial('JSCoverage should report multiple scripts', async t => {75 const { page, server } = t.context76 await page.coverage.startJSCoverage()77 await page.goto(server.PREFIX + '/jscoverage/multiple.html')78 const coverage = await page.coverage.stopJSCoverage()79 t.is(coverage.length, 2)80 coverage.sort((a, b) => a.url.localeCompare(b.url))81 t.true(coverage[0].url.includes('/jscoverage/script1.js'))82 t.true(coverage[1].url.includes('/jscoverage/script2.js'))83})84test.serial('JSCoverage should report right ranges', async t => {85 const { page, server } = t.context86 await page.coverage.startJSCoverage()87 await page.goto(server.PREFIX + '/jscoverage/ranges.html')88 const coverage = await page.coverage.stopJSCoverage()89 t.is(coverage.length, 1)90 const entry = coverage[0]91 t.is(entry.ranges.length, 1)92 const range = entry.ranges[0]93 t.is(entry.text.substring(range.start, range.end), `console.log('used!');`)94})95test.serial(96 'JSCoverage should report scripts that have no coverage',97 async t => {98 const { page, server } = t.context99 await page.coverage.startJSCoverage()100 await page.goto(server.PREFIX + '/jscoverage/unused.html')101 const coverage = await page.coverage.stopJSCoverage()102 t.is(coverage.length, 1)103 const entry = coverage[0]104 t.true(entry.url.includes('unused.html'))105 t.is(entry.ranges.length, 0)106 }107)108test.serial('JSCoverage should work with conditionals', async t => {109 const { page, server } = t.context110 await page.coverage.startJSCoverage()111 await page.goto(server.PREFIX + '/jscoverage/involved.html')112 const coverage = await page.coverage.stopJSCoverage()113 t.context.toBeGolden(114 t,115 JSON.stringify(coverage, null, 2).replace(/:\d{4}\//g, ':<PORT>/'),116 'jscoverage-involved.txt'117 )118})119test.serial(120 'JSCoverage - resetOnNavigation: should report scripts across navigations when disabled',121 async t => {122 const { page, server } = t.context123 await page.coverage.startJSCoverage({ resetOnNavigation: false })124 await page.goto(server.PREFIX + '/jscoverage/multiple.html')125 await page.goto(server.EMPTY_PAGE)126 const coverage = await page.coverage.stopJSCoverage()127 t.is(coverage.length, 2)128 }129)130test.serial(131 'JSCoverage - resetOnNavigation: should NOT report scripts across navigations when enabled',132 async t => {133 const { page, server } = t.context134 await page.coverage.startJSCoverage() // Enabled by default.135 await page.goto(server.PREFIX + '/jscoverage/multiple.html')136 await page.goto(server.EMPTY_PAGE)137 const coverage = await page.coverage.stopJSCoverage()138 t.is(coverage.length, 0)139 }140)141test.serial(142 'JSCoverage should not hang when there is a debugger statement',143 async t => {144 const { page, server } = t.context145 await page.coverage.startJSCoverage()146 await page.goto(server.EMPTY_PAGE)147 await page.evaluate(`() => {148 debugger // eslint-disable-line no-debugger149 }`)150 await page.coverage.stopJSCoverage()151 t.pass()152 }153)154test.serial('CSSCoverage should work', async t => {155 const { page, server } = t.context156 await page.coverage.startCSSCoverage()157 await page.goto(server.PREFIX + '/csscoverage/simple.html')158 const coverage = await page.coverage.stopCSSCoverage()159 t.is(coverage.length, 1)...
electron-cl.spec.js
Source:electron-cl.spec.js
...112 //console.log(appPath);113 */114 // Get the first window that the app opens, wait if necessary.115 window = await app.firstWindow();116 await window.coverage.startJSCoverage({reportAnonymousScripts: true});117 // Check cmd line parameters have been read118 await waitVrm2Ready()119 let exclids = await window.locator('id=excluded_ids').inputValue()120 await expect(exclids.includes('some_id')).toBeTruthy()121 await expect(exclids.includes('some_other_id')).toBeTruthy()122 // Check empty main file (file not found)123 await expect(await window.locator('id=name').innerHTML()).toBe('')124 // Check empty ref file (file not found)125 await expect(await window.locator('#ref_name').innerHTML()).toBe('')126 await app.close({runBeforeUnload: true})127 })128 test('launch the application', async () => {129 app = await electron.launch({130 path: electronPath,131 args: ['lib/main.js',132 '--settDir', './tmp', //rq: ->(rq_cl_settings_file)133 '--settFile', 'cl-settings.json',134 '--regex',135 '--select', 'maze',136 '--exclIds', '"some_id,some_other_id"',137 '--exclDoctypes', '"foo,fie,fum"',138 '--diagram',139 '--hierarchy',140 '--safety',141 '--format', 'svg',142 '--output', 'tmp/cl-test',143 '--oreqm_main', './testdata/oreqm_testdata_del_movement.oreqm', //rq: ->(rq_one_oreqm_cmd_line)144 '--oreqm_ref', './testdata/oreqm_testdata_no_ogre.oreqm' //rq: ->(rq_two_oreqm_cmd_line)145 ]146 })147 window = await app.firstWindow();148 await window.coverage.startJSCoverage({reportAnonymousScripts: true});149 await waitVrm2Ready()150 // Check main file151 await expect((await window.locator('id=name').innerHTML()).includes('./testdata/oreqm_testdata_del_movement.oreqm')).toBe(true)152 // Check ref file153 await expect((await window.locator('#ref_name').innerHTML())).toBe('./testdata/oreqm_testdata_no_ogre.oreqm')154 // Wait for operations from command line to complete155 await waitVrm2Done()156 //test('Check specobject diagram', async () => {157 await compareFiles('./tmp/cl-test-diagram.svg', './test/refdata/cl-test-diagram.svg') //rq: ->(rq_automatic_diagram)158 //test('Check hierarchy diagram', async () => {159 await compareFiles('./tmp/cl-test-doctypes.svg', './test/refdata/cl-test-doctypes.svg')160 // test('Check safetyclass diagram', async () => {161 await compareFiles('./tmp/cl-test-safety.svg', './test/refdata/cl-test-safety.svg')162 await app.close({runBeforeUnload: true})163 })164 test('Open vr2x file from cmd line', async () => {165 app = await electron.launch({166 path: electronPath,167 args: ['lib/main.js',168 '--settDir', './tmp',169 '--settFile', 'cl-settings.json',170 '--diagram',171 '--format', 'svg',172 '--output', 'tmp/cl-context',173 '--context', './testdata/test_context.vr2x'174 ]175 })176 window = await app.firstWindow();177 await window.coverage.startJSCoverage({reportAnonymousScripts: true});178 await waitVrm2Done()179 // Check main file180 await expect((await window.locator('id=name').innerHTML()).includes('oreqm_testdata_no_ogre')).toBe(true)181 // Check ref file182 await expect((await window.locator('id=ref_name').innerHTML()).includes('oreqm_testdata_del_movement')).toBe(true)183 await app.close({runBeforeUnload: true})184 })185 test('Open non-existing vr2x file from cmd line', async () => {186 app = await electron.launch({187 path: electronPath,188 args: ['lib/main.js',189 '--settDir', './tmp',190 '--settFile', 'cl-settings.json',191 '--context', './testdata/thisdoesnotexist.vr2x'192 ]193 })194 window = await app.firstWindow();195 await window.coverage.startJSCoverage({reportAnonymousScripts: true});196 await waitVrm2Done()197 // Check main file198 await expect(await window.locator('id=name').innerHTML()).toBe('')199 // Check ref file200 await expect(await window.locator('id=ref_name').innerHTML()).toBe('')201 await app.close({runBeforeUnload: true})202 })203 test('Select vql from cmd line', async () => {204 app = await electron.launch({205 path: electronPath,206 args: ['lib/main.js',207 '--settDir', './tmp',208 '--settFile', 'cl-settings.json',209 '--oreqm_main', './testdata/oreqm_testdata_del_movement.oreqm',210 '--vql',211 '--format', 'svg'212 ]213 })214 window = await app.firstWindow();215 await window.coverage.startJSCoverage({reportAnonymousScripts: true});216 await waitVrm2Done()217 await expect(window.locator('#vql_radio_input')).toBeChecked()218 await app.close({runBeforeUnload: true})219 })220 test('Select idOnly from cmd line', async () => {221 app = await electron.launch({222 path: electronPath,223 args: ['lib/main.js',224 '--settDir', './tmp',225 '--settFile', 'cl-settings.json',226 '--idOnly',227 '--format', 'svg'228 ]229 })230 window = await app.firstWindow();231 await window.coverage.startJSCoverage({reportAnonymousScripts: true});232 await waitVrm2Done()233 await expect(window.locator('#id_radio_input')).toBeChecked()234 await app.close({runBeforeUnload: true})235 })...
setup.test.js
Source:setup.test.js
...7 })8 const page = await browser.newPage()9 // Enable both JavaScript and CSS coverage10 await Promise.all([11 page.coverage.startJSCoverage([false, false]),12 page.coverage.startCSSCoverage()13 ]);14 await page.goto(15 'file:///Users/anirudhsharma/Documents/Semester8_College/STAD/Final%20Project/MonopolyTestFinalProj/monopoly-master1/index.html'16 )17 await page.click('#setup > div:nth-child(10) > input[type=button]')18 const [jsCoverage, cssCoverage] = await Promise.all([19 page.coverage.stopJSCoverage(),20 page.coverage.stopCSSCoverage(),21 ]);22 pti.write([...jsCoverage, ...cssCoverage], { includeHostname: true , storagePath: './.nyc_output' })23 await browser.close()24})25test("test start game click - 4 players", async () => {26 const browser = await puppeteer.launch({27 })28 const page = await browser.newPage()29 // Enable both JavaScript and CSS coverage30 await Promise.all([31 page.coverage.startJSCoverage([false, false]),32 page.coverage.startCSSCoverage()33 ]);34 await page.goto(35 'file:///Users/anirudhsharma/Documents/Semester8_College/STAD/Final%20Project/MonopolyTestFinalProj/monopoly-master1/index.html'36 )37 const [jsCoverage, cssCoverage] = await Promise.all([38 page.coverage.stopJSCoverage(),39 page.coverage.stopCSSCoverage(),40 ]);41 pti.write([...jsCoverage, ...cssCoverage], { includeHostname: true , storagePath: './.nyc_output' })42 await browser.close()...
Using AI Code Generation
1const puppeteer = require('puppeteer');2const fs = require('fs');3(async () => {4 const browser = await puppeteer.launch();5 const page = await browser.newPage();6 await page.coverage.startJSCoverage();7 const jsCoverage = await page.coverage.stopJSCoverage();8 const coverage = jsCoverage.map(({url, ranges, text}) => {9 return {10 functions: ranges.map(range => text.substring(range.start, range.end))11 };12 });13 fs.writeFileSync('coverage.json', JSON.stringify(coverage));14 await browser.close();15})();16[{
Using AI Code Generation
1const puppeteer = require('puppeteer');2(async () => {3 const browser = await puppeteer.launch({headless: false});4 const page = await browser.newPage();5 await page.coverage.startJSCoverage();6 const jscoverage = await page.coverage.stopJSCoverage();7 console.log(jscoverage);8 await browser.close();9})();
Using AI Code Generation
1const puppeteer = require('puppeteer');2const path = require('path');3(async () => {4 const browser = await puppeteer.launch();5 const page = await browser.newPage();6 await page.coverage.startJSCoverage();7 await page.waitFor(5000);8 const jsCoverage = await page.coverage.stopJSCoverage();9 console.log(jsCoverage);10 await browser.close();11})();12 text: 'var s={137:function(e,t,n){' } ]13const puppeteer = require('puppeteer');14const path = require('path');15const fs = require('fs');16(async () => {17 const browser = await puppeteer.launch();18 const page = await browser.newPage();19 await page.coverage.startJSCoverage();20 await page.waitFor(5000);21 const jsCoverage = await page.coverage.stopJSCoverage();22 await fs.writeFileSync(path.join(__dirname, 'coverage.json'), JSON.stringify(jsCoverage));23 console.log(jsCoverage);24 await browser.close();25})();26{27 "scripts": {28 },
Using AI Code Generation
1const puppeteer = require('puppeteer');2(async () => {3 const browser = await puppeteer.launch();4 const page = await browser.newPage();5 await page.coverage.startJSCoverage();6 const jsCoverage = await page.coverage.stopJSCoverage();7})();8const puppeteer = require('puppeteer');9(async () => {10 const browser = await puppeteer.launch();11 const page = await browser.newPage();12 await page.coverage.startCSSCoverage();13 const cssCoverage = await page.coverage.stopCSSCoverage();14})();15const puppeteer = require('puppeteer');16(async () => {17 const browser = await puppeteer.launch();18 const page = await browser.newPage();19 await page.coverage.startCoverage();20 const coverage = await page.coverage.stopCoverage();21})();
Using AI Code Generation
1const puppeteer = require('puppeteer');2const fs = require('fs');3const path = require('path');4const mkdirp = require('mkdirp');5const util = require('util');6const { URL } = require('url');7const { startJSCoverage } = require('puppeteer-to-istanbul');8const JSCOVERAGE_DIR = path.join(__dirname, 'coverage');9const JSCOVERAGE_FILE = path.join(JSCOVERAGE_DIR, 'coverage-final.json');10(async () => {11 const browser = await puppeteer.launch();12 const page = await browser.newPage();13 await startJSCoverage(page);14 const coverage = await page.coverage.stopJSCoverage();15 await browser.close();16 mkdirp.sync(JSCOVERAGE_DIR);17 fs.writeFileSync(JSCOVERAGE_FILE, JSON.stringify(coverage));18})();19{20 "scripts": {21 }22}23{24}
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!!