constpuppeteer = require('puppeteer') //khai báo thư viện dùng để chụp ảnh
consttoken = process.env.TEST_TOKEN
beforeAll(async () => {
try {
browser = awaitpuppeteer.launch({
headless: true, //optional: defines whether or not the task is allowed in foreground. Default is false
slowMo: 0, //đặt thời gian trì hoãn của dòng lệnh được thực hiện
ignoreDefaultArgs: ['--no-sandbox'],
})
page = await browser.newPage()
viewport = await page.setViewport({ width: 1853 , height: 951 }) // hiển thị màn hình khi truy cập
await page.goto('http://localhost:5000') //truy cập vào port 5000
await page.evaluate((token) => {
localStorage.setItem('access_token', token) //truy cập vào trang bằng token
}, token)
} catch (error) {
console.log(error)
}
})
describe('Home page', () => {
test('go to tasks', async () => {
await page.goto('http://localhost:5000/home') //sau khi truy cập thành công bằng token thì được chuyển đến trang home
await page.waitForResponse((response) => response.url().includes('/taskBoards') && response.status() === 200) //trả về status 200 sau khi truy cập thành công
await page.waitForSelector('.board') //chờ class này xuất hiện trên màn hình
await page.waitForSelector('.statuses') //chờ class này xuất hiện trên màn hình
})
Bước 2:
Tạo bước để thực hiện test (CRUD) và sau khi thực hiện xong test thì thoát
//Add task
test('create task', async () => {
await page.waitForSelector('div.testing-in-task .add-task') //chờ class này xuất hiện trên màn hình
await page.click('div.testing-in-task .add-task') //thực hiện click được gọi
await page.waitFor(5000) //cho thời gian đợi không quá 5s
await page.waitForSelector('.task-add') //chờ class này xuất hiện trên màn hình
const addTaskImage = await page.screenshot() //chụp ảnh màn hình
expect(addTaskImage).toMatchImageSnapshot() //kiểm tra xem đã chụp ảnh màn hình hay chưa
await page.keyboard.type('task created in task status') //thực hiện nhập kí tự
await page.click('.save-add') //thực hiện click được gọi
await page.waitForSelector('.testing-in-task .task-name') ///chờ class này xuất hiện trên màn hình
const image = await page.screenshot() //chụp ảnh màn hình
expect(image).toMatchImageSnapshot() //kiểm tra xem đã chụp ảnh màn hình hay chưa