-
Notifications
You must be signed in to change notification settings - Fork 301
/
Runner.ts
134 lines (112 loc) · 3.56 KB
/
Runner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
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 { PuppeteerRunnerOwningBrowserExtension } from './PuppeteerRunnerExtension.js';
import { RunnerExtension } from './RunnerExtension.js';
import { UserFlow, Step } from './Schema.js';
async function _runStepWithHooks(
extension: RunnerExtension,
step: Step,
flow?: UserFlow
) {
await extension.beforeEachStep?.(step, flow);
await extension.runStep(step, flow);
await extension.afterEachStep?.(step, flow);
}
export class Runner {
#flow?: UserFlow;
#extension: RunnerExtension;
#aborted: boolean = false;
/**
* @internal
*/
constructor(extension: RunnerExtension) {
this.#extension = extension;
}
abort(): void {
this.#aborted = true;
}
set flow(flow: UserFlow) {
this.#flow = flow;
}
async runBeforeAllSteps(flow?: UserFlow): Promise<void> {
await this.#extension.beforeAllSteps?.(flow);
}
async runAfterAllSteps(flow?: UserFlow): Promise<void> {
await this.#extension.afterAllSteps?.(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);
}
/**
* Run all the steps in the flow
* @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?.(flow);
if (this.#aborted) {
return false;
}
for (const step of flow.steps) {
if (this.#aborted) {
await this.#extension.afterAllSteps?.(flow);
return false;
}
await _runStepWithHooks(this.#extension, step, 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
): Promise<Runner>;
export async function createRunner(
flowOrExtension?: UserFlow | RunnerExtension,
maybeExtension?: RunnerExtension
) {
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() {
const { default: puppeteer } = await import('puppeteer');
const browser = await puppeteer.launch();
const page = await browser.newPage();
return new PuppeteerRunnerOwningBrowserExtension(browser, page);
}