This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathdriverProviderAttachSession.js
More file actions
140 lines (130 loc) Β· 3.62 KB
/
driverProviderAttachSession.js
File metadata and controls
140 lines (130 loc) Β· 3.62 KB
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
#!/usr/bin/env node
'use strict';
const http = require('http');
const child_process = require('child_process');
// Delete session method to be used at the end of the test as well as
// when the tests fail.
const deleteSession = (sessionId, err) => {
return new Promise(resolve => {
const deleteOptions = {
hostname: 'localhost',
port: 4444,
path: '/wd/hub/session/' + sessionId,
method: 'DELETE'
};
const req = http.request(deleteOptions, res => {
res.on('end', () => {
if (err) {
throw err;
}
resolve();
});
});
req.end();
});
};
const run = async () => {
// 1. Create a new selenium session.
const sessionId = await new Promise(resolve => {
const postData = JSON.stringify(
{'desiredCapabilities': {'browserName': 'chrome'}});
const createOptions = {
hostname: '127.0.0.1',
port: 4444,
path: '/wd/hub/session',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
let body = '';
const req = http.request(createOptions, (res) => {
res.on('data', (data) => {
body = JSON.parse(data.toString());
});
res.on('end', () => {
resolve(body.sessionId);
});
});
req.write(postData);
req.end();
});
await new Promise(resolve => {
// 2. After getting the session id, verify that the selenium session exists.
// If the session exists, run the protractor test.
const checkOptions = {
hostname: '127.0.0.1',
port: 4444,
path: '/wd/hub/sessions',
method: 'GET'
};
let values = [];
const req = http.request(checkOptions, (res) => {
res.on('data', (chunk) => {
values = JSON.parse(chunk.toString())['value'];
});
res.on('end', () => {
let found = false;
for (let value of values) {
if (value['id'] === sessionId) {
found = true;
}
}
if (found) {
resolve();
} else {
throw new Error('The selenium session was not created.');
}
});
res.on('error', (err) => {
console.log(err);
process.exit(1);
});
});
req.end();
});
// 3. Run Protractor and attach to the session.
const runProtractor = child_process.spawnSync('node',
['bin/protractor', 'spec/driverProviderAttachSessionConf.js',
'--seleniumSessionId=' + sessionId]);
console.log(runProtractor.stdout.toString());
if (runProtractor.status !== 0) {
const e = new Error('Protractor did not run properly.');
await deleteSession(sessionId, e);
process.exit(1);
}
// 4. After the protractor test completes, check to see that the session still
// exists. If we can find the session, delete it.
await new Promise(resolve => {
const checkOptions = {
hostname: '127.0.0.1',
port: 4444,
path: '/wd/hub/session/' + sessionId,
method: 'GET'
};
const req = http.request(checkOptions, (res) => {
let state = '';
res.on('data', (chunk) => {
state = JSON.parse(chunk.toString()).state;
});
res.on('end', () => {
if (state === 'success') {
resolve();
}
else {
const e = new Error('The selenium session should still exist.');
deleteSession(sessionId, e);
}
});
res.on('error', (err) => {
console.log(err);
process.exit(1);
});
});
req.end();
});
// 5. Delete the selenium session.
await deleteSession(sessionId);
}
run().then();