-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add PuppeteerReplayStringifyExtension
- Loading branch information
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
__snapshots__/PuppeteerReplayStringifyExtension.test.ts.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)});`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
); | ||
}); | ||
}); |