Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine committed Aug 14, 2023
1 parent b48c205 commit 120dd29
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
7 changes: 7 additions & 0 deletions core/test/scenarios/api-test-pptr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
});
Expand Down Expand Up @@ -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`,
});
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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({
Expand Down
70 changes: 36 additions & 34 deletions core/test/scenarios/pptr-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,77 +33,79 @@ 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
* @param {number=} secondaryPort
*/
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();
});
},

installSetupAndTeardownHooks() {
this.installServerHooks();

before(async () => {
browser = await puppeteer.launch({
this.browser = await puppeteer.launch({
headless: true,
executablePath: getChromePath(),
ignoreDefaultArgs: ['--enable-automation'],
});
});

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;
},
};
}

Expand Down
2 changes: 2 additions & 0 deletions core/test/scenarios/start-end-navigation-test-pptr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down

0 comments on commit 120dd29

Please sign in to comment.