Best JavaScript code snippet using ava
browser.test.js
Source:browser.test.js
1/**2 * This file is part of the chromium-screenshot package3 *4 * For the full copyright and license information, please view the LICENSE5 * file that was distributed with this source code.6 */7// const assert = require("assert");8const {9 describe10} = require("mocha");11// const sinon = require("sinon");12// const puppeteer = require("puppeteer");13// const EventEmitter = require("events");14// const Browser = require("../src/browser");15describe(16 "Browser",17 () =>18 {19 // const sandBox = sinon.createSandbox();20 /*21 describe(22 "#fetch()",23 () =>24 {25 // stub logger method before each test26 beforeEach(() => sandBox.stub(puppeteer, "launch"));27 // restore logger state after each test28 afterEach(() => sandBox.restore());29 it(30 "should return the browser instance",31 async () =>32 {33 const browser = new Browser(["--no-sandbox"]);34 const instance = new EventEmitter();35 puppeteer.launch.returns(Promise.resolve(instance));36 assert.equal(await browser.fetch(), instance);37 }38 );39 it(40 "should cache the browser instance",41 async () =>42 {43 const browser = new Browser(["--no-sandbox"]);44 const firstInstance = new EventEmitter();45 const secondInstance = new EventEmitter();46 puppeteer.launch.onCall(0).returns(Promise.resolve(firstInstance));47 puppeteer.launch.onCall(1).returns(Promise.resolve(secondInstance));48 await browser.fetch();49 const secondResult = await browser.fetch();50 sandBox.assert.calledOnce(puppeteer.launch);51 assert.equal(secondResult, firstInstance);52 }53 );54 it(55 "should cache the browser promise",56 async () =>57 {58 const browser = new Browser(["--no-sandbox"]);59 // this promise never resolves60 puppeteer.launch.returns(new Promise(() => true));61 const firstPromise = browser.fetch();62 const secondPromise = browser.fetch();63 assert.equal(firstPromise, secondPromise);64 }65 );66 it(67 "should clear the instance when the browser disconnects",68 async () =>69 {70 const browser = new Browser(["--no-sandbox"]);71 const instance = new EventEmitter();72 // mock73 puppeteer.launch.returns(Promise.resolve(instance));74 sandBox.stub(browser, "clear");75 // fetch instance and trigger disconnect76 await browser.fetch();77 // this calls listeners synchronously78 instance.emit("disconnected");79 sandBox.assert.calledOnce(browser.clear);80 }81 );82 it(83 "should clear the instance when the browser disconnects",84 async () =>85 {86 const browser = new Browser(["--no-sandbox"]);87 const instance = new EventEmitter();88 // mock89 puppeteer.launch.returns(Promise.resolve(instance));90 sandBox.stub(browser, "clear");91 // fetch instance and trigger disconnect92 await browser.fetch();93 // this calls listeners synchronously94 instance.emit("disconnected");95 sandBox.assert.calledOnce(browser.clear);96 }97 );98 }99 );100 describe(101 "#clear()",102 () =>103 {104 // stub logger method before each test105 beforeEach(() => sandBox.stub(puppeteer, "launch"));106 // restore logger state after each test107 afterEach(() => sandBox.restore());108 it(109 "should clear the browser instance",110 async () =>111 {112 const browser = new Browser(["--no-sandbox"]);113 // mock114 const firstInstance = new EventEmitter();115 const secondInstance = new EventEmitter();116 firstInstance.close = () => true;117 secondInstance.close = () => true;118 puppeteer.launch.onCall(0).returns(Promise.resolve(firstInstance));119 puppeteer.launch.onCall(1).returns(Promise.resolve(secondInstance));120 // fetch, clear then fetch again121 await browser.fetch();122 browser.clear();123 const secondResult = await browser.fetch();124 sandBox.assert.calledTwice(puppeteer.launch);125 assert.equal(secondResult, secondInstance);126 }127 );128 it(129 "should not repeat calls to close the browser",130 async () =>131 {132 const browser = new Browser(["--no-sandbox"]);133 // mock134 const instance = new EventEmitter();135 instance.close = () => true;136 const closeSpy = sinon.spy(instance, "close");137 puppeteer.launch.returns(Promise.resolve(instance));138 // fetch, clear then fetch again139 await browser.fetch();140 browser.clear();141 browser.clear();142 sandBox.assert.calledOnce(closeSpy);143 }144 );145 }146 );147 */148 }...
index-puppeteer.js
Source:index-puppeteer.js
1const puppeteer = require("puppeteer");2(async () => {3 // const browser = await puppeteer.launch({4 // headless: false,5 // devtools: true,6 // });7 // const page = await browser.newPage();8 // await page.goto("https://google.com");9 // await page.type("[title~=Search]", "World", { delay: 100 });10 // await page.keyboard.press("Enter");11 // await page.waitForNavigation();12 // await page.screenshot({ path: "example.png" });13 // await browser.close();14 // /* 1. Creating a PDF from the website */15 // const browser = await puppeteer.launch({ headless: true });16 // const page = await browser.newPage();17 // await page.goto("https://google.com/");18 // await page.pdf({19 // path: "./page.pdf",20 // format: "A4",21 // });22 // await browser.close();23 // /* 2. Getting the URL or the Title of the current page */24 // const browser = await puppeteer.launch({ headless: false });25 // const page = await browser.newPage();26 // await page.goto("https://learnscraping.com/");27 // let title = await page.title();28 // console.log(`Title of the page is ${title}`);29 // let url = await page.url();30 // console.log(`URL of the page is ${url}`);31 // await browser.close();32 // // /* 3. Emulate a phone */33 // try {34 // const browser = await puppeteer.launch({ headless: false });35 // const page = await browser.newPage();36 // await page.emulate(puppeteer.devices["iPhone X"]);37 // await page.goto("https://learnscraping.com/");38 // // await browser.close();39 // } catch (err) {40 // console.log(err);41 // }42 //instagram login43 // const browser = await puppeteer.launch({44 // headless: false,45 // });46 // const page = await browser.newPage();47 // await page.goto("https://instagram.com");48 // await page.waitForTimeout(1000);49 // await page.waitForSelector('input[name="username"]');50 // await page.type('input[name="username"]', "Hayo_man");51 // await page.type('input[name="password"]', "12345");52 // await page.click("#loginForm > div > div:nth-child(3) > button");53 //website loads faster54 // const browser = await puppeteer.launch({55 // headless: false,56 // });57 // const page = await browser.newPage();58 // await page.setRequestInterception(true);59 // page.on("request", (request) => {60 // if (["image", "stylesheet", "font"].includes(request.resourceType())) {61 // request.abort();62 // } else {63 // request.continue();64 // }65 // });66 // await page.goto("https://amazon.com/");67 // debugger;68 // // await browser.close()69 //basic auth70 // const browser = await puppeteer.launch({71 // headless: false,72 // });73 // const page = await browser.newPage();74 // await page.authenticate({75 // username: "admin",76 // password: "1234",77 // });78 // await page.goto("https://httpbin.org/basic-auth/admin/1234");79 // debugger;80 //IGNORE HTTP ERRORS AND CHANGE VIEW PORT OF BROSWER81 // const browser = await puppeteer.launch({82 // headless: false,83 // ignoreHTTPSErrors: true,84 // defaultViewport: {85 // width: 920,86 // height: 1080,87 // },88 // });89 // const page = await browser.newPage();90 // await page.goto("https://google.com/");91 // debugger;92 //USING PROXY93 const browser = await puppeteer.launch({94 headless: false,95 args: ["--proxy-server=60.246.7.4:8080"],96 });97 const page = await browser.newPage();98 await page.goto("https://httpbin.org/ip");99 debugger;...
print.js
Source:print.js
...20 const pdfFilename = typeof print === 'string' ? print : filename.replace(/\.md$/, '.pdf');21 debug({ initialUrl, printPluginPath, pdfFilename, puppeteerLaunchConfig });22 console.log(`Attempting to print "${filename}" to "${pdfFilename}".`);23 try {24 const browser = await puppeteer.launch(puppeteerLaunchConfig);25 const page = await browser.newPage();26 const pdfOptions = { path: pdfFilename, printBackground: true };27 Object.assign(pdfOptions, getPageOptions(printSize));28 await page.goto(`${initialUrl}?print-pdf`, { waitUntil: 'load' });29 await page.pdf(pdfOptions);30 await browser.close();31 } catch (err) {32 console.error(`Error while generating PDF for "${filename}"`);33 debug(err);34 }...
index.js
Source:index.js
...14 */15function createFactory({ puppeteerLaunchArgs, validate }) {16 const factory = {};17 factory.create = function createFn() {18 return puppeteer.launch(...puppeteerLaunchArgs);19 };20 factory.destroy = function destroyFn(browserInstance) {21 return browserInstance.close();22 };23 if (validate && typeof validate === 'function') {24 factory.validate = validate;25 }26 return factory;27}28/**29 *30 * @returns {*}31 * @param poolConfig32 */...
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.screenshot({path: 'example.png'});6 await browser.close();7})();8const puppeteer = require('puppeteer');9(async () => {10 const browser = await puppeteer.launch({headless: false});11 const page = await browser.newPage();12 await page.screenshot({path: 'example.png'});13 await browser.close();14})();15const puppeteer = require('puppeteer');16(async () => {17 const browser = await puppeteer.launch({headless: false});18 const page = await browser.newPage();19 await page.screenshot({path: 'example.png'});20 await browser.close();21})();22const puppeteer = require('puppeteer');23(async () => {24 const browser = await puppeteer.launch({headless: false});25 const page = await browser.newPage();26 await page.screenshot({path: 'example.png'});27 await browser.close();28})();29const puppeteer = require('puppeteer');30(async () => {31 const browser = await puppeteer.launch({headless: false});32 const page = await browser.newPage();33 await page.screenshot({path: 'example.png'});34 await browser.close();35})();36const puppeteer = require('puppeteer');37(async () => {38 const browser = await puppeteer.launch({headless: false});39 const page = await browser.newPage();40 await page.screenshot({path: 'example.png'});41 await browser.close();42})();43const puppeteer = require('puppeteer
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})();6const puppeteer = require('puppeteer');7(async () => {8 const browser = await puppeteer.launch({headless: false});9 const page = await browser.newPage();10 await page.waitForSelector('video');11 await page.click('video');12 await page.waitFor(5000);
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.screenshot({path: 'google.png'});6 await browser.close();7})();8const puppeteer = require('puppeteer');9(async () => {10 const browser = await puppeteer.launch({headless: true});11 const page = await browser.newPage();12 await page.screenshot({path: 'google.png'});13 await browser.close();14})();
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.screenshot({path: 'example.png'});6 await browser.close();7})();8const puppeteer = require('puppeteer');9(async () => {10 const browser = await puppeteer.launch({headless: false});11 const page = await browser.newPage();12 const data = await page.evaluate(() => {13 const title = document.querySelector('title').innerText;14 const description = document.querySelector('meta[name="description"]').getAttribute('content');15 return {title, description}16 });17 console.log(data)18 await browser.close();19})();20const puppeteer = require('puppeteer');21(async () => {22 const browser = await puppeteer.launch({headless: false});23 const page = await browser.newPage();24 const title = await page.title();25 const description = await page.$eval('meta[name="description"]', element => element.getAttribute('content'));26 console.log(title, description)27 await browser.close();28})();
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.screenshot({path: 'screenshot.png'});7 await browser.close();8})();9const puppeteer = require('puppeteer');10const fs = require('fs');11(async () => {12 const browser = await puppeteer.launch();13 const page = await browser.newPage();14 await page.screenshot({path: 'screenshot.png'});15 await browser.close();16})();17const puppeteer = require('puppeteer');18const fs = require('fs');19(async () => {20 const browser = await puppeteer.launch();21 const page = await browser.newPage();22 await page.screenshot({path: 'screenshot.png'});23 await browser.close();24})();25const puppeteer = require('puppeteer');26const fs = require('fs');27(async () => {28 const browser = await puppeteer.launch();29 const page = await browser.newPage();30 await page.screenshot({path: 'screenshot.png'});31 await browser.close();32})();33const puppeteer = require('puppeteer');34const fs = require('fs');35(async () => {36 const browser = await puppeteer.launch();37 const page = await browser.newPage();38 await page.screenshot({path: 'screenshot.png'});39 await browser.close();40})();41const puppeteer = require('puppeteer');42const fs = require('fs');43(async () => {44 const browser = await puppeteer.launch();45 const page = await browser.newPage();46 await page.goto('
Using AI Code Generation
1const puppeteer = require('puppeteer');2const devices = require('puppeteer/DeviceDescriptors');3const iPhone = devices['iPhone 6'];4(async () => {5 const browser = await puppeteer.launch({6 });7 const page = await browser.newPage();8 await page.emulate(iPhone);9 await page.screenshot({path: 'google.png'});10 await browser.close();11})();12{13 "scripts": {14 },15 "dependencies": {16 }17}18const puppeteer = require('puppeteer');19const devices = require('puppeteer/DeviceDescriptors');20const iPhone = devices['iPhone 6'];
Using AI Code Generation
1const puppeteer = require('puppeteer');2async function getBrowser() {3try {4const browser = await puppeteer.launch({5});6return browser;7} catch (error) {8console.log(error);9}10}11module.exports = getBrowser;12const puppeteer = require('puppeteer');13async function getBrowser() {14try {15const browser = await puppeteer.launch({16});17return browser;18} catch (error) {19console.log(error);20}21}22module.exports = getBrowser;23const puppeteer = require('puppeteer');24async function getBrowser() {25try {26const browser = await puppeteer.launch({27});28return browser;29} catch (error) {30console.log(error);31}32}33module.exports = getBrowser;34const puppeteer = require('puppeteer');35async function getBrowser() {36try {37const browser = await puppeteer.launch({38});39return browser;40} catch (error) {41console.log(error);42}43}44module.exports = getBrowser;45const puppeteer = require('puppeteer');46async function getBrowser() {47try {48const browser = await puppeteer.launch({
Using AI Code Generation
1const puppeteer = require('puppeteer');2(async () => {3const browser = await puppeteer.launch();4const page = await browser.newPage();5await page.screenshot({path: 'google.png'});6await browser.close();7})();8const puppeteer = require('puppeteer');9(async () => {10const browser = await puppeteer.launch();11const page = await browser.newPage();12await page.type('input[title="Search"]', 'puppeteer');13await page.click('input[value="Google Search"]');14await page.screenshot({path: 'google.png'});15await browser.close();16})();17const puppeteer = require('puppeteer');18(async () => {19const browser = await puppeteer.launch();20const page = await browser.newPage();21await page.type('input[title="Search"]', 'puppeteer');22await page.click('input[value="Google Search"]');23await page.waitForNavigation();24await page.screenshot({path: 'google.png'});
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!!