Skip to content

Commit

Permalink
fix: allow specifying no flow when creating a runner (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Oct 27, 2022
1 parent 146771a commit d920654
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 27 deletions.
54 changes: 48 additions & 6 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,64 @@ limitations under the License.

### createRunner

**createRunner**(`flow`, `extension?`): `Promise`<[`Runner`](classes/Runner.md)\>
**createRunner**(): `Promise`<[`Runner`](classes/Runner.md)\>

#### Returns

`Promise`<[`Runner`](classes/Runner.md)\>

#### Defined in

[Runner.ts:94](https://github.com/puppeteer/replay/blob/main/src/Runner.ts#L94)

**createRunner**(`flow`): `Promise`<[`Runner`](classes/Runner.md)\>

#### Parameters

| Name | Type |
| :----- | :------------------------------------------ |
| `flow` | [`UserFlow`](interfaces/Schema.UserFlow.md) |

#### Returns

`Promise`<[`Runner`](classes/Runner.md)\>

#### Defined in

[Runner.ts:95](https://github.com/puppeteer/replay/blob/main/src/Runner.ts#L95)

**createRunner**(`extension`): `Promise`<[`Runner`](classes/Runner.md)\>

#### Parameters

| Name | Type |
| :---------- | :---------------------------------------------- |
| `extension` | [`RunnerExtension`](classes/RunnerExtension.md) |

#### Returns

`Promise`<[`Runner`](classes/Runner.md)\>

#### Defined in

[Runner.ts:96](https://github.com/puppeteer/replay/blob/main/src/Runner.ts#L96)

**createRunner**(`flow`, `extension`): `Promise`<[`Runner`](classes/Runner.md)\>

#### Parameters

| Name | Type |
| :----------- | :---------------------------------------------- |
| `flow` | [`UserFlow`](interfaces/Schema.UserFlow.md) |
| `extension?` | [`RunnerExtension`](classes/RunnerExtension.md) |
| Name | Type |
| :---------- | :---------------------------------------------- |
| `flow` | [`UserFlow`](interfaces/Schema.UserFlow.md) |
| `extension` | [`RunnerExtension`](classes/RunnerExtension.md) |

#### Returns

`Promise`<[`Runner`](classes/Runner.md)\>

#### Defined in

[Runner.ts:79](https://github.com/puppeteer/replay/blob/main/src/Runner.ts#L79)
[Runner.ts:97](https://github.com/puppeteer/replay/blob/main/src/Runner.ts#L97)

---

Expand Down
33 changes: 30 additions & 3 deletions docs/api/classes/Runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,36 @@

## Table of contents

### Accessors

- [flow](Runner.md#flow)

### Methods

- [abort](Runner.md#abort)
- [run](Runner.md#run)
- [runStep](Runner.md#runstep)

## Accessors

### flow

`set` **flow**(`flow`): `void`

#### Parameters

| Name | Type |
| :----- | :--------------------------------------------- |
| `flow` | [`UserFlow`](../interfaces/Schema.UserFlow.md) |

#### Returns

`void`

#### Defined in

[Runner.ts:47](https://github.com/puppeteer/replay/blob/main/src/Runner.ts#L47)

## Methods

### abort
Expand All @@ -22,7 +46,7 @@

#### Defined in

[Runner.ts:44](https://github.com/puppeteer/replay/blob/main/src/Runner.ts#L44)
[Runner.ts:43](https://github.com/puppeteer/replay/blob/main/src/Runner.ts#L43)

---

Expand All @@ -40,14 +64,17 @@ whether all the steps are run or the execution is aborted

#### Defined in

[Runner.ts:56](https://github.com/puppeteer/replay/blob/main/src/Runner.ts#L56)
[Runner.ts:63](https://github.com/puppeteer/replay/blob/main/src/Runner.ts#L63)

---

### runStep

**runStep**(`step`): `Promise`<`void`\>

Runs the provided `step` with `beforeEachStep` and `afterEachStep` hooks.
Parameters from the `flow` apply if the `flow` is set.

#### Parameters

| Name | Type |
Expand All @@ -60,4 +87,4 @@ whether all the steps are run or the execution is aborted

#### Defined in

[Runner.ts:48](https://github.com/puppeteer/replay/blob/main/src/Runner.ts#L48)
[Runner.ts:55](https://github.com/puppeteer/replay/blob/main/src/Runner.ts#L55)
54 changes: 43 additions & 11 deletions src/Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,29 @@ async function _runStepWithHooks(
}

export class Runner {
#flow: UserFlow;
#flow?: UserFlow;
#extension: RunnerExtension;
#aborted: boolean = false;

/**
* @internal
*/
constructor(flow: UserFlow, extension: RunnerExtension) {
this.#flow = flow;
constructor(extension: RunnerExtension) {
this.#extension = extension;
}

abort(): void {
this.#aborted = true;
}

set flow(flow: UserFlow) {
this.#flow = flow;
}

/**
* Runs the provided `step` with `beforeEachStep` and `afterEachStep` hooks.
* Parameters from the `flow` apply if the `flow` is set.
*/
async runStep(step: Step): Promise<void> {
await _runStepWithHooks(this.#extension, step);
}
Expand All @@ -54,36 +61,61 @@ export class Runner {
* @returns whether all the steps are run or the execution is aborted
*/
async run(): Promise<boolean> {
if (!this.#flow) {
throw new Error(
'Set the flow on the runner instance before calling `run`.'
);
}

const flow = this.#flow;

this.#aborted = false;

await this.#extension.beforeAllSteps?.(this.#flow);
await this.#extension.beforeAllSteps?.(flow);

if (this.#aborted) {
return false;
}

for (const step of this.#flow.steps) {
for (const step of flow.steps) {
if (this.#aborted) {
await this.#extension.afterAllSteps?.(this.#flow);
await this.#extension.afterAllSteps?.(flow);
return false;
}
await _runStepWithHooks(this.#extension, step, this.#flow);
await _runStepWithHooks(this.#extension, step, flow);
}

await this.#extension.afterAllSteps?.(this.#flow);
await this.#extension.afterAllSteps?.(flow);

return true;
}
}

export async function createRunner(): Promise<Runner>;
export async function createRunner(flow: UserFlow): Promise<Runner>;
export async function createRunner(extension: RunnerExtension): Promise<Runner>;
export async function createRunner(
flow: UserFlow,
extension?: RunnerExtension
extension: RunnerExtension
): Promise<Runner>;
export async function createRunner(
flowOrExtension?: UserFlow | RunnerExtension,
maybeExtension?: RunnerExtension
) {
return new Runner(
flow,
const extension =
flowOrExtension instanceof RunnerExtension
? flowOrExtension
: maybeExtension;
const flow = !(flowOrExtension instanceof RunnerExtension)
? flowOrExtension
: undefined;
const runner = new Runner(
extension ?? (await createPuppeteerRunnerOwningBrowserExtension())
);
if (flow) {
runner.flow = flow;
}
return runner;
}

async function createPuppeteerRunnerOwningBrowserExtension() {
Expand Down
4 changes: 2 additions & 2 deletions src/lighthouse/LighthouseRunnerExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class LighthouseRunnerExtension extends PuppeteerRunnerExtension {
});
}

override async beforeEachStep(step: Step, flow: UserFlow) {
override async beforeEachStep(step: Step, flow?: UserFlow) {
await super.beforeEachStep?.(step, flow);
if (step.type === StepType.SetViewport) return;

Expand All @@ -69,7 +69,7 @@ export class LighthouseRunnerExtension extends PuppeteerRunnerExtension {
}
}

override async afterEachStep(step: Step, flow: UserFlow) {
override async afterEachStep(step: Step, flow?: UserFlow) {
if (this.#isNavigationRunning) {
await this.#lhFlow.endNavigation();
this.#isNavigationRunning = false;
Expand Down
5 changes: 0 additions & 5 deletions test/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,11 +939,6 @@ describe('Runner', () => {

it('should replay individual steps', async () => {
const runner = await createRunner(
{
title: 'Test Recording',
timeout: 3000,
steps: [],
},
new PuppeteerRunnerExtension(browser, page)
);
await runner.runStep({
Expand Down

0 comments on commit d920654

Please sign in to comment.