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: run all recording files in directory with cli #184

Merged
merged 2 commits into from
Jun 22, 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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ In your `package.json` add a new script to invoke the `replay` command:
}
```

You can also give folder name as a parameter to run all the files in a folder.

Using CLI + npx:

```bash
npx @puppeteer/replay all-recordings # runs all recordings in the "all-recordings" folder.
```

Using CLI + package.json:

```json
{
"scripts": {
"replay": "replay all-recordings"
}
}
```

Set the `PUPPETEER_HEADLESS` environment variable or `--headless` CLI flag to control whether the browser is start in a headful or headless mode. For example,

```
Expand Down
39 changes: 37 additions & 2 deletions src/CLIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,47 @@
*/

import { parse, createRunner } from '../lib/main.js';
import { readFileSync } from 'fs';
import { join, isAbsolute } from 'path';
import { readFileSync, readdirSync, lstatSync } from 'fs';
import { join, isAbsolute, extname } from 'path';
import { pathToFileURL } from 'url';
import { cwd } from 'process';
import { PuppeteerRunnerOwningBrowserExtension } from '../lib/main.js';

export function getJSONFilesFromFolder(path: string): string[] {
return readdirSync(path)
.filter((file) => extname(file) === '.json')
.map((file) => join(path, file));
}

export function getRecordingPaths(
paths: string[],
log: boolean = true
): string[] {
const recordingPaths: string[] = [];

for (const path of paths) {
let isDirectory;
try {
isDirectory = lstatSync(path).isDirectory();
} catch (err) {
log && console.error(`Couldn't find file/folder: ${path}`, err);

continue;
}

if (isDirectory) {
const filesInFolder = getJSONFilesFromFolder(path);

if (!filesInFolder.length)
log && console.error(`There is no recordings in: ${path}`);

recordingPaths.push(...filesInFolder);
} else recordingPaths.push(path);
}

return recordingPaths;
}

export function getHeadlessEnvVar(headless?: string) {
if (!headless) {
return true;
Expand Down
7 changes: 4 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

import { getHeadlessEnvVar, runFiles } from './CLIUtils.js';
import { getHeadlessEnvVar, getRecordingPaths, runFiles } from './CLIUtils.js';

interface Arguments {
files: string[];
Expand All @@ -32,7 +31,9 @@ yargs(hideBin(process.argv))
() => {},
async (argv) => {
const args = argv as unknown as Arguments;
await runFiles(args.files, {
const recordingPaths = getRecordingPaths(args.files);

await runFiles(recordingPaths, {
log: true,
headless: getHeadlessEnvVar(
args.headless || process.env.PUPPETEER_HEADLESS
Expand Down
43 changes: 42 additions & 1 deletion test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
limitations under the License.
*/

import { runFiles, getHeadlessEnvVar } from '../src/CLIUtils.js';
import {
runFiles,
getHeadlessEnvVar,
getRecordingPaths,
getJSONFilesFromFolder,
} from '../src/CLIUtils.js';
import { assert } from 'chai';
import path from 'path';
import url from 'url';
Expand Down Expand Up @@ -42,6 +47,20 @@ describe('cli', () => {
);
});

it('is not able to run', async () => {
assert.isFalse(
await runFiles([path.join(__dirname, 'resources', 'replay-fail.json')])
);
});

it('is able to run able to run folder of recordings', async () => {
const recordings = getJSONFilesFromFolder(
path.join(__dirname, 'resources')
);

assert.isFalse(await runFiles([...recordings]));
});

it('is able to run successfully with an extension', async () => {
assert.isTrue(
await runFiles([path.join(__dirname, 'resources', 'replay.json')], {
Expand All @@ -52,4 +71,26 @@ describe('cli', () => {
);
});
});

describe('getRecordingPaths', () => {
it('is able to get recordings from a directory', () => {
const recordingsFolderPath = 'test/resources';
const recordingPaths = getRecordingPaths([recordingsFolderPath]);

assert.isTrue(
!!recordingPaths.find((path) => path.includes('replay.json'))
);
assert.isTrue(
!!recordingPaths.find((path) => path.includes('replay-fail.json'))
);
});
});

describe('getJSONFilesFromFolder', () => {
it('is able to return json files from a directory', () => {
const files = getJSONFilesFromFolder(path.join(__dirname, 'resources'));

assert.isTrue(files.every((file) => file.endsWith('.json')));
});
});
});
8 changes: 8 additions & 0 deletions test/resources/replay-fail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"steps": [
{
"type": "navigate",
"url": "https://www.wikipedia.org/"
}
]
}