/
legacy-lifecycle-hooks.class.ts
229 lines (213 loc) · 6.66 KB
/
legacy-lifecycle-hooks.class.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { Builder, ThenableWebDriver } from "selenium-webdriver";
import { ifPresent, Maybe, throwIfAbsent } from "@sakuli/commons";
import {
createKeyboardApi,
createLoggerObject,
createMouseApi,
createTestCaseClass,
createThenableApplicationClass,
createThenableEnvironmentClass,
createThenableRegionClass,
Key,
MouseButton,
} from "./common";
import { sahiApi } from "./sahi/api";
import {
Project,
TestExecutionContext,
TestExecutionLifecycleHooks,
TestFile,
} from "@sakuli/core";
import { basename, dirname, join, parse, sep } from "path";
import { LegacyProjectProperties } from "../loader/legacy-project-properties.class";
import { promises as fs } from "fs";
import { LegacyApi } from "./legacy-api.interface";
import { createDriverFromProject } from "./selenium-config/create-driver-from-project.function";
import { TestStepCache } from "./common/test-case/steps-cache/test-step-cache.class";
import { NoopSahiApi } from "./noop-sahi-api.const";
import { SahiApi } from "./sahi/sahi-api.interface";
import { getActiveKeys } from "./common/button-registry";
import { releaseKeys } from "./common/release-keys.function";
import {
createDriverLoggingAdapter,
DriverLoggingAdapter,
} from "./createDriverLoggingAdapter";
import Signals = NodeJS.Signals;
export class LegacyLifecycleHooks implements TestExecutionLifecycleHooks {
driver: Maybe<ThenableWebDriver> = null;
/**
* Path to the current Testsuite. Might be used in `requestContext`
*/
currentTest: Maybe<string> = null;
uiOnly = false;
reuseBrowser = true;
properties: Maybe<LegacyProjectProperties> = null;
driverLoggingAdapter: Maybe<DriverLoggingAdapter>;
constructor(readonly builder: Builder) {}
async onProject(
project: Project,
testExecutionContext: TestExecutionContext
) {
this.properties = project.objectFactory(LegacyProjectProperties);
this.uiOnly = this.properties.isUiOnly();
this.reuseBrowser = this.properties.isReuseBrowser();
this.driverLoggingAdapter = createDriverLoggingAdapter(
testExecutionContext.logger
);
await this.driverLoggingAdapter.start();
if (!this.uiOnly && this.reuseBrowser) {
await this.setupDriver(project, testExecutionContext);
}
}
async beforeExecution(
project: Project,
testExecutionContext: TestExecutionContext
) {
const id = this.properties!.testsuiteId
? this.properties!.testsuiteId
: project.rootDir.split(sep).pop();
const warningTime = this.properties!.testsuiteWarningTime || 0;
const criticalTime = this.properties!.testsuiteCriticalTime || 0;
testExecutionContext.startTestSuite({ id, warningTime, criticalTime });
}
async afterExecution(
project: Project,
testExecutionContext: TestExecutionContext
) {
testExecutionContext.endTestSuite();
if (this.reuseBrowser) {
await this.quitDriver(testExecutionContext);
}
}
private currentFile: string = "";
async beforeRunFile(
file: TestFile,
project: Project,
testExecutionContext: TestExecutionContext
) {
this.currentFile = file.path;
this.currentTest = dirname(
await fs.realpath(join(project.rootDir, file.path))
);
if (!this.reuseBrowser) {
await this.setupDriver(project, testExecutionContext);
}
}
async afterRunFile(
file: TestFile,
project: Project,
testExecutionContext: TestExecutionContext
) {
const { name } = parse(file.path);
ifPresent(testExecutionContext.getCurrentTestCase(), (ctc) => {
if (!ctc.id) {
testExecutionContext.updateCurrentTestCase({ id: name });
}
});
if (!this.reuseBrowser) {
await this.quitDriver(testExecutionContext);
}
}
async requestContext(
testExecutionContext: TestExecutionContext,
project: Project
): Promise<LegacyApi> {
const sahi: SahiApi = this.driver
? sahiApi(this.driver, testExecutionContext, this.properties!)
: NoopSahiApi;
const currentTestFolder = throwIfAbsent(
this.currentTest,
Error(
"Could not initialize LegacyDslContext because no test folder was found / provided"
)
);
const stepCacheFileName = basename(this.currentFile);
const stepsCache = new TestStepCache(
join(currentTestFolder, `.${stepCacheFileName}.steps.cache`)
);
return Promise.resolve({
driver: this.driver!,
context: testExecutionContext,
TestCase: createTestCaseClass(
testExecutionContext,
project,
this.currentTest,
stepsCache
),
Application: createThenableApplicationClass(
testExecutionContext,
project
),
Key,
MouseButton,
Environment: createThenableEnvironmentClass(
testExecutionContext,
project
),
Region: createThenableRegionClass(testExecutionContext, project),
Logger: createLoggerObject(testExecutionContext),
$includeFolder: "",
...sahi,
});
}
private async setupDriver(
project: Project,
testExecutionContext: TestExecutionContext
) {
await this.createDriver(project, testExecutionContext);
if (this.driver) {
this.driverLoggingAdapter?.setDriver(this.driver);
}
}
private async createDriver(
project: Project,
testExecutionContext: TestExecutionContext
) {
this.driver = createDriverFromProject(
project,
testExecutionContext,
this.builder
);
await this.driver.manage().window().maximize();
testExecutionContext.logger.debug("Created webdriver");
}
private async quitDriver(testExecutionContext: TestExecutionContext) {
this.driverLoggingAdapter?.stop();
await ifPresent(
this.driver,
async (driver) => {
try {
await driver.quit();
testExecutionContext.logger.debug("Closed webdriver");
} catch (e) {
testExecutionContext.logger.warn(
`Webdriver doesn't quit reliably`,
e
);
}
},
() => Promise.resolve()
);
}
async onUnhandledError(
error: any,
project: Project,
_: TestExecutionContext
) {
this.driverLoggingAdapter?.stop();
await this.releaseKeys(project);
}
async onSignal(signal: Signals, project: Project, _: TestExecutionContext) {
if (signal === "SIGINT" || signal === "SIGTERM") {
this.driverLoggingAdapter?.stop();
await this.releaseKeys(project);
}
}
private async releaseKeys(project: Project) {
const properties = project.objectFactory(LegacyProjectProperties);
const keyboardApi = createKeyboardApi(properties);
const mouseApi = createMouseApi(properties);
const buttonRegistry = getActiveKeys();
await releaseKeys(buttonRegistry, mouseApi, keyboardApi);
}
}