|
2 | 2 |
|
3 | 3 | 'use strict';
|
4 | 4 |
|
5 |
| -var http = require('http'), |
6 |
| - spawn = require('child_process').spawnSync; |
| 5 | +const http = require('http'); |
| 6 | +const child_process = require('child_process'); |
7 | 7 |
|
8 |
| -var sessionId = ''; |
9 |
| - |
10 |
| -// 1. Create a new selenium session. |
11 |
| -var postData = JSON.stringify( |
12 |
| - {'desiredCapabilities': {'browserName': 'firefox'}}); |
13 |
| -var createOptions = { |
14 |
| - hostname: 'localhost', |
15 |
| - port: 4444, |
16 |
| - path: '/wd/hub/session', |
17 |
| - method: 'POST', |
18 |
| - headers: { |
19 |
| - 'Content-Type': 'application/x-www-form-urlencoded', |
20 |
| - 'Content-Length': Buffer.byteLength(postData) |
21 |
| - } |
22 |
| -}; |
23 |
| -var req = http.request(createOptions, function(res) { |
24 |
| - res.on('data', setBody); |
25 |
| - res.on('end', checkSession); |
26 |
| -}); |
27 |
| -req.write(postData); |
28 |
| -req.end(); |
29 |
| - |
30 |
| -// 2. After making the request to create a selenium session, read the selenium |
31 |
| -// session id. |
32 |
| -var setBody = function(chunk) { |
33 |
| - var body = chunk.toString(); |
34 |
| - sessionId = JSON.parse(body).sessionId; |
| 8 | +// Delete session method to be used at the end of the test as well as |
| 9 | +// when the tests fail. |
| 10 | +const deleteSession = (sessionId, err) => { |
| 11 | + return new Promise(resolve => { |
| 12 | + const deleteOptions = { |
| 13 | + hostname: 'localhost', |
| 14 | + port: 4444, |
| 15 | + path: '/wd/hub/session/' + sessionId, |
| 16 | + method: 'DELETE' |
| 17 | + }; |
| 18 | + const req = http.request(deleteOptions, res => { |
| 19 | + res.on('end', () => { |
| 20 | + if (err) { |
| 21 | + throw err; |
| 22 | + } |
| 23 | + resolve(); |
| 24 | + }); |
| 25 | + }); |
| 26 | + req.end(); |
| 27 | + }); |
35 | 28 | };
|
36 | 29 |
|
37 |
| -// 3. After getting the session id, verify that the selenium session exists. |
38 |
| -// If the session exists, run the protractor test. |
39 |
| -var checkSession = function() { |
40 |
| - var checkOptions = { |
41 |
| - hostname: 'localhost', |
42 |
| - port: 4444, |
43 |
| - path: '/wd/hub/session/' + sessionId, |
44 |
| - method: 'GET' |
45 |
| - }; |
46 |
| - var state = ''; |
47 |
| - var req = http.request(checkOptions, function(res) { |
48 |
| - res.on('data', function(chunk) { |
49 |
| - state = JSON.parse(chunk.toString()).state; |
50 |
| - }); |
51 |
| - res.on('end', function() { |
52 |
| - if (state === 'success') { |
53 |
| - var runProtractor = spawn('node', |
54 |
| - ['bin/protractor', 'spec/driverProviderAttachSessionConf.js', |
55 |
| - '--seleniumSessionId=' + sessionId]); |
56 |
| - console.log(runProtractor.stdout.toString()); |
57 |
| - if (runProtractor.status !== 0) { |
58 |
| - throw new Error('Protractor did not run properly.'); |
59 |
| - } |
| 30 | +const run = async () => { |
| 31 | + // 1. Create a new selenium session. |
| 32 | + const sessionId = await new Promise(resolve => { |
| 33 | + const postData = JSON.stringify( |
| 34 | + {'desiredCapabilities': {'browserName': 'chrome'}}); |
| 35 | + const createOptions = { |
| 36 | + hostname: '127.0.0.1', |
| 37 | + port: 4444, |
| 38 | + path: '/wd/hub/session', |
| 39 | + method: 'POST', |
| 40 | + headers: { |
| 41 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 42 | + 'Content-Length': Buffer.byteLength(postData) |
60 | 43 | }
|
61 |
| - else { |
62 |
| - throw new Error('The selenium session was not created.'); |
63 |
| - } |
64 |
| - checkStoppedSession(); |
| 44 | + }; |
| 45 | + let body = ''; |
| 46 | + const req = http.request(createOptions, (res) => { |
| 47 | + res.on('data', (data) => { |
| 48 | + body = JSON.parse(data.toString()); |
| 49 | + }); |
| 50 | + res.on('end', () => { |
| 51 | + resolve(body.sessionId); |
| 52 | + }); |
65 | 53 | });
|
| 54 | + req.write(postData); |
| 55 | + req.end(); |
66 | 56 | });
|
67 |
| - req.end(); |
68 |
| -}; |
69 | 57 |
|
70 |
| -// 4. After the protractor test completes, check to see that the session still |
71 |
| -// exists. If we can find the session, delete it. |
72 |
| -var checkStoppedSession = function() { |
73 |
| - var checkOptions = { |
74 |
| - hostname: 'localhost', |
75 |
| - port: 4444, |
76 |
| - path: '/wd/hub/session/' + sessionId, |
77 |
| - method: 'GET' |
78 |
| - }; |
79 |
| - var state = ''; |
80 |
| - var req = http.request(checkOptions, function(res) { |
81 |
| - res.on('data', function(chunk) { |
82 |
| - state = JSON.parse(chunk.toString()).state; |
| 58 | + await new Promise(resolve => { |
| 59 | + // 2. After getting the session id, verify that the selenium session exists. |
| 60 | + // If the session exists, run the protractor test. |
| 61 | + const checkOptions = { |
| 62 | + hostname: '127.0.0.1', |
| 63 | + port: 4444, |
| 64 | + path: '/wd/hub/sessions', |
| 65 | + method: 'GET' |
| 66 | + }; |
| 67 | + |
| 68 | + let values = []; |
| 69 | + const req = http.request(checkOptions, (res) => { |
| 70 | + res.on('data', (chunk) => { |
| 71 | + values = JSON.parse(chunk.toString())['value']; |
| 72 | + }); |
| 73 | + res.on('end', () => { |
| 74 | + let found = false; |
| 75 | + for (let value of values) { |
| 76 | + if (value['id'] === sessionId) { |
| 77 | + found = true; |
| 78 | + } |
| 79 | + } |
| 80 | + if (found) { |
| 81 | + resolve(); |
| 82 | + } else { |
| 83 | + throw new Error('The selenium session was not created.'); |
| 84 | + } |
| 85 | + }); |
83 | 86 | });
|
84 |
| - res.on('end', function() { |
85 |
| - if (state === 'success') { |
86 |
| - deleteSession(); |
87 |
| - } |
88 |
| - else { |
89 |
| - throw new Error('The selenium session should still exist.'); |
90 |
| - } |
| 87 | + req.end(); |
| 88 | + }); |
| 89 | + |
| 90 | + // 3. Run Protractor and attach to the session. |
| 91 | + const runProtractor = child_process.spawnSync('node', |
| 92 | + ['bin/protractor', 'spec/driverProviderAttachSessionConf.js', |
| 93 | + '--seleniumSessionId=' + sessionId]); |
| 94 | + console.log(runProtractor.stdout.toString()); |
| 95 | + if (runProtractor.status !== 0) { |
| 96 | + const e = new Error('Protractor did not run properly.'); |
| 97 | + deleteSession(sessionId, e); |
| 98 | + } |
| 99 | + |
| 100 | + // 4. After the protractor test completes, check to see that the session still |
| 101 | + // exists. If we can find the session, delete it. |
| 102 | + await new Promise(resolve => { |
| 103 | + const checkOptions = { |
| 104 | + hostname: '127.0.0.1', |
| 105 | + port: 4444, |
| 106 | + path: '/wd/hub/session/' + sessionId, |
| 107 | + method: 'GET' |
| 108 | + }; |
| 109 | + const req = http.request(checkOptions, (res) => { |
| 110 | + let state = ''; |
| 111 | + res.on('data', (chunk) => { |
| 112 | + state = JSON.parse(chunk.toString()).state; |
| 113 | + }); |
| 114 | + res.on('end', () => { |
| 115 | + if (state === 'success') { |
| 116 | + resolve(); |
| 117 | + } |
| 118 | + else { |
| 119 | + const e = new Error('The selenium session should still exist.'); |
| 120 | + deleteSession(sessionId, e); |
| 121 | + } |
| 122 | + }); |
91 | 123 | });
|
| 124 | + req.end(); |
92 | 125 | });
|
93 |
| - req.end(); |
94 |
| -}; |
95 | 126 |
|
96 |
| -// 5. Delete the selenium session. |
97 |
| -var deleteSession = function() { |
98 |
| - var deleteOptions = { |
99 |
| - hostname: 'localhost', |
100 |
| - port: 4444, |
101 |
| - path: '/wd/hub/session/' + sessionId, |
102 |
| - method: 'DELETE' |
103 |
| - }; |
104 |
| - var req = http.request(deleteOptions); |
105 |
| - req.end(); |
106 |
| -}; |
| 127 | + // 5. Delete the selenium session. |
| 128 | + await deleteSession(sessionId); |
| 129 | +} |
| 130 | + |
| 131 | +run().then(); |
0 commit comments