-3

I have a code where I want to generate manually using a function that creates variables and assign their values dynamically

an example code I want the function to generate :

  const browser1 = await puppeteer.launch();
  const browser2 = await puppeteer.launch();
  const browser3 = await puppeteer.launch();
  const browser4 = await puppeteer.launch();
  const browser5 = await puppeteer.launch();
  const browser6 = await puppeteer.launch();
  const context1 = await browser1.createIncognitoBrowserContext();
  const context2 = await browser2.createIncognitoBrowserContext();
  const context3 = await browser3.createIncognitoBrowserContext();
  const context4 = await browser4.createIncognitoBrowserContext();
  const context5 = await browser5.createIncognitoBrowserContext();
  const context6 = await browser6.createIncognitoBrowserContext();
  const page1 = await context1.newPage();
  const page2 = await context2.newPage();
  const page3 = await context3.newPage();
  const page4 = await context4.newPage();
  const page5 = await context5.newPage();
  const page6 = await context6.newPage();

1 Answers1

0

Maybe something like this?

const browsers = []
const contexts = []
const pages = []
const size = 6

for (let i = 0; i < size; i ++) {
  const browser = await puppeteer.launch()
  browsers.push(browser)
  const context = await browser.createIncognitoBrowserContext()
  contexts.push(context)
  const page = await context.newPage()
  pages.push(page)
}
wardialer
  • 490
  • 3
  • 7