-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
report: add PSI.prepareLabData() #5804
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6556ef5
add PSI.prepareLabData()
paulirish 030d5f7
tests
paulirish 46dc3aa
move getFinalScreenshot to PSI
paulirish e9d9fbf
lint and types
paulirish bd34c84
feedback.
paulirish e8b7c01
node <10.x needs thsi global.URL set.
paulirish 930c0b9
refactor handling the PSI environment
paulirish c136de3
sort the test assertions.
paulirish cea947e
feedback
paulirish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @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'; | ||
|
||
/* globals self DOM PerformanceCategoryRenderer Util DetailsRenderer */ | ||
|
||
class PSI { | ||
/** | ||
* Returns all the elements that PSI needs to render the report | ||
* We expose this helper method to minimize the 'public' API surface of the renderer | ||
* and allow us to refactor without two-sided patches. | ||
* | ||
* const {scoreGaugeEl, perfCategoryEl, finalScreenshotDataUri} = PSI.prepareLabData( | ||
* LHResultJsonString, | ||
* document | ||
* ); | ||
* | ||
* @param {string} LHResultJsonString The stringified version of {LH.Result} | ||
* @param {Document} document The host page's window.document | ||
* @return {{scoreGaugeEl: Element, perfCategoryEl: Element, finalScreenshotDataUri: string|null}} | ||
*/ | ||
static prepareLabData(LHResultJsonString, document) { | ||
const lhResult = /** @type {LH.Result} */ JSON.parse(LHResultJsonString); | ||
const dom = new DOM(document); | ||
|
||
const reportLHR = Util.prepareReportResult(lhResult); | ||
const perfCategory = reportLHR.reportCategories.find(cat => cat.id === 'performance'); | ||
if (!perfCategory) throw new Error(`No performance category. Can't make lab data section`); | ||
if (!reportLHR.categoryGroups) throw new Error(`No category groups found.`); | ||
|
||
const perfRenderer = new PerformanceCategoryRenderer(dom, new DetailsRenderer(dom)); | ||
const perfCategoryEl = perfRenderer.render(perfCategory, reportLHR.categoryGroups); | ||
|
||
const scoreGaugeEl = dom.find('.lh-score__gauge', perfCategoryEl); | ||
const scoreGaugeWrapper = dom.find('.lh-gauge__wrapper', scoreGaugeEl); | ||
scoreGaugeWrapper.classList.add('lh-gauge__wrapper--huge'); | ||
// Remove Performance category title/description | ||
dom.find('.lh-category-header', perfCategoryEl).remove(); | ||
// Remove navigation links | ||
scoreGaugeWrapper.removeAttribute('href'); | ||
dom.find('.lh-permalink', perfCategoryEl).remove(); | ||
|
||
const finalScreenshotDataUri = PSI.getFinalScreenshot(perfCategory); | ||
return {scoreGaugeEl, perfCategoryEl, finalScreenshotDataUri}; | ||
} | ||
|
||
/** | ||
* @param {LH.ReportResult.Category} perfCategory | ||
* @return {null|string} | ||
*/ | ||
static getFinalScreenshot(perfCategory) { | ||
const auditRef = perfCategory.auditRefs.find(audit => audit.id === 'final-screenshot'); | ||
if (!auditRef || !auditRef.result || auditRef.result.scoreDisplayMode === 'error') return null; | ||
return auditRef.result.details.data; | ||
} | ||
} | ||
|
||
if (typeof module !== 'undefined' && module.exports) { | ||
module.exports = PSI; | ||
} else { | ||
self.PSI = PSI; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* @license Copyright 2017 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'; | ||
|
||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const jsdom = require('jsdom'); | ||
|
||
const PSI = require('../../../../report/html/renderer/psi.js'); | ||
const Util = require('../../../../report/html/renderer/util.js'); | ||
const DOM = require('../../../../report/html/renderer/dom.js'); | ||
const CategoryRenderer = require('../../../../report/html/renderer/category-renderer'); | ||
const DetailsRenderer = require('../../../../report/html/renderer/details-renderer'); | ||
const CriticalRequestChainRenderer = | ||
require('../../../../report/html/renderer/crc-details-renderer'); | ||
|
||
const sampleResultsStr = fs.readFileSync(__dirname + '/../../../results/sample_v2.json', 'utf-8'); | ||
const sampleResults = JSON.parse(sampleResultsStr) | ||
; | ||
const TEMPLATE_FILE = fs.readFileSync( | ||
__dirname + '/../../../../report/html/templates.html', | ||
'utf8' | ||
); | ||
|
||
/* eslint-env jest */ | ||
|
||
describe('DOM', () => { | ||
let document; | ||
beforeAll(() => { | ||
global.Util = Util; | ||
global.DOM = DOM; | ||
global.CategoryRenderer = CategoryRenderer; | ||
global.DetailsRenderer = DetailsRenderer; | ||
|
||
// Delayed so that CategoryRenderer is in global scope | ||
const PerformanceCategoryRenderer = | ||
require('../../../../report/html/renderer/performance-category-renderer'); | ||
global.PerformanceCategoryRenderer = PerformanceCategoryRenderer; | ||
global.CriticalRequestChainRenderer = CriticalRequestChainRenderer; | ||
|
||
document = jsdom.jsdom(TEMPLATE_FILE); | ||
}); | ||
|
||
afterAll(() => { | ||
global.Util = undefined; | ||
global.DOM = undefined; | ||
global.CategoryRenderer = undefined; | ||
global.DetailsRenderer = undefined; | ||
global.PerformanceCategoryRenderer = undefined; | ||
global.CriticalRequestChainRenderer = undefined; | ||
}); | ||
|
||
describe('psi prepareLabData helpers', () => { | ||
describe('prepareLabData', () => { | ||
it('reports expected data', () => { | ||
const result = PSI.prepareLabData(sampleResultsStr, document); | ||
assert.ok(result.scoreGaugeEl instanceof document.defaultView.Element); | ||
assert.ok(result.perfCategoryEl instanceof document.defaultView.Element); | ||
assert.equal(typeof result.finalScreenshotDataUri, 'string'); | ||
|
||
assert.ok(result.finalScreenshotDataUri.startsWith('data:image/jpeg;base64,')); | ||
assert.ok(result.scoreGaugeEl.outerHTML.includes('<style>'), 'score gauge comes with CSS'); | ||
assert.ok(result.scoreGaugeEl.outerHTML.includes('<svg'), 'score gauge comes with SVG'); | ||
assert.ok(result.perfCategoryEl.outerHTML.length > 50000, 'perfCategory HTML is populated'); | ||
}); | ||
|
||
it('throws if there is no perf category', () => { | ||
const lhrWithoutPerf = JSON.parse(sampleResultsStr); | ||
delete lhrWithoutPerf.categories.performance; | ||
const lhrWithoutPerfStr = JSON.stringify(lhrWithoutPerf); | ||
|
||
assert.throws(() => { | ||
PSI.prepareLabData(lhrWithoutPerfStr, document); | ||
}, /no performance category/i); | ||
}); | ||
|
||
it('throws if there is no category groups', () => { | ||
const lhrWithoutGroups = JSON.parse(sampleResultsStr); | ||
delete lhrWithoutGroups.categoryGroups; | ||
const lhrWithoutGroupsStr = JSON.stringify(lhrWithoutGroups); | ||
|
||
assert.throws(() => { | ||
PSI.prepareLabData(lhrWithoutGroupsStr, document); | ||
}, /no category groups/i); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getFinalScreenshot', () => { | ||
it('gets a datauri as a string', () => { | ||
const cloneResults = Util.prepareReportResult(sampleResults); | ||
const perfCategory = cloneResults.reportCategories.find(cat => cat.id === 'performance'); | ||
const datauri = PSI.getFinalScreenshot(perfCategory); | ||
assert.equal(typeof datauri, 'string'); | ||
assert.ok(datauri.startsWith('data:image/jpeg;base64,')); | ||
}); | ||
|
||
it('returns null if there is no final-screenshot audit', () => { | ||
const clonedResults = JSON.parse(JSON.stringify(sampleResults)); | ||
delete clonedResults.audits['final-screenshot']; | ||
const lhrNoFinalSS = Util.prepareReportResult(clonedResults); | ||
const perfCategory = lhrNoFinalSS.reportCategories.find(cat => cat.id === 'performance'); | ||
|
||
const datauri = PSI.getFinalScreenshot(perfCategory); | ||
assert.equal(datauri, null); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these
remove()
s are definitely the hackiest bits. Could we do with CSS or have a method for it onCategoryRenderer
or something else?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
impl-wise it really comes down to avoiding these two lines, so yah we can easily avoid these .remove's.
this._dom.isDevTools()
we add athis._dom.isPSI()
and drop it into PCR.render().display:none
) is possible and technically safe for screenreaders, but it feels a tad messy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this has its downsides, but since we have precedent with
isDevTools()
it seems (to me) like the best option out of these (besides a.isPSI
CSS class, but seems like you really don't like that :)I was hoping if I suggested it vaguely enough you'd assume I meant something in particular and bring that back as a possible solution :)