-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
59 lines (50 loc) · 1.67 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const { join } = require('path')
const { chromium } = require('playwright')
const dataDir = join(__dirname, 'user-data')
const extension = join(__dirname, 'extension')
const launchOptions = {
headless: false,
args: [
`--disable-extensions-except=${extension}`,
`--load-extension=${extension}`,
],
}
const test = async (page) => {
await page.goto('https://example.org/', { waitUntil: 'networkidle' })
// this validates the extension is indeed running on the page
console.log('DOM insertion:', !!(await page.$('#a-very-nice-clock')))
// this verifies that message passing is working as expected
const promise = page.waitForSelector('#a-very-nice-clock >> text=/^\\d+:/')
console.log('clock updated:', !!(await promise.catch(() => {})))
}
const runInNewPage = async (browser, task) => {
const page = await browser.newPage()
page.setDefaultTimeout(1000)
await task(page)
await page.close()
}
const runPersistentTest = async () => {
const browser = await chromium.launchPersistentContext(dataDir, launchOptions)
await runInNewPage(browser, test)
await browser.close()
}
const enableExtensionInInconito = async (page) => {
await page.goto('chrome://extensions')
await page.click('extensions-item >> #detailsButton')
await page.click('#allow-incognito')
}
const runInconitoTest = async () => {
const browser = await chromium.launch(launchOptions)
await runInNewPage(browser, enableExtensionInInconito)
await runInNewPage(browser, test)
await browser.close()
}
!(async () => {
for (const [name, runTest] of [
['persistent', runPersistentTest],
['inconigto', runInconitoTest],
]) {
console.log(`==== ${name} ====`)
await runTest()
}
})()