-
Notifications
You must be signed in to change notification settings - Fork 3
/
basic.test.js
165 lines (129 loc) · 4.27 KB
/
basic.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { resolve } from 'path'
import fs from 'fs'
import env from 'node-env-file'
import { createBrowser, createPageFunctions } from '../../src'
import { waitFor } from '../utils'
import pageFunctions from './page-functions'
const browserString = process.env.BROWSER_STRING || 'puppeteer/core'
describe(browserString, () => {
let browser
let page
const folder = resolve(__dirname, '..', 'fixtures/basic')
beforeAll(async () => {
if (browserString.includes('browserstack') && browserString.includes('local')) {
const envFile = resolve(__dirname, '..', '..', '.env-browserstack')
if (fs.existsSync(envFile)) {
env(envFile)
}
}
try {
browser = await createBrowser(browserString, {
folder,
async extendPage(page) {
return {
...await createPageFunctions(page, pageFunctions),
routeData() {
return page.runScript(() => ({
path: window.$vueMeta.$route.path,
query: window.$vueMeta.$route.query
}))
}
}
}
}, false)
browser.setLogLevel(['warn', 'error', 'info', 'log'])
await browser.start()
} catch (e) {
console.error(e) // eslint-disable-line no-console
}
/* TODO: check why Jest doesnt bail when browser fails to start
* - https://github.com/facebook/jest/issues/6695
*/
expect(browser).toBeDefined()
expect(browser.isReady()).toBe(true)
})
afterAll(async () => {
if (browser) {
await browser.close()
}
})
test('load page', async function () {
// this should prevent the test to succeed if the browser failed to start
expect.hasAssertions()
if (!browser) {
return
}
const webPath = '/index.html'
const url = browser.getUrl(webPath)
page = await browser.page(url, () => !!window.$vueMeta)
const html = await page.getHtml()
expect(html).toBeDefined()
expect(html).toContain('<html')
expect(html).toContain('</html>')
})
test('getElement', async () => {
const div = await page.getElement('div')
expect(div.attrsMap).toEqual({ id: 'app' })
expect(div.children.length).toBe(5)
})
test('getElements', async () => {
const divs = await page.getElements('div')
expect(divs[0].attrsMap).toEqual({ id: 'app' })
expect(divs[1].attrsMap).toEqual({})
expect(divs[1].children.length).toBe(3)
})
test('getAttribute', async () => {
expect(await page.getAttribute('div', 'id')).toBe('app')
})
test('getAttributes', async () => {
expect(await page.getAttributes('div', 'id')).toEqual(['app', null])
})
test('getText', async () => {
expect(await page.getText('h1')).toBe('Basic')
})
test('getTexts', async () => {
expect(await page.getTexts('h1, h2')).toEqual(['Basic', 'Home'])
})
test('getElementCount', async () => {
expect(await page.getElementCount('meta')).toBe(2)
})
test('getTitle', async () => {
expect(await page.getTitle()).toBe('Home | Vue Meta Test')
})
test('run(Async)Script', async () => {
try {
await page.navigate('/about')
} catch (e) {}
expect(await page.routeData()).toMatchObject({
path: '/about'
})
expect(await page.getTitle()).toBe('About')
})
test('clickElement', async () => {
await page.clickElement('a')
await waitFor(1000)
expect(await page.routeData()).toMatchObject({
path: '/'
})
expect(await page.getTitle()).toBe('Home | Vue Meta Test')
})
test('click', async () => {
try {
await page.navigateByClick('a')
} catch (e) {}
expect(await page.routeData()).toMatchObject({
path: '/about'
})
expect(await page.getTitle()).toBe('About')
})
test('Doesnt fail on non-existing elements', async () => {
await expect(page.getText('.non-existing-element-class')).resolves.toBeNull()
await expect(page.getTexts('.non-existing-element-class')).resolves.toEqual([])
})
test('getText with trim', async () => {
expect(await page.getText('body', true)).toEqual('Basic About Go to Home Inspect Element to see the meta info')
})
test('getTexts with trim', async () => {
expect(await page.getTexts('div', true)).toEqual(['Basic About Go to Home Inspect Element to see the meta info', 'About Go to Home'])
})
})