Skip to content

Commit

Permalink
fix: record correct screenshots for popups (#353)
Browse files Browse the repository at this point in the history
* fix: record correct screenshots for popups

* use last active open page

* add tests
  • Loading branch information
vigneshshanmugam committed Jul 27, 2021
1 parent e43fb26 commit fd08098
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
33 changes: 33 additions & 0 deletions __tests__/core/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,37 @@ describe('runner', () => {
},
]);
});

it('run - differentiate screenshots for popups', async () => {
const j1 = journey('j1', async ({ page, context }) => {
step('visit test page', async () => {
await page.goto(server.TEST_PAGE);
await page.setContent(
'<a target=_blank rel=noopener href="/popup.html">popup</a>'
);
});
step('click popup', async () => {
const [page1] = await Promise.all([
context.waitForEvent('page'),
page.click('a'),
]);
await page1.waitForLoadState();
});
});
runner.addJourney(j1);

await runner.run({
wsEndpoint,
reporter: 'json',
screenshots: 'on',
outfd: fs.openSync(dest, 'w'),
});

const screenshotDocs = readAndCloseStreamJson().filter(
({ type }) => type === 'step/screenshot'
);
expect(screenshotDocs.length).toEqual(2);
const blobs = screenshotDocs.map(data => data.blob);
expect(blobs[0]).not.toEqual(blobs[1]);
});
});
13 changes: 10 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,16 @@ if (process.env.DEBUG === namespace || Boolean(options.debug)) {
}

const loadInlineScript = source => {
const scriptFn = new Function('step', 'page', 'browser', 'params', source);
journey('inline', async ({ page, browser, params }) => {
scriptFn.apply(null, [step, page, browser, params]);
const scriptFn = new Function(
'step',
'page',
'context',
'browser',
'params',
source
);
journey('inline', async ({ page, context, browser, params }) => {
scriptFn.apply(null, [step, page, context, browser, params]);
});
};

Expand Down
17 changes: 14 additions & 3 deletions src/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export default class Runner extends EventEmitter {
driver.context.off('request', captureUrl);
};
driver.context.on('request', captureUrl);

try {
pluginManager.onStep(step);
await step.callback();
Expand All @@ -243,9 +244,19 @@ export default class Runner extends EventEmitter {
data.status = 'failed';
data.error = error;
} finally {
data.url ??= driver.page.url();
if (screenshots && screenshots !== 'off') {
await this.captureScreenshot(driver.page, step);
/**
* Capture screenshot for the newly created pages
* via popup or new windows/tabs
*
* Last open page will get us the correct screenshot
*/
const pages = driver.context.pages();
const page = pages[pages.length - 1];
if (page) {
data.url ??= page.url();
if (screenshots && screenshots !== 'off') {
await this.captureScreenshot(page, step);
}
}
}
log(`Runner: end step (${step.name})`);
Expand Down

0 comments on commit fd08098

Please sign in to comment.