2019-12-04 19:13:00 +08:00
|
|
|
import puppeteer from 'puppeteer'
|
|
|
|
|
2020-02-06 10:10:43 +08:00
|
|
|
export const E2E_TIMEOUT = 30 * 1000
|
|
|
|
|
2019-12-04 19:13:00 +08:00
|
|
|
const puppeteerOptions = process.env.CI
|
|
|
|
? { args: ['--no-sandbox', '--disable-setuid-sandbox'] }
|
|
|
|
: {}
|
|
|
|
|
|
|
|
export function setupPuppeteer() {
|
|
|
|
let browser: puppeteer.Browser
|
|
|
|
let page: puppeteer.Page
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
browser = await puppeteer.launch(puppeteerOptions)
|
|
|
|
page = await browser.newPage()
|
2019-12-11 23:15:52 +08:00
|
|
|
|
|
|
|
page.on('console', e => {
|
|
|
|
if (e.type() === 'error') {
|
2019-12-13 09:46:20 +08:00
|
|
|
const err = e.args()[0] as any
|
|
|
|
console.error(
|
|
|
|
`Error from Puppeteer-loaded page:\n`,
|
|
|
|
err._remoteObject.description
|
|
|
|
)
|
2019-12-11 23:15:52 +08:00
|
|
|
}
|
|
|
|
})
|
2019-12-04 19:13:00 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await browser.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
async function click(selector: string, options?: puppeteer.ClickOptions) {
|
|
|
|
await page.click(selector, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function count(selector: string) {
|
|
|
|
return (await page.$$(selector)).length
|
|
|
|
}
|
|
|
|
|
|
|
|
async function text(selector: string) {
|
|
|
|
return await page.$eval(selector, node => node.textContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function value(selector: string) {
|
2019-12-11 22:13:47 +08:00
|
|
|
return await page.$eval(selector, (node: HTMLInputElement) => node.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function html(selector: string) {
|
|
|
|
return await page.$eval(selector, node => node.innerHTML)
|
2019-12-04 19:13:00 +08:00
|
|
|
}
|
|
|
|
|
2019-12-10 03:20:56 +08:00
|
|
|
async function classList(selector: string) {
|
|
|
|
return await page.$eval(selector, (node: any) => [...node.classList])
|
|
|
|
}
|
|
|
|
|
|
|
|
async function children(selector: string) {
|
|
|
|
return await page.$eval(selector, (node: any) => [...node.children])
|
|
|
|
}
|
|
|
|
|
2019-12-04 19:13:00 +08:00
|
|
|
async function isVisible(selector: string) {
|
|
|
|
const display = await page.$eval(selector, (node: HTMLElement) => {
|
|
|
|
return window.getComputedStyle(node).display
|
|
|
|
})
|
|
|
|
return display !== 'none'
|
|
|
|
}
|
|
|
|
|
|
|
|
async function isChecked(selector: string) {
|
2019-12-11 22:13:47 +08:00
|
|
|
return await page.$eval(selector, (node: HTMLInputElement) => node.checked)
|
2019-12-04 19:13:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function isFocused(selector: string) {
|
|
|
|
return await page.$eval(selector, node => node === document.activeElement)
|
|
|
|
}
|
|
|
|
|
2019-12-18 22:19:31 +08:00
|
|
|
async function setValue(selector: string, value: string) {
|
|
|
|
const el = (await page.$(selector))!
|
|
|
|
await el.evaluate((node: HTMLInputElement) => (node.value = ''))
|
|
|
|
await el.type(value)
|
|
|
|
}
|
|
|
|
|
2019-12-04 19:13:00 +08:00
|
|
|
async function enterValue(selector: string, value: string) {
|
|
|
|
const el = (await page.$(selector))!
|
2019-12-11 22:13:47 +08:00
|
|
|
await el.evaluate((node: HTMLInputElement) => (node.value = ''))
|
2019-12-04 19:13:00 +08:00
|
|
|
await el.type(value)
|
|
|
|
await el.press('Enter')
|
|
|
|
}
|
|
|
|
|
|
|
|
async function clearValue(selector: string) {
|
2019-12-11 22:13:47 +08:00
|
|
|
return await page.$eval(
|
|
|
|
selector,
|
|
|
|
(node: HTMLInputElement) => (node.value = '')
|
|
|
|
)
|
2019-12-04 19:13:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
page: () => page,
|
|
|
|
click,
|
|
|
|
count,
|
|
|
|
text,
|
|
|
|
value,
|
2019-12-11 22:13:47 +08:00
|
|
|
html,
|
2019-12-04 19:13:00 +08:00
|
|
|
classList,
|
2019-12-10 03:20:56 +08:00
|
|
|
children,
|
2019-12-04 19:13:00 +08:00
|
|
|
isVisible,
|
|
|
|
isChecked,
|
|
|
|
isFocused,
|
2019-12-18 22:19:31 +08:00
|
|
|
setValue,
|
2019-12-04 19:13:00 +08:00
|
|
|
enterValue,
|
|
|
|
clearValue
|
|
|
|
}
|
|
|
|
}
|