Skip to content

Commit

Permalink
feat: add PuppeteerReplayStringifyExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Nov 16, 2022
1 parent 869fb9d commit 5b7dc8e
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
24 changes: 24 additions & 0 deletions __snapshots__/PuppeteerReplayStringifyExtension.test.ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
exports['PuppeteerReplayStringifyExtension should print the script for a click step 1'] = `
await runner.runStep({"type":"click","target":"main","selectors":["aria/Test"],"offsetX":1,"offsetY":1});
`

exports['PuppeteerReplayStringifyExtension should print an entire script 1'] = `
import url from 'url';
import { createRunner } from '@puppeteer/replay';
export async function run(extension) {
const runner = await createRunner(extension);
await runner.runBeforeAllSteps();
await runner.runStep({"type":"click","target":"main","selectors":["aria/Test"],"offsetX":1,"offsetY":1,"assertedEvents":[{"type":"navigation"}]});
await runner.runAfterAllSteps();
}
if (process && import.meta.url === url.pathToFileURL(process.argv[1]).href) {
run()
}
`
56 changes: 56 additions & 0 deletions src/PuppeteerReplayStringifyExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
Copyright 2022 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import type { LineWriter } from './LineWriter.js';
import type { Step } from './Schema.js';
import { StringifyExtension } from './StringifyExtension.js';

/**
* Stringifies a user flow to a script that uses \@puppeteer/replay's own API.
*/
export class PuppeteerReplayStringifyExtension extends StringifyExtension {
override async beforeAllSteps(out: LineWriter) {
out.appendLine("import url from 'url';");
out.appendLine("import { createRunner } from '@puppeteer/replay';");
out.appendLine('');
out.appendLine('export async function run(extension) {').startBlock();
out.appendLine('const runner = await createRunner(extension);');
out.appendLine('');
out.appendLine('await runner.runBeforeAllSteps();');
out.appendLine('');
}

override async afterAllSteps(out: LineWriter) {
out.appendLine('');
out
.appendLine('await runner.runAfterAllSteps();')
.endBlock()
.appendLine('}');
out.appendLine('');
out
.appendLine(
'if (process && import.meta.url === url.pathToFileURL(process.argv[1]).href) {'
)
.startBlock()
.appendLine('run()')
.endBlock()
.appendLine('}');
}

override async stringifyStep(out: LineWriter, step: Step) {
out.appendLine(`await runner.runStep(${JSON.stringify(step)});`);
}
}
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export { createRunner, Runner } from './Runner.js';
export { PuppeteerRunnerExtension } from './PuppeteerRunnerExtension.js';
export { PuppeteerRunnerOwningBrowserExtension } from './PuppeteerRunnerExtension.js';
export { PuppeteerStringifyExtension } from './PuppeteerStringifyExtension.js';
export { PuppeteerReplayStringifyExtension } from './PuppeteerReplayStringifyExtension.js';
export { LighthouseStringifyExtension } from './lighthouse/LighthouseStringifyExtension.js';
55 changes: 55 additions & 0 deletions test/PuppeteerReplayStringifyExtension.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
Copyright 2022 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import snapshot from 'snap-shot-it';
import { stringify } from '../src/stringify.js';
import { InMemoryLineWriter } from '../src/InMemoryLineWriter.js';
import { PuppeteerReplayStringifyExtension } from '../src/PuppeteerReplayStringifyExtension.js';
import { StepType, AssertedEventType } from '../src/Schema.js';

describe('PuppeteerReplayStringifyExtension', () => {
const ext = new PuppeteerReplayStringifyExtension();

it('should print the script for a click step', async () => {
const step = {
type: StepType.Click as const,
target: 'main',
selectors: ['aria/Test'],
offsetX: 1,
offsetY: 1,
};
const writer = new InMemoryLineWriter(' ');
await ext.stringifyStep(writer, step);
snapshot(writer.toString());
});

it('should print an entire script', async () => {
const step = {
type: StepType.Click as const,
target: 'main',
selectors: ['aria/Test'],
offsetX: 1,
offsetY: 1,
assertedEvents: [{ type: AssertedEventType.Navigation as const }],
};
const flow = { title: 'test', steps: [step] };
snapshot(
await stringify(flow, {
extension: ext,
})
);
});
});

0 comments on commit 5b7dc8e

Please sign in to comment.