เริ่มต้นทำ Automate testing with Puppeteer (EP.02)

Piyathida San-aoun
1 min readJul 14, 2019

--

ถ้าใครยังไม่ทราบว่า Puppeteer คืออะไร และติดตั้งอย่างไร สามารถเข้าไปดูได้ใน ในบทความที่แล้ว

วิธีติดตั้ง Automate Software testing โดย Puppeteer (EP.01)

มาเริ่มต้นกันเลยค๊าา….

  • สร้างไฟล์ JavaScript ขึ้นมา เช่น lazada_test.js จากนั้น ทำการ import Puppeteer library
const puppeteer = require(‘puppeteer’);

หลังจากนั้น set up Open Browser and Close Browser ซึ่งสร้างไฟล์ config.js และ Puppeteer สามารถ set up ตั้งค่า headless

  • false: แสดงหน้าจอ
  • true: รันแบบซ่อนหน้าจอ

เมื่อทำการ Set up Open Browser เรียบร้อยแล้ว มาเริ่มทำ Test case กัน

  • describe : อธิบายการทดสอบ
  • it : Test case / Test scenario

เพื่อให้ง่ายต่อการ implement สามารถทำการแยก Object เพื่องานต่อการ coding ยกตัวอย่างเช่น สร้าง Utility.js

หลังจากที่สร้าง test case เรียบร้อยแล้ว

Run Program

  1. สร้าง file package.json และกำหนด path
{"name": "pptr-project","version": "1.0.0","description": "Test WEB UI","main": "index.js","scripts": {"test": "D:/MyJobs/pptr-lazada/node_modules/.bin/mocha --timeout=30000 ./tests"},"author": "","license": "ISC","dependencies": {"chai": "^4.2.0","mocha": "^6.1.4","puppeteer": "^1.17.0"}}

2. Run ผ่าน Terminal โดยใช้คำสั่ง

npm run test

3. แสดงผลการทดสอบดังนี้

สรุป

Puppeteer เป็น Automate Tool ที่ใช้งานได้ง่ายมากถ้าคุณเคยเขียน JavaScript มา และถ้าใครใช้ Robot framework with selenium มาก่อน ก็ไม่ได้ยากเกินไป เพราะแค่เรารู้ syntax ก็ทำได้แล้วและถ้าใคร มีปัญหาว่าเมื่อทำ End to End กับ Robot framework with selenium แล้วช้า รับรองว่า puppeteer ตอบโจทย์และแก้ปัญหานี้ได้แน่นอน เพราะจากการทดลองรัน เปรียบเทียบ แล้ว พบว่า puppeteer ทราบผลการรันทดสอบเร็วกว่าถึง 2 เท่า

--

--

Piyathida San-aoun
Piyathida San-aoun

Written by Piyathida San-aoun

Software Developer Engineer In Test

No responses yet