-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
viewer-test-pptr.js
142 lines (120 loc) · 5.22 KB
/
viewer-test-pptr.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
/**
* @license Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
/* eslint-env jest */
const path = require('path');
const assert = require('assert');
const puppeteer = require('../../node_modules/puppeteer/index.js');
const {server} = require('../../lighthouse-cli/test/fixtures/static-server.js');
const portNumber = 10200;
const viewerUrl = `http://localhost:${portNumber}/dist/viewer/index.html`;
const sampleLhr = __dirname + '/../../lighthouse-core/test/results/sample_v2.json';
const config = require(path.resolve(__dirname, '../../lighthouse-core/config/default-config.js'));
const lighthouseCategories = Object.keys(config.categories);
const getAuditsOfCategory = category => config.categories[category].auditRefs;
// TODO: should be combined in some way with clients/test/extension/extension-test.js
describe('Lighthouse Viewer', function() {
// eslint-disable-next-line no-console
console.log('\n✨ Be sure to have recently run this: yarn build-viewer');
let browser;
let viewerPage;
const pageErrors = [];
function getAuditElementsIds({category, selector}) {
return viewerPage.evaluate(
({category, selector}) => {
const elems = document.querySelector(`#${category}`).parentNode.querySelectorAll(selector);
return Array.from(elems).map(el => el.id);
}, {category, selector}
);
}
function getCategoryElementsIds() {
return viewerPage.evaluate(
() => {
const elems = Array.from(document.querySelectorAll(`.lh-category`));
return elems.map(el => {
const permalink = el.querySelector('.lh-permalink');
return permalink && permalink.id;
});
});
}
beforeAll(async function() {
server.listen(portNumber, 'localhost');
// start puppeteer
browser = await puppeteer.launch({
headless: true,
executablePath: process.env.CHROME_PATH,
});
viewerPage = await browser.newPage();
viewerPage.on('pageerror', pageError => pageErrors.push(pageError));
await viewerPage.goto(viewerUrl, {waitUntil: 'networkidle2', timeout: 30000});
const fileInput = await viewerPage.$('#hidden-file-input');
await fileInput.uploadFile(sampleLhr);
await viewerPage.waitForSelector('.lh-container', {timeout: 30000});
});
afterAll(async function() {
// Log any page load errors encountered in case before() failed.
// eslint-disable-next-line no-console
console.error(pageErrors);
await Promise.all([
new Promise(resolve => server.close(resolve)),
browser && browser.close(),
]);
});
const selectors = {
audits: '.lh-audit, .lh-metric',
titles: '.lh-audit__title, .lh-metric__title',
};
it('should load with no errors', async () => {
assert.deepStrictEqual(pageErrors, []);
});
it('should contain all categories', async () => {
const categories = await getCategoryElementsIds();
assert.deepStrictEqual(
categories.sort(),
lighthouseCategories.sort(),
`all categories not found`
);
});
it('should contain audits of all categories', async () => {
for (const category of lighthouseCategories) {
let expected = getAuditsOfCategory(category);
if (category === 'performance') {
expected = getAuditsOfCategory(category).filter(a => !!a.group);
}
expected = expected.map(audit => audit.id);
const elementIds = await getAuditElementsIds({category, selector: selectors.audits});
assert.deepStrictEqual(
elementIds.sort(),
expected.sort(),
`${category} does not have the identical audits`
);
}
});
it('should contain a filmstrip', async () => {
const filmstrip = await viewerPage.$('.lh-filmstrip');
assert.ok(!!filmstrip, `filmstrip is not available`);
});
it('should not have any unexpected audit errors', async () => {
function getErrors(elems, selectors) {
return elems.map(el => {
const audit = el.closest(selectors.audits);
const auditTitle = audit && audit.querySelector(selectors.titles);
return {
explanation: el.textContent,
title: auditTitle ? auditTitle.textContent : 'Audit title unvailable',
};
});
}
const errorSelectors = '.lh-audit-explanation, .tooltip--error';
const auditErrors = await viewerPage.$$eval(errorSelectors, getErrors, selectors);
const errors = auditErrors.filter(item => item.explanation.includes('Audit error:'));
const unexpectedErrrors = errors.filter(item => {
return !item.explanation.includes('Required RobotsTxt gatherer did not run') &&
!item.explanation.includes('Required TapTargets gatherer did not run');
});
assert.deepStrictEqual(unexpectedErrrors, [], 'Audit errors found within the report');
});
});