forked from DevExpress/testcafe
/
testcafe-configuration.ts
298 lines (228 loc) · 11 KB
/
testcafe-configuration.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import Configuration from './configuration-base';
import { castArray, flatten } from 'lodash';
import { getGrepOptions, getSSLOptions } from '../utils/get-options';
import OPTION_NAMES from './option-names';
import getFilterFn from '../utils/get-filter-fn';
import prepareReporters from '../utils/prepare-reporters';
import { getConcatenatedValuesString, getPluralSuffix } from '../utils/string';
import renderTemplate from '../utils/render-template';
import WARNING_MESSAGES from '../notifications/warning-message';
import resolvePathRelativelyCwd from '../utils/resolve-path-relatively-cwd';
import {
DEFAULT_APP_INIT_DELAY,
DEFAULT_CONCURRENCY_VALUE,
DEFAULT_DEVELOPMENT_MODE,
DEFAULT_DISABLE_HTTP2,
DEFAULT_FILTER_FN,
DEFAULT_PROXYLESS,
DEFAULT_RETRY_TEST_PAGES,
DEFAULT_SCREENSHOT_THUMBNAILS,
DEFAULT_SOURCE_DIRECTORIES,
DEFAULT_SPEED_VALUE,
DEFAULT_TIMEOUT,
getDefaultCompilerOptions,
} from './default-values';
import OptionSource from './option-source';
import {
Dictionary,
FilterOption,
ReporterOption,
TypeScriptCompilerOptions,
} from './interfaces';
import CustomizableCompilers from './customizable-compilers';
import { DEPRECATED, getDeprecationMessage } from '../notifications/deprecated';
import WarningLog from '../notifications/warning-log';
import browserProviderPool from '../browser/provider/pool';
import BrowserConnection, { BrowserInfo } from '../browser/connection';
import { CONFIGURATION_EXTENSIONS } from './formats';
import { GeneralError } from '../errors/runtime';
import { RUNTIME_ERRORS } from '../errors/types';
const BASE_CONFIGURATION_FILENAME = '.testcaferc';
const CONFIGURATION_FILENAMES = CONFIGURATION_EXTENSIONS.map(ext => `${BASE_CONFIGURATION_FILENAME}${ext}`);
const DEFAULT_SCREENSHOTS_DIRECTORY = 'screenshots';
const OPTION_FLAG_NAMES = [
OPTION_NAMES.skipJsErrors,
OPTION_NAMES.debugMode,
OPTION_NAMES.debugOnFail,
OPTION_NAMES.skipUncaughtErrors,
OPTION_NAMES.stopOnFirstFail,
OPTION_NAMES.takeScreenshotsOnFails,
OPTION_NAMES.disablePageCaching,
OPTION_NAMES.disablePageReloads,
OPTION_NAMES.disableScreenshots,
OPTION_NAMES.disableMultipleWindows,
];
const OPTION_INIT_FLAG_NAMES = [
OPTION_NAMES.developmentMode,
OPTION_NAMES.retryTestPages,
OPTION_NAMES.cache,
OPTION_NAMES.disableHttp2,
OPTION_NAMES.proxyless,
];
interface TestCafeAdditionalStartOptions {
retryTestPages: boolean;
ssl: string;
developmentMode: boolean;
cache: boolean;
disableHttp2: boolean;
}
interface TestCafeStartOptions {
hostname?: string;
port1?: number;
port2?: number;
options: TestCafeAdditionalStartOptions;
}
type BrowserInfoSource = BrowserInfo | BrowserConnection;
export default class TestCafeConfiguration extends Configuration {
protected readonly _isExplicitConfig: boolean;
public constructor (configFile = '') {
super(configFile || CONFIGURATION_FILENAMES);
this._isExplicitConfig = !!configFile;
}
public async init (options?: object): Promise<void> {
await super.init();
const opts = await this._load();
if (opts) {
this._options = Configuration._fromObj(opts);
await this._normalizeOptionsAfterLoad();
}
await this.asyncMergeOptions(options);
}
public async asyncMergeOptions (options?: object): Promise<void> {
options = options || {};
super.mergeOptions(options);
if (this._options.browsers)
this._options.browsers.value = await this._getBrowserInfo();
}
public prepare (): void {
this._prepareFlags();
this._setDefaultValues();
this._prepareCompilerOptions();
}
public notifyAboutOverriddenOptions (warningLog?: WarningLog): void {
if (!this._overriddenOptions.length)
return;
const optionsStr = getConcatenatedValuesString(this._overriddenOptions);
const optionsSuffix = getPluralSuffix(this._overriddenOptions);
const renderedMessage = renderTemplate(WARNING_MESSAGES.configOptionsWereOverridden, optionsStr, optionsSuffix);
Configuration._showConsoleWarning(renderedMessage);
if (warningLog)
warningLog.addWarning(renderedMessage);
this._overriddenOptions = [];
}
public notifyAboutDeprecatedOptions (warningLog: WarningLog): void {
const deprecatedOptions = this.getOptions((name, option) => name in DEPRECATED && option.value !== void 0);
for (const optionName in deprecatedOptions)
warningLog.addWarning(getDeprecationMessage(DEPRECATED[optionName]));
}
public get startOptions (): TestCafeStartOptions {
const result: TestCafeStartOptions = {
hostname: this.getOption(OPTION_NAMES.hostname) as string,
port1: this.getOption(OPTION_NAMES.port1) as number,
port2: this.getOption(OPTION_NAMES.port2) as number,
options: {
ssl: this.getOption(OPTION_NAMES.ssl) as string,
developmentMode: this.getOption(OPTION_NAMES.developmentMode) as boolean,
retryTestPages: this.getOption(OPTION_NAMES.retryTestPages) as boolean,
cache: this.getOption(OPTION_NAMES.cache) as boolean,
disableHttp2: this.getOption(OPTION_NAMES.disableHttp2) as boolean,
},
};
return result;
}
private _prepareFlag (name: string, source = OptionSource.Configuration): void {
const option = this._ensureOption(name, void 0, source);
option.value = !!option.value;
}
private _prepareFlags (): void {
OPTION_FLAG_NAMES.forEach(name => this._prepareFlag(name));
}
private _prepareInitFlags (): void {
OPTION_INIT_FLAG_NAMES.forEach(name => this._prepareFlag(name, OptionSource.Default));
}
private async _normalizeOptionsAfterLoad (): Promise<void> {
await this._prepareSslOptions();
this._prepareInitFlags();
this._prepareFilterFn();
this._ensureArrayOption(OPTION_NAMES.src);
this._ensureArrayOption(OPTION_NAMES.browsers);
this._ensureArrayOption(OPTION_NAMES.clientScripts);
this._prepareReporters();
}
private _prepareFilterFn (): void {
const filterOption = this._ensureOption(OPTION_NAMES.filter, DEFAULT_FILTER_FN, OptionSource.Default);
if (!filterOption.value)
return;
const filterOptionValue = filterOption.value as FilterOption;
if (filterOptionValue.testGrep)
filterOptionValue.testGrep = getGrepOptions(OPTION_NAMES.filterTestGrep, filterOptionValue.testGrep as string);
if (filterOptionValue.fixtureGrep)
filterOptionValue.fixtureGrep = getGrepOptions(OPTION_NAMES.filterFixtureGrep, filterOptionValue.fixtureGrep as string);
filterOption.value = getFilterFn(filterOption.value) as Function;
filterOption.source = OptionSource.Configuration;
}
private _ensureScreenshotOptions (): void {
const path = resolvePathRelativelyCwd(DEFAULT_SCREENSHOTS_DIRECTORY);
const screenshots = this._ensureOption(OPTION_NAMES.screenshots, {}, OptionSource.Configuration).value as Dictionary<string|boolean>;
if (!screenshots.path)
screenshots.path = path;
if (screenshots.thumbnails === void 0)
screenshots.thumbnails = DEFAULT_SCREENSHOT_THUMBNAILS;
}
private _prepareReporters (): void {
const reporterOption = this._options[OPTION_NAMES.reporter];
if (!reporterOption)
return;
const optionValue = castArray(reporterOption.value as ReporterOption);
reporterOption.value = prepareReporters(optionValue);
}
private async _prepareSslOptions (): Promise<void> {
const sslOptions = this._options[OPTION_NAMES.ssl];
if (!sslOptions)
return;
sslOptions.value = await getSSLOptions(sslOptions.value as string) as Dictionary<string | boolean | number>;
}
private _setDefaultValues (): void {
this._ensureOptionWithValue(OPTION_NAMES.selectorTimeout, DEFAULT_TIMEOUT.selector, OptionSource.Configuration);
this._ensureOptionWithValue(OPTION_NAMES.assertionTimeout, DEFAULT_TIMEOUT.assertion, OptionSource.Configuration);
this._ensureOptionWithValue(OPTION_NAMES.pageLoadTimeout, DEFAULT_TIMEOUT.pageLoad, OptionSource.Configuration);
this._ensureOptionWithValue(OPTION_NAMES.speed, DEFAULT_SPEED_VALUE, OptionSource.Configuration);
this._ensureOptionWithValue(OPTION_NAMES.appInitDelay, DEFAULT_APP_INIT_DELAY, OptionSource.Configuration);
this._ensureOptionWithValue(OPTION_NAMES.concurrency, DEFAULT_CONCURRENCY_VALUE, OptionSource.Configuration);
this._ensureOptionWithValue(OPTION_NAMES.src, DEFAULT_SOURCE_DIRECTORIES, OptionSource.Configuration);
this._ensureOptionWithValue(OPTION_NAMES.developmentMode, DEFAULT_DEVELOPMENT_MODE, OptionSource.Configuration);
this._ensureOptionWithValue(OPTION_NAMES.retryTestPages, DEFAULT_RETRY_TEST_PAGES, OptionSource.Configuration);
this._ensureOptionWithValue(OPTION_NAMES.disableHttp2, DEFAULT_DISABLE_HTTP2, OptionSource.Configuration);
this._ensureOptionWithValue(OPTION_NAMES.proxyless, DEFAULT_PROXYLESS, OptionSource.Configuration);
this._ensureScreenshotOptions();
}
private _prepareCompilerOptions (): void {
const compilerOptions = this._ensureOption(OPTION_NAMES.compilerOptions, getDefaultCompilerOptions(), OptionSource.Configuration);
compilerOptions.value = compilerOptions.value || getDefaultCompilerOptions();
const tsConfigPath = this.getOption(OPTION_NAMES.tsConfigPath);
if (tsConfigPath) {
const compilerOptionValue = compilerOptions.value as CompilerOptions;
let typeScriptCompilerOptions = compilerOptionValue[CustomizableCompilers.typescript] as TypeScriptCompilerOptions;
typeScriptCompilerOptions = Object.assign({
configPath: tsConfigPath,
}, typeScriptCompilerOptions);
(compilerOptions.value as CompilerOptions)[CustomizableCompilers.typescript] = typeScriptCompilerOptions;
}
}
private async _getBrowserInfo (): Promise<BrowserInfoSource[]> {
if (!this._options.browsers.value)
return [];
const browsers = Array.isArray(this._options.browsers.value) ? [...this._options.browsers.value] : [this._options.browsers.value];
const browserInfo = await Promise.all(browsers.map(browser => browserProviderPool.getBrowserInfo(browser)));
return flatten(browserInfo);
}
protected async _isConfigurationFileExists (filePath = this.filePath): Promise<boolean> {
const fileExists = await super._isConfigurationFileExists(filePath);
if (!fileExists && this._isExplicitConfig)
throw new GeneralError(RUNTIME_ERRORS.cannotFindTestcafeConfigurationFile, filePath);
return fileExists;
}
public static get FILENAMES (): string[] {
return CONFIGURATION_FILENAMES;
}
}