/
client.ts
178 lines (154 loc) · 4.27 KB
/
client.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
import {
CliRunner,
CliRunnerInstance,
client as createClient,
Client,
Api,
NightwatchError
} from 'nightwatch';
import fs from 'fs';
import { log } from './logger';
import { createFailureScreenshot } from './screenshots';
import reporter from './reporter';
interface IOptions {
env?: string;
configFile?: string;
silent?: boolean;
}
let runner: CliRunnerInstance | null;
let runnerOptions: IOptions | null;
let client: Client | null;
let screenshots: string[] = [];
export function deleteRunner() {
runner = null;
runnerOptions = null;
client = null;
}
function getDefaultEnvironment() {
return 'default';
}
function getDefaultConfigFile() {
const jsonConfigFile = './nightwatch.json';
const jsConfigFie = './nightwatch.conf.js';
if (fs.existsSync(jsonConfigFile)) {
return jsonConfigFile;
}
if (fs.existsSync(jsConfigFie)) {
return jsConfigFie;
}
throw new Error(
[
'No configuration file was found for Nightwatch in the current process folder.',
'(nightwatch.json or nightwatch.conf.js).',
'For custom location please provide "configFile" option for "startWebDriver" or',
'"createSession" functions.'
].join(' ')
);
}
function createRunner(options: IOptions) {
if (!runner) {
runnerOptions = {
env: (options && options.env) || getDefaultEnvironment(),
configFile: (options && options.configFile) || getDefaultConfigFile(),
silent: (options && options.silent) || false
};
runner = CliRunner({ env: runnerOptions.env, config: runnerOptions.configFile });
runner.isWebDriverManaged = function() {
if (this.baseSettings.selenium) {
this.baseSettings.selenium.start_process = true;
}
return true;
};
runner.setup();
}
return runner;
}
export async function startWebDriver(options: IOptions) {
deleteRunner();
const runner = createRunner(options);
const { port } = runner.test_settings.webdriver;
await runner.startWebDriver();
log(`WebDriver started on port ${port} for ${runner.testEnv} environment`);
}
export async function stopWebDriver() {
if (!runner) {
return;
}
const { port } = runner.test_settings.webdriver;
await runner.stopWebDriver();
log(`WebDriver stopped on port ${port} for ${runner.testEnv} environment`);
}
export async function createSession(options: IOptions): Promise<Api> {
if (options) {
deleteRunner();
}
const runner = createRunner(options);
const settings = runner.test_settings;
client =
runnerOptions && runnerOptions.silent
? createClient(settings)
: createClient(settings, new reporter());
log(`Creating session for ${runner.testEnv} environment on port ${settings.webdriver.port}`);
await client.startSession();
log(`Session created for ${runner.testEnv} environment`);
return client.api;
}
function resetQueue() {
if (client && client.queue) {
client.queue
.reset()
.removeAllListeners()
.empty();
}
}
export async function closeSession() {
await new Promise(resolve => {
if (!client) {
return;
}
client.transportActions.sessionAction('DELETE', client.sessionId, resolve);
});
/* istanbul ignore next */
if (runner) {
log(`Session closed for ${runner.testEnv} environment`);
}
}
async function handleQueueResult(err: NightwatchError, resolve: Function, reject: Function) {
if (!err) {
resolve();
return;
}
if (client && client.api.screenshotsPath) {
log('Creating screenshot because of failure');
const fileName = await createFailureScreenshot(client);
screenshots.push(fileName);
}
err.stack = [err.message, err.stack].join('\n');
reject(err);
}
export async function runQueue() {
try {
await new Promise((resolve, reject) => {
if (!client || !client.queue) {
throw new Error(
`Nightwatch client is not ready.
Looks like function "createSession" did not succeed or was not called yet.`
);
}
client.queue.once(
'queue:finished',
async (err: NightwatchError) => await handleQueueResult(err, resolve, reject)
);
client.queue.run();
});
} catch (err) {
throw err;
} finally {
resetQueue();
}
}
export function getNewScreenshots() {
const result = screenshots;
screenshots = [];
return result;
}