forked from angular/protractor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattachSession.js
More file actions
105 lines (97 loc) · 2.71 KB
/
attachSession.js
File metadata and controls
105 lines (97 loc) · 2.71 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
#!/usr/bin/env node
'use strict';
var http = require('http'),
spawn = require('child_process').spawnSync;
var sessionId = '';
// 1. Create a new selenium session.
var postData = JSON.stringify(
{'desiredCapabilities': {'browserName': 'firefox'}});
var createOptions = {
hostname: 'localhost',
port: 4444,
path: '/wd/hub/session',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
var req = http.request(createOptions, function(res) {
res.on('data', setBody);
res.on('end', checkSession);
});
req.write(postData);
req.end();
// 2. After making the request to create a selenium session, read the selenium
// session id.
var setBody = function(chunk) {
var body = chunk.toString();
sessionId = JSON.parse(body).sessionId;
};
// 3. After getting the session id, verify that the selenium session exists.
// If the session exists, run the protractor test.
var checkSession = function() {
var checkOptions = {
hostname: 'localhost',
port: 4444,
path: '/wd/hub/session/' + sessionId,
method: 'GET'
};
var state = '';
var req = http.request(checkOptions, function(res) {
res.on('data', function(chunk) {
state = JSON.parse(chunk.toString()).state;
});
res.on('end', function() {
if (state === 'success') {
var runProtractor = spawn('node',
['bin/protractor', 'spec/attachSession.js', '--seleniumSessionId=' + sessionId]);
console.log(runProtractor.stdout.toString());
if (runProtractor.status !== 0) {
throw new Error('Protractor did not run properly.');
}
}
else {
throw new Error('The selenium session was not created.');
}
checkStoppedSession();
});
});
req.end();
};
// 4. After the protractor test completes, check to see that the session still
// exists. If we can find the session, delete it.
var checkStoppedSession = function() {
var checkOptions = {
hostname: 'localhost',
port: 4444,
path: '/wd/hub/session/' + sessionId,
method: 'GET'
};
var state = '';
var req = http.request(checkOptions, function(res) {
res.on('data', function(chunk) {
state = JSON.parse(chunk.toString()).state;
});
res.on('end', function() {
if (state === 'success') {
deleteSession();
}
else {
throw new Error('The selenium session should still exist.');
}
});
});
req.end();
};
// 5. Delete the selenium session.
var deleteSession = function() {
var deleteOptions = {
hostname: 'localhost',
port: 4444,
path: '/wd/hub/session/' + sessionId,
method: 'DELETE'
};
var req = http.request(deleteOptions);
req.end();
};