Skip to content

Commit

Permalink
feat: support stringifying flows for Firefox (#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN authored Aug 7, 2024
1 parent 3093447 commit 94319cd
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
6 changes: 3 additions & 3 deletions __snapshots__/LighthouseStringifyExtension.test.ts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
exports['LighthouseStringifyExtension handles ending timespan 1'] = `
const fs = require('fs');
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand Down Expand Up @@ -58,7 +58,7 @@ const puppeteer = require('puppeteer'); // v22.0.0 or later

exports['LighthouseStringifyExtension handles ending navigation 1'] = `
const fs = require('fs');
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand Down Expand Up @@ -130,7 +130,7 @@ exports[
'LighthouseStringifyExtension handles multiple sequential navigations 1'
] = `
const fs = require('fs');
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand Down
12 changes: 12 additions & 0 deletions __snapshots__/PuppeteerStringifyExtension.test.ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,15 @@ exports[
}
`;

exports['PuppeteerStringifyExtension Firefox should stringify 1'] = `
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch({browser: 'firefox'});
const page = await browser.newPage();
const timeout = 5000;
page.setDefaultTimeout(timeout);
`;
2 changes: 1 addition & 1 deletion __snapshots__/lighthouse.test.ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ exports[
'Lighthouse user flow run via stringify produces a valid desktop flow report 1'
] = `
const fs = require('fs');
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand Down
20 changes: 10 additions & 10 deletions __snapshots__/stringify.test.ts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
exports['stringify should print the correct script for a navigate step 1'] = `
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand All @@ -25,7 +25,7 @@ const puppeteer = require('puppeteer'); // v22.0.0 or later
exports[
'stringify should print the correct script for a emulateNetworkCondition step 1'
] = `
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand Down Expand Up @@ -56,7 +56,7 @@ const puppeteer = require('puppeteer'); // v22.0.0 or later
exports[
'stringify should print the correct script if the target is not the main page 1'
] = `
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand Down Expand Up @@ -91,7 +91,7 @@ const puppeteer = require('puppeteer'); // v22.0.0 or later
`;

exports['stringify should use step and flow timeouts 1'] = `
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand Down Expand Up @@ -129,7 +129,7 @@ const puppeteer = require('puppeteer'); // v22.0.0 or later
exports[
'stringify should print the correct script if the step is within an iframe 1'
] = `
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand Down Expand Up @@ -165,7 +165,7 @@ const puppeteer = require('puppeteer'); // v22.0.0 or later
`;

exports['stringify should print the correct script for a keydown step 1'] = `
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand All @@ -189,7 +189,7 @@ const puppeteer = require('puppeteer'); // v22.0.0 or later
`;

exports['stringify should print the correct script for a keyup step 1'] = `
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand All @@ -213,7 +213,7 @@ const puppeteer = require('puppeteer'); // v22.0.0 or later
`;

exports['stringify should print the correct script for scroll events 1'] = `
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand Down Expand Up @@ -247,7 +247,7 @@ const puppeteer = require('puppeteer'); // v22.0.0 or later
exports[
'stringify should print the correct script for waitForElement steps 1'
] = `
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand Down Expand Up @@ -405,7 +405,7 @@ const puppeteer = require('puppeteer'); // v22.0.0 or later
exports[
'stringify should print the correct script for waitForExpression steps 1'
] = `
const puppeteer = require('puppeteer'); // v22.0.0 or later
const puppeteer = require('puppeteer'); // v23.0.0 or later
(async () => {
const browser = await puppeteer.launch();
Expand Down
18 changes: 16 additions & 2 deletions src/PuppeteerStringifyExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,30 @@ import {
} from './SchemaUtils.js';
import { formatJSONAsJS } from './JSONUtils.js';

type TargetBrowser = 'chrome' | 'firefox';

export class PuppeteerStringifyExtension extends StringifyExtension {
#shouldAppendWaitForElementHelper = false;
#targetBrowser: TargetBrowser;

constructor(targetBrowser: TargetBrowser = 'chrome') {
super();
this.#targetBrowser = targetBrowser;
}

override async beforeAllSteps(out: LineWriter, flow: UserFlow) {
out.appendLine(
"const puppeteer = require('puppeteer'); // v22.0.0 or later"
"const puppeteer = require('puppeteer'); // v23.0.0 or later"
);
out.appendLine('');
out.appendLine('(async () => {').startBlock();
out.appendLine('const browser = await puppeteer.launch();');
if (this.#targetBrowser === 'firefox') {
out.appendLine(
`const browser = await puppeteer.launch({browser: 'firefox'});`
);
} else {
out.appendLine('const browser = await puppeteer.launch();');
}
out.appendLine('const page = await browser.newPage();');
out.appendLine(`const timeout = ${flow.timeout || defaultTimeout};`);
out.appendLine('page.setDefaultTimeout(timeout);');
Expand Down
19 changes: 19 additions & 0 deletions test/PuppeteerStringifyExtension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,23 @@ describe('PuppeteerStringifyExtension', () => {
await ext.stringifyStep(writer, step, flow);
snapshot(writer.toString());
});

describe('Firefox', () => {
const ext = new PuppeteerStringifyExtension('firefox');

it('should stringify', async () => {
const step = {
type: StepType.Click as const,
target: 'main',
selectors: ['aria/Test'],
offsetX: 1,
offsetY: 1,
};
const flow = { title: 'test', steps: [step] };

const writer = new InMemoryLineWriter(' ');
await ext.beforeAllSteps(writer, flow);
snapshot(writer.toString());
});
});
});

0 comments on commit 94319cd

Please sign in to comment.