diff --git a/core/test/scenarios/api-test-pptr.js b/core/test/scenarios/api-test-pptr.js index b293ee81d286..811141e86467 100644 --- a/core/test/scenarios/api-test-pptr.js +++ b/core/test/scenarios/api-test-pptr.js @@ -75,6 +75,7 @@ describe('Individual modes API', function() { if (!result) throw new Error('Lighthouse failed to produce a result'); const {lhr, artifacts} = result; + state.saveTrace(artifacts.Trace); expect(artifacts.URL).toEqual({ finalDisplayedUrl: `${state.serverBaseUrl}/onclick.html#done`, }); @@ -133,6 +134,8 @@ describe('Individual modes API', function() { if (!result) throw new Error('Lighthouse failed to produce a result'); + state.saveTrace(result.artifacts.Trace); + expect(result.artifacts.URL).toEqual({ finalDisplayedUrl: `${serverBaseUrl}/onclick.html#done`, }); @@ -165,6 +168,8 @@ describe('Individual modes API', function() { if (!result) throw new Error('Lighthouse failed to produce a result'); + state.saveTrace(result.artifacts.Trace); + const networkRequestsDetails = /** @type {LH.Audit.Details.Table} */ ( result.lhr.audits['network-requests'].details); const networkRequests = networkRequestsDetails?.items @@ -222,6 +227,7 @@ Array [ if (!result) throw new Error('Lighthouse failed to produce a result'); const {lhr, artifacts} = result; + state.saveTrace(artifacts.Trace); expect(artifacts.URL).toEqual({ requestedUrl: url, mainDocumentUrl: url, @@ -263,6 +269,7 @@ Array [ expect(requestor).toHaveBeenCalled(); const {lhr, artifacts} = result; + state.saveTrace(artifacts.Trace); expect(lhr.requestedUrl).toEqual(requestedUrl); expect(lhr.finalDisplayedUrl).toEqual(mainDocumentUrl); expect(artifacts.URL).toEqual({ diff --git a/core/test/scenarios/pptr-test-utils.js b/core/test/scenarios/pptr-test-utils.js index 885dd4611c6a..93278c12eb2b 100644 --- a/core/test/scenarios/pptr-test-utils.js +++ b/core/test/scenarios/pptr-test-utils.js @@ -33,24 +33,16 @@ function createTestState() { throw new Error(`${name} used without invoking \`state.before\``); }}); - /** @type {puppeteer.Browser} */ - let browser = any('browser'); - /** @type {puppeteer.Page} */ - let page = any('page'); - /** @type {StaticServer} */ - let server = any('server'); - /** @type {StaticServer} */ - let secondaryServer = any('server'); - let serverBaseUrl = ''; - let secondaryServerBaseUrl = ''; + /** @type {LH.Trace|undefined} */ + let trace; return { - browser, - page, - server, - secondaryServer, - serverBaseUrl, - secondaryServerBaseUrl, + browser: /** @type {puppeteer.Browser} */ (any('browser')), + page: /** @type {puppeteer.Page} */ (any('page')), + server: /** @type {StaticServer} */ (any('server')), + secondaryServer: /** @type {StaticServer} */ (any('server')), + serverBaseUrl: '', + secondaryServerBaseUrl: '', /** * @param {number=} port @@ -58,17 +50,17 @@ function createTestState() { */ installServerHooks(port = 10200, secondaryPort = 10503) { before(async () => { - server = new Server(port); - secondaryServer = new Server(secondaryPort); - await server.listen(port, '127.0.0.1'); - await secondaryServer.listen(secondaryPort, '127.0.0.1'); - serverBaseUrl = `http://localhost:${this.server.getPort()}`; - secondaryServerBaseUrl = `http://localhost:${this.secondaryServer.getPort()}`; + this.server = new Server(port); + this.secondaryServer = new Server(secondaryPort); + await this.server.listen(port, '127.0.0.1'); + await this.secondaryServer.listen(secondaryPort, '127.0.0.1'); + this.serverBaseUrl = `http://localhost:${this.server.getPort()}`; + this.secondaryServerBaseUrl = `http://localhost:${this.secondaryServer.getPort()}`; }); after(async () => { - await server.close(); - await secondaryServer.close(); + await this.server.close(); + await this.secondaryServer.close(); }); }, @@ -76,7 +68,7 @@ function createTestState() { this.installServerHooks(); before(async () => { - browser = await puppeteer.launch({ + this.browser = await puppeteer.launch({ headless: true, executablePath: getChromePath(), ignoreDefaultArgs: ['--enable-automation'], @@ -84,26 +76,36 @@ function createTestState() { }); beforeEach(async () => { - page = await browser.newPage(); - await page.tracing.start(); + trace = undefined; + this.page = await this.browser.newPage(); }); - afterEach(async function() { - await page.close(); - const traceData = await page.tracing.stop(); + afterEach(async () => { + await this.page.close(); + }); + + afterEach(function() { // eslint-disable-next-line no-invalid-this const currentTest = this.currentTest; - if (currentTest?.state === 'failed' && traceData) { - const testOutputDir = `${UNIT_OUTPUT_DIR}/${currentTest.fullTitle()}`; + if (currentTest?.state === 'failed' && trace) { + const dirname = currentTest.fullTitle().replace(/[^a-z0-9]/gi, '_').toLowerCase(); + const testOutputDir = `${UNIT_OUTPUT_DIR}/${dirname}`; fs.mkdirSync(testOutputDir, {recursive: true}); - fs.writeFileSync(`${testOutputDir}/trace.json`, traceData); + fs.writeFileSync(`${testOutputDir}/trace.json`, JSON.stringify(trace, null, 2)); } }); after(async () => { - await browser.close(); + await this.browser.close(); }); }, + + /** + * @param {LH.Trace} testTrace + */ + saveTrace(testTrace) { + trace = testTrace; + }, }; } diff --git a/core/test/scenarios/start-end-navigation-test-pptr.js b/core/test/scenarios/start-end-navigation-test-pptr.js index 294489bf5a9a..efa50d1e1b2e 100644 --- a/core/test/scenarios/start-end-navigation-test-pptr.js +++ b/core/test/scenarios/start-end-navigation-test-pptr.js @@ -37,6 +37,8 @@ describe('Start/End navigation', function() { const lhr = flowResult.steps[0].lhr; const artifacts = flowArtifacts.gatherSteps[0].artifacts; + state.saveTrace(artifacts.Trace); + expect(artifacts.URL).toEqual({ requestedUrl: `${state.serverBaseUrl}/?redirect=/index.html`, mainDocumentUrl: `${state.serverBaseUrl}/index.html`,