From dbe03310ebd1919af4bba855c19d9136838ca58a Mon Sep 17 00:00:00 2001 From: pujitha7 Date: Thu, 10 Sep 2020 21:25:31 +0530 Subject: [PATCH 1/3] report: show overall category scores in csv output --- lighthouse-core/report/report-generator.js | 16 ++++++++++------ .../test/report/report-generator-test.js | 8 +++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lighthouse-core/report/report-generator.js b/lighthouse-core/report/report-generator.js index 8127686aa144..b599af862ec7 100644 --- a/lighthouse-core/report/report-generator.js +++ b/lighthouse-core/report/report-generator.js @@ -66,19 +66,23 @@ class ReportGenerator { const separator = ','; /** @param {string} value @return {string} */ const escape = value => `"${value.replace(/"/g, '""')}"`; + /** @param {Array} row @return {string[]} */ + const rowFormatter = row => row.map(value => value.toString()).map(escape); // Possible TODO: tightly couple headers and row values const header = ['requestedUrl', 'finalUrl', 'category', 'name', 'title', 'type', 'score']; const table = Object.values(lhr.categories).map(category => { - return category.auditRefs.map(auditRef => { + const rows = []; + const overallCategoryScore = category.score === null ? -1 : category.score; + rows.push(rowFormatter([lhr.requestedUrl, lhr.finalUrl, category.title, + 'overall-category-score', 'Overall Category Score', 'numeric', overallCategoryScore])); + return rows.concat(category.auditRefs.map(auditRef => { const audit = lhr.audits[auditRef.id]; // CSV validator wants all scores to be numeric, use -1 for now const numericScore = audit.score === null ? -1 : audit.score; - return [lhr.requestedUrl, lhr.finalUrl, category.title, audit.id, audit.title, - audit.scoreDisplayMode, numericScore] - .map(value => value.toString()) - .map(escape); - }); + return rowFormatter([lhr.requestedUrl, lhr.finalUrl, category.title, audit.id, audit.title, + audit.scoreDisplayMode, numericScore]); + })); }); return [header].concat(...table) diff --git a/lighthouse-core/test/report/report-generator-test.js b/lighthouse-core/test/report/report-generator-test.js index 5f4e86dc2539..1146552c6f5f 100644 --- a/lighthouse-core/test/report/report-generator-test.js +++ b/lighthouse-core/test/report/report-generator-test.js @@ -104,7 +104,7 @@ describe('ReportGenerator', () => { expect(lines.length).toBeGreaterThan(100); expect(lines.slice(0, 2).join('\n')).toMatchInlineSnapshot(` "requestedUrl,finalUrl,category,name,title,type,score - \\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"Performance\\",\\"first-contentful-paint\\",\\"First Contentful Paint\\",\\"numeric\\",\\"0.51\\" + \\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"Performance\\",\\"overall-category-score\\",\\"Overall Category Score\\",\\"numeric\\",\\"0.64\\" " `); @@ -117,6 +117,12 @@ describe('ReportGenerator', () => { } }); + it('creates CSV for results including overall category scores', async () => { + const csvOutput = ReportGenerator.generateReport(sampleResults, 'csv'); + const count = (csvOutput.match(/overall-category-score/g) || []).length; + expect(count).toBe(5); + }); + it('writes extended info', () => { const htmlOutput = ReportGenerator.generateReport(sampleResults, 'html'); const outputCheck = new RegExp('dobetterweb/dbw_tester.css', 'i'); From 093bfabfe34be02f100455c0cc53ee5597184ef3 Mon Sep 17 00:00:00 2001 From: pujitha7 Date: Thu, 10 Sep 2020 22:40:26 +0530 Subject: [PATCH 2/3] report: add category names to column values --- lighthouse-core/report/report-generator.js | 6 ++++-- lighthouse-core/test/report/report-generator-test.js | 12 ++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lighthouse-core/report/report-generator.js b/lighthouse-core/report/report-generator.js index b599af862ec7..9996be60f9ea 100644 --- a/lighthouse-core/report/report-generator.js +++ b/lighthouse-core/report/report-generator.js @@ -71,11 +71,13 @@ class ReportGenerator { // Possible TODO: tightly couple headers and row values const header = ['requestedUrl', 'finalUrl', 'category', 'name', 'title', 'type', 'score']; - const table = Object.values(lhr.categories).map(category => { + const table = Object.keys(lhr.categories).map(categoryId => { const rows = []; + const category = lhr.categories[categoryId]; const overallCategoryScore = category.score === null ? -1 : category.score; rows.push(rowFormatter([lhr.requestedUrl, lhr.finalUrl, category.title, - 'overall-category-score', 'Overall Category Score', 'numeric', overallCategoryScore])); + `${categoryId}-score`, `Overall ${category.title} Category Score`, 'numeric', + overallCategoryScore])); return rows.concat(category.auditRefs.map(auditRef => { const audit = lhr.audits[auditRef.id]; // CSV validator wants all scores to be numeric, use -1 for now diff --git a/lighthouse-core/test/report/report-generator-test.js b/lighthouse-core/test/report/report-generator-test.js index 1146552c6f5f..7db805dd207e 100644 --- a/lighthouse-core/test/report/report-generator-test.js +++ b/lighthouse-core/test/report/report-generator-test.js @@ -102,9 +102,10 @@ describe('ReportGenerator', () => { const lines = csvOutput.split('\n'); expect(lines.length).toBeGreaterThan(100); - expect(lines.slice(0, 2).join('\n')).toMatchInlineSnapshot(` + expect(lines.slice(0, 3).join('\n')).toMatchInlineSnapshot(` "requestedUrl,finalUrl,category,name,title,type,score - \\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"Performance\\",\\"overall-category-score\\",\\"Overall Category Score\\",\\"numeric\\",\\"0.64\\" + \\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"Performance\\",\\"performance-score\\",\\"Overall Performance Category Score\\",\\"numeric\\",\\"0.64\\" + \\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"http://localhost:10200/dobetterweb/dbw_tester.html\\",\\"Performance\\",\\"first-contentful-paint\\",\\"First Contentful Paint\\",\\"numeric\\",\\"0.51\\" " `); @@ -119,8 +120,11 @@ describe('ReportGenerator', () => { it('creates CSV for results including overall category scores', async () => { const csvOutput = ReportGenerator.generateReport(sampleResults, 'csv'); - const count = (csvOutput.match(/overall-category-score/g) || []).length; - expect(count).toBe(5); + csvOutput.includes('performance-score'); + csvOutput.includes('accessibility-score'); + csvOutput.includes('best-practices-score'); + csvOutput.includes('seo-score'); + csvOutput.includes('pwa-score'); }); it('writes extended info', () => { From 23d7519ff82e5ddfdbf186002544cc124eaedad2 Mon Sep 17 00:00:00 2001 From: pujitha7 Date: Fri, 11 Sep 2020 08:56:24 +0530 Subject: [PATCH 3/3] report: fix test case --- lighthouse-core/test/report/report-generator-test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lighthouse-core/test/report/report-generator-test.js b/lighthouse-core/test/report/report-generator-test.js index 7db805dd207e..bcc649a83205 100644 --- a/lighthouse-core/test/report/report-generator-test.js +++ b/lighthouse-core/test/report/report-generator-test.js @@ -118,13 +118,13 @@ describe('ReportGenerator', () => { } }); - it('creates CSV for results including overall category scores', async () => { + it('creates CSV for results including overall category scores', () => { const csvOutput = ReportGenerator.generateReport(sampleResults, 'csv'); - csvOutput.includes('performance-score'); - csvOutput.includes('accessibility-score'); - csvOutput.includes('best-practices-score'); - csvOutput.includes('seo-score'); - csvOutput.includes('pwa-score'); + expect(csvOutput).toContain('performance-score'); + expect(csvOutput).toContain('accessibility-score'); + expect(csvOutput).toContain('best-practices-score'); + expect(csvOutput).toContain('seo-score'); + expect(csvOutput).toContain('pwa-score'); }); it('writes extended info', () => {