mylamour / blog

Your internal mediocrity is the moment when you lost the faith of being excellent. Just do it.
https://fz.cool
61 stars 14 forks source link

Puppeteer: A way to reduce the amount of repetitive work #86

Open mylamour opened 3 years ago

mylamour commented 3 years ago

In recent days, I faced a problem that our team have to mange lot of tickets with ServiceNow. also ServiceNow was provided a cli tools but anyway i didn't get that from service-now store. finally I write a automation scripts with puppeteer and show my little experience within this tutorial.

0x01 Intro

Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome or Chromium. and It was useful to do automation, eg. form submission, UI testing, keyboard input, etc.

Puppeteer Basics

we can just install it with npm install -i puppeteer , and i list some basics options here.

(async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://example.com'); await page.screenshot({ path: 'example.png' });

await browser.close(); })();


* save a pdf
```js
  await page.pdf({ path: 'hn.pdf', format: 'a4' });

Puppeteer Advanced

0x02 Scenario

There was two main operations from my side, The fist one is to assign ticket to each person, and the second one is to close tickets. i will show you how to use puppeteer to implement an automation tools. and this is part code of tools.

const puppeteer = require('puppeteer-debug')
const fs = require('fs');
// const iPhone = puppeteer.devices['iPhone 6'];

; (async function () {

  const browser = await puppeteer.launch({
    // devtools: true,
    headless: false,
    userDataDir: './userdata'
  })

  const page = await browser.newPage()
  // await page.emulate(iPhone);
  await page.goto('https://paypal.service-now.com/sc_task_list.do?xxxxxxxxx&yyyyyyyy')
  await page.waitForTimeout(50000)

  var array = fs.readFileSync('owners').toString().split("\n");

  for (m in array) {

    console.log("Assign ticket to " + array[m])

    await page.evaluate(() => {
      var aTags = document.getElementsByTagName("td");
      var searchText = "\(empty\)";
      var found;

      for (var i = 0; i < aTags.length; i++) {
        if (aTags[i].textContent == searchText) {
          found = aTags[i];
          found.click();
          break;
        }
      }
    });

    for (let i = 0; i < 20; i++) {
      await page.keyboard.press('Enter')
      await page.waitForTimeout('2000')
      await page.keyboard.type(array[m])
      await page.waitForTimeout('2000')
      await page.keyboard.press('ArrowDown')
      await page.waitForTimeout('2000')
      await page.keyboard.press('Enter')
      await page.waitForTimeout('3000')
      if (i != 19) {
        await page.keyboard.press('ArrowDown')
      }
    }

    await page.evaluate(() => {
      document.getElementsByTagName("button")[4].click();
    });

    await page.waitForTimeout(5000)

  }

  await puppeteer.debug() // go to REPL
  await browser.close()

})()

The code was not difficult, and here is some trick what i learned from that.

Here is the final results:

https://user-images.githubusercontent.com/12653147/122204653-f2af6600-ced1-11eb-8eac-6adbc8f13140.mp4

0x03 Conclusion

In last year, we are under fastest growing and there was lot of works need to do. After period of testing , i have to say —— it's necessary to make something automation. especially In daily basis.

Resources