|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +var http = require('http'), |
| 6 | + spawn = require('child_process').spawnSync; |
| 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; |
| 35 | +}; |
| 36 | + |
| 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('bin/protractor', |
| 54 | + ['spec/attachSession.js', '--seleniumSessionId=' + sessionId]); |
| 55 | + console.log(runProtractor.stdout.toString()); |
| 56 | + if (runProtractor.status !== 0) { |
| 57 | + throw new Error('Protractor did not run properly.'); |
| 58 | + } |
| 59 | + } |
| 60 | + else { |
| 61 | + throw new Error('The selenium session was not created.'); |
| 62 | + } |
| 63 | + checkStoppedSession(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + req.end(); |
| 67 | +}; |
| 68 | + |
| 69 | +// 4. After the protractor test completes, check to see that the session still |
| 70 | +// exists. If we can find the session, delete it. |
| 71 | +var checkStoppedSession = function() { |
| 72 | + var checkOptions = { |
| 73 | + hostname: 'localhost', |
| 74 | + port: 4444, |
| 75 | + path: '/wd/hub/session/' + sessionId, |
| 76 | + method: 'GET' |
| 77 | + }; |
| 78 | + var state = ''; |
| 79 | + var req = http.request(checkOptions, function(res) { |
| 80 | + res.on('data', function(chunk) { |
| 81 | + state = JSON.parse(chunk.toString()).state; |
| 82 | + }); |
| 83 | + res.on('end', function() { |
| 84 | + if (state === 'success') { |
| 85 | + deleteSession(); |
| 86 | + } |
| 87 | + else { |
| 88 | + throw new Error('The selenium session should still exist.'); |
| 89 | + } |
| 90 | + }); |
| 91 | + }); |
| 92 | + req.end(); |
| 93 | +}; |
| 94 | + |
| 95 | +// 5. Delete the selenium session. |
| 96 | +var deleteSession = function() { |
| 97 | + var deleteOptions = { |
| 98 | + hostname: 'localhost', |
| 99 | + port: 4444, |
| 100 | + path: '/wd/hub/session/' + sessionId, |
| 101 | + method: 'DELETE' |
| 102 | + }; |
| 103 | + var req = http.request(deleteOptions); |
| 104 | + req.end(); |
| 105 | +}; |
0 commit comments