Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add PuppeteerReplayStringifyExtension #381

Merged
merged 3 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions __snapshots__/PuppeteerReplayStringifyExtension.test.ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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,
"assertedEvents": [
{
"type": "navigation"
}
]
});

`;

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()
}

`;
16 changes: 12 additions & 4 deletions src/InMemoryLineWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ export class InMemoryLineWriter implements LineWriter {
}

appendLine(line: string): LineWriter {
const indentedLine = line
? this.#indentation.repeat(this.#currentIndentation) + line.trimEnd()
: '';
this.#lines.push(indentedLine);
const lines = line.split('\n').map((line) => {
const indentedLine = line
? this.#indentation.repeat(this.#currentIndentation) + line.trimEnd()
: '';
return indentedLine;
});

this.#lines.push(...lines);
return this;
}

Expand All @@ -47,4 +51,8 @@ export class InMemoryLineWriter implements LineWriter {
// Scripts should end with a final blank line.
return this.#lines.join('\n') + '\n';
}

getIndent(): string {
return this.#indentation;
}
}
1 change: 1 addition & 0 deletions src/LineWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export interface LineWriter {
appendLine(line: string): LineWriter;
startBlock(): LineWriter;
endBlock(): LineWriter;
getIndent(): string;
}
58 changes: 58 additions & 0 deletions src/PuppeteerReplayStringifyExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
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, null, out.getIndent())});`
);
}
}
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';
56 changes: 56 additions & 0 deletions test/PuppeteerReplayStringifyExtension.test.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 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,
assertedEvents: [{ type: AssertedEventType.Navigation as const }],
};
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,
})
);
});
});