forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.js
165 lines (138 loc) · 6.42 KB
/
browser.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
/* global page, browser */
const sleep = require('await-sleep')
const querystring = require('querystring')
describe('homepage', () => {
test('should be titled "GitHub Documentation"', async () => {
await page.goto('http://localhost:4001')
await expect(page.title()).resolves.toMatch('GitHub Documentation')
})
})
describe('algolia browser search', () => {
it('works on the homepage', async () => {
await page.goto('http://localhost:4001/en')
await page.click('#search-input-container input[type="search"]')
await page.type('#search-input-container input[type="search"]', 'actions')
await page.waitForSelector('.ais-Hits')
const hits = await page.$$('.ais-Hits-item')
expect(hits.length).toBeGreaterThan(5)
})
it('works on article pages', async () => {
await page.goto('http://localhost:4001/en/actions')
await page.click('#search-input-container input[type="search"]')
await page.type('#search-input-container input[type="search"]', 'workflows')
await page.waitForSelector('.ais-Hits')
const hits = await page.$$('.ais-Hits-item')
expect(hits.length).toBeGreaterThan(5)
})
it('works on 404 error page', async () => {
await page.goto('http://localhost:4001/en/404')
await page.click('#search-input-container input[type="search"]')
await page.type('#search-input-container input[type="search"]', 'actions')
await page.waitForSelector('.ais-Hits')
const hits = await page.$$('.ais-Hits-item')
expect(hits.length).toBeGreaterThan(5)
})
it('sends the correct data to algolia', async () => {
const newPage = await browser.newPage()
await newPage.goto('http://localhost:4001/ja/enterprise/2.22/admin/installation')
await newPage.setRequestInterception(true)
newPage.on('request', interceptedRequest => {
if (interceptedRequest.method() === 'POST' && /algolia/i.test(interceptedRequest.url())) {
const data = JSON.parse(interceptedRequest.postData())
const { indexName, params } = data.requests[0]
const parsedParams = querystring.parse(params)
const analyticsTags = JSON.parse(parsedParams.analyticsTags)
expect(indexName).toBe('github-docs-2.22-ja')
expect(analyticsTags).toHaveLength(2)
// browser tests are run against production build, so we are expecting env:production
expect(analyticsTags).toEqual(expect.arrayContaining(['site:docs.github.com', 'env:production']))
}
interceptedRequest.continue()
})
await newPage.click('#search-input-container input[type="search"]')
await newPage.type('#search-input-container input[type="search"]', 'test')
})
it('removes `algolia-query` query param after page load', async () => {
await page.goto('http://localhost:4001/en?algolia-query=helpme')
// check that the query is still present at page load
let location = await getLocationObject(page)
expect(location.search).toBe('?algolia-query=helpme')
// query removal is in a setInterval, so wait a bit
await sleep(1000)
// check that the query has been removed after a bit
location = await getLocationObject(page)
expect(location.search).toBe('')
})
it('does not remove hash when removing `algolia-query` query', async () => {
await page.goto('http://localhost:4001/en?algolia-query=helpme#some-header')
// check that the query is still present at page load
let location = await getLocationObject(page)
expect(location.search).toBe('?algolia-query=helpme')
// query removal is in a setInterval, so wait a bit
await sleep(1000)
// check that the query has been removed after a bit
location = await getLocationObject(page)
expect(location.search).toBe('')
expect(location.hash).toBe('#some-header')
})
})
describe('google analytics', () => {
it('is set on page load with expected properties', async () => {
await page.goto('http://localhost:4001/en/actions')
// check that GA global object exists and is a function
const gaObjectType = await page.evaluate(() => typeof window.ga)
expect(gaObjectType).toBe('function')
// check that default tracker is set
// https://developers.google.com/analytics/devguides/collection/analyticsjs/ga-object-methods-reference#getByName
const gaDefaultTracker = await page.evaluate(() => window.ga.getByName('t0'))
expect('filters' in gaDefaultTracker).toBe(true)
expect(Object.keys(gaDefaultTracker)).toHaveLength(3)
// check that default cookies are set
// https://developers.google.com/analytics/devguides/collection/analyticsjs/cookie-usage#analyticsjs
const cookies = await page.cookies()
expect(cookies.some(cookie => cookie.name === '_gat')).toBe(true)
expect(cookies.some(cookie => cookie.name === '_gid')).toBe(true)
})
})
describe('helpfulness', () => {
it('sends an event to /events when submitting form', async () => {
// Visit a page that displays the prompt
await page.goto('http://localhost:4001/en/actions/getting-started-with-github-actions/about-github-actions')
// Track network requests
await page.setRequestInterception(true)
page.on('request', request => {
// Ignore GET to google analytics
if (!/\/events/.test(request.method())) return request.continue()
expect(request.method()).toMatch(/POST|PUT/)
request.respond({
contentType: 'application/json',
body: JSON.stringify({ id: 'abcd1234' }),
status: 200
})
})
// When I click the "Yes" button
await page.click('#helpfulness-sm [for=helpfulness-yes-sm]')
// (sent a POST request to /events)
// I see the request for my email
await page.waitForSelector('#helpfulness-sm [type="email"]')
// When I fill in my email and submit the form
await page.type('#helpfulness-sm [type="email"]', '[email protected]')
await sleep(1000)
await page.click('#helpfulness-sm [type="submit"]')
// (sent a PUT request to /events/{id})
// I see the feedback
await page.waitForSelector('#helpfulness-sm [data-help-end]')
})
})
describe('csrf meta', () => {
it('should have a csrf-token meta tag on the page', async () => {
await page.goto('http://localhost:4001/en/actions/getting-started-with-github-actions/about-github-actions')
await page.waitForSelector('meta[name="csrf-token"]')
})
})
async function getLocationObject (page) {
const location = await page.evaluate(() => {
return Promise.resolve(JSON.stringify(window.location, null, 2))
})
return JSON.parse(location)
}