|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +var fs = require('fs'); |
| 4 | +var os = require('os'); |
| 5 | +var url = require('url'); |
| 6 | +var http = require('http'); |
| 7 | +var path = require('path'); |
| 8 | +var AdmZip = require('adm-zip'); |
| 9 | +var optimist = require('optimist'); |
| 10 | + |
| 11 | +/** |
| 12 | + * Download the requested binaries to node_modules/protractor/selenium/ |
| 13 | + */ |
| 14 | +var SELENIUM_DIR = path.resolve(__dirname, '../selenium'); |
| 15 | + |
| 16 | +var versions = require('../package.json').webdriverVersions; |
| 17 | + |
| 18 | +var binaries = { |
| 19 | + standalone: { |
| 20 | + name: 'selenium standalone', |
| 21 | + isDefault: true, |
| 22 | + prefix: 'selenium-server-standalone', |
| 23 | + filename: 'selenium-server-standalone-' + versions.selenium + '.jar', |
| 24 | + url: function() { |
| 25 | + return 'https://selenium.googlecode.com/files/' + |
| 26 | + 'selenium-server-standalone-' + versions.selenium+ '.jar'; |
| 27 | + } |
| 28 | + }, |
| 29 | + chrome: { |
| 30 | + name: 'chromedriver', |
| 31 | + isDefault: true, |
| 32 | + prefix: 'chromedriver_', |
| 33 | + filename: 'chromedriver_' + versions.chromedriver + '.zip', |
| 34 | + url: function() { |
| 35 | + var urlPrefix = 'https://chromedriver.storage.googleapis.com/' + |
| 36 | + versions.chromedriver + '/chromedriver_'; |
| 37 | + if (os.type() == 'Darwin') { |
| 38 | + return urlPrefix + 'mac32.zip'; |
| 39 | + } else if (os.type() == 'Linux') { |
| 40 | + if (os.arch() == 'x64') { |
| 41 | + return urlPrefix + 'linux64.zip'; |
| 42 | + } else { |
| 43 | + return urlPrefix + 'linux32.zip'; |
| 44 | + } |
| 45 | + } else if (os.type() == 'Windows_NT') { |
| 46 | + return urlPrefix + 'win32.zip'; |
| 47 | + } |
| 48 | + } |
| 49 | + }, |
| 50 | + ie: { |
| 51 | + name: 'IEDriver', |
| 52 | + isDefault: false, |
| 53 | + prefix: 'IEDriverServer', |
| 54 | + filename: 'IEDriverServer_' + versions.iedriver + '.zip', |
| 55 | + url: function() { |
| 56 | + var urlPrefix = 'https://selenium.googlecode.com/files/IEDriverServer'; |
| 57 | + if (os.type() == 'Windows_NT') { |
| 58 | + if (os.arch() == 'x64') { |
| 59 | + binaries.iedriver.url = urlPrefix + '_x64_' + versions.iedriver + |
| 60 | + '.zip'; |
| 61 | + } else { |
| 62 | + binaries.iedriver.url = urlPrefix + '_win32_' + versions.iedriver + |
| 63 | + '.zip'; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | + |
| 71 | +var cli = optimist. |
| 72 | + usage('Usage: manage_webdriver <command>\n' + |
| 73 | + 'Commands:\n' + |
| 74 | + ' update: install or update selected binaries\n' + |
| 75 | + ' start: start up the selenium server\n' + |
| 76 | + ' status: list the current available drivers'). |
| 77 | + describe('out_dir', 'Location to output/expect '). |
| 78 | + default('out_dir', SELENIUM_DIR); |
| 79 | + |
| 80 | +for (bin in binaries) { |
| 81 | + cli.describe(bin, 'install or update ' + binaries[bin].name). |
| 82 | + boolean(bin). |
| 83 | + default(bin, binaries[bin].isDefault); |
| 84 | +} |
| 85 | + |
| 86 | +var argv = cli. |
| 87 | + check(function(arg) { |
| 88 | + if (arg._.length != 1) { |
| 89 | + throw 'Please specify one command'; |
| 90 | + } |
| 91 | + }). |
| 92 | + argv; |
| 93 | + |
| 94 | +if (!fs.existsSync(argv.out_dir) || !fs.statSync(argv.out_dir).isDirectory()) { |
| 95 | + fs.mkdirSync(argv.out_dir); |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Function to download file using HTTP.get. |
| 100 | + * Thanks to http://www.hacksparrow.com/using-node-js-to-download-files.html |
| 101 | + * for the outline of this code. |
| 102 | + */ |
| 103 | +var httpGetFile = function(fileUrl, fileName, outputDir, callback) { |
| 104 | + console.log('downloading ' + fileUrl + '...'); |
| 105 | + var options = { |
| 106 | + host: url.parse(fileUrl).host, |
| 107 | + port: 80, |
| 108 | + path: url.parse(fileUrl).pathname |
| 109 | + }; |
| 110 | + |
| 111 | + var filePath = path.join(outputDir, fileName); |
| 112 | + var file = fs.createWriteStream(filePath); |
| 113 | + |
| 114 | + http.get(options, function(res) { |
| 115 | + res.on('data', function(data) { |
| 116 | + file.write(data); |
| 117 | + }).on('end', function() { |
| 118 | + file.end(function() { |
| 119 | + console.log(fileName + ' downloaded to ' + filePath); |
| 120 | + if (callback) { |
| 121 | + callback(filePath); |
| 122 | + } |
| 123 | + }); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}; |
| 127 | + |
| 128 | +/** |
| 129 | + * If a new version of the file with the given url exists, download and |
| 130 | + * delete the old version. |
| 131 | + */ |
| 132 | +var downloadIfNew = function(bin, outputDir, existingFiles, opt_callback) { |
| 133 | + if (!bin.exists) { |
| 134 | + // Remove anything else that matches the exclusive prefix. |
| 135 | + existingFiles.forEach(function(file) { |
| 136 | + if (file.indexOf(bin.prefix) != -1) { |
| 137 | + fs.unlinkSync(path.join(outputDir, file)); |
| 138 | + } |
| 139 | + }); |
| 140 | + console.log('Updating ' + bin.name); |
| 141 | + var url = bin.url(); |
| 142 | + if (!url) { |
| 143 | + console.error(bin.name + ' is not available for your system.'); |
| 144 | + return; |
| 145 | + } |
| 146 | + httpGetFile(url, bin.filename, outputDir, function(downloaded) { |
| 147 | + if (opt_callback) { |
| 148 | + opt_callback(downloaded); |
| 149 | + } |
| 150 | + }); |
| 151 | + } else { |
| 152 | + console.log(bin.name + ' is up to date.'); |
| 153 | + } |
| 154 | +}; |
| 155 | + |
| 156 | +/** |
| 157 | + * Append '.exe' to a filename if the system is windows. |
| 158 | + */ |
| 159 | +var executableName = function(file) { |
| 160 | + if (os.type() == 'Windows_NT') { |
| 161 | + return file + '.exe'; |
| 162 | + } else { |
| 163 | + return file; |
| 164 | + } |
| 165 | +}; |
| 166 | + |
| 167 | + |
| 168 | +// Setup before any command. |
| 169 | +var existingFiles = fs.readdirSync(argv.out_dir); |
| 170 | + |
| 171 | +for (name in binaries) { |
| 172 | + bin = binaries[name]; |
| 173 | + var exists = fs.existsSync(path.join(argv.out_dir, bin.filename)); |
| 174 | + var outOfDateExists = false; |
| 175 | + existingFiles.forEach(function(file) { |
| 176 | + if (file.indexOf(bin.prefix) != -1 && file != bin.filename) { |
| 177 | + outOfDateExists = true; |
| 178 | + } |
| 179 | + }); |
| 180 | + bin.exists = exists; |
| 181 | + bin.outOfDateExists = outOfDateExists; |
| 182 | +} |
| 183 | + |
| 184 | +switch (argv._[0]) { |
| 185 | + case 'start': |
| 186 | + if (!binaries.standalone.exists) { |
| 187 | + console.error('Selenium Standalone is not present. Install with ' + |
| 188 | + 'manage_webdriver update --standalone'); |
| 189 | + process.exit(1); |
| 190 | + } |
| 191 | + var args = ['-jar', path.join(argv.out_dir, binaries.standalone.filename)]; |
| 192 | + if (binaries.chrome.exists) { |
| 193 | + args.push('-Dwebdriver.chrome.driver=' + |
| 194 | + path.join(argv.out_dir, executableName('chromedriver'))); |
| 195 | + } |
| 196 | + if (binaries.ie.exists) { |
| 197 | + args.push('-Dwebdriver.ie.driver=' + |
| 198 | + path.join(argv.out_dir, executableName('IEDriverServer'))); |
| 199 | + } |
| 200 | + var javaChild = require('child_process').spawn( |
| 201 | + 'java', args, { stdio: 'inherit' }); |
| 202 | + break; |
| 203 | + case 'status': |
| 204 | + for (name in binaries) { |
| 205 | + bin = binaries[name]; |
| 206 | + if (bin.exists) { |
| 207 | + console.log(bin.name + ' is up to date'); |
| 208 | + } else if (bin.outOfDateExists) { |
| 209 | + console.log('**' + bin.name + ' needs to be updated'); |
| 210 | + } else { |
| 211 | + console.log(bin.name + ' is not present') |
| 212 | + } |
| 213 | + } |
| 214 | + break; |
| 215 | + case 'update': |
| 216 | + if (argv.standalone) { |
| 217 | + downloadIfNew(binaries.standalone, argv.out_dir, existingFiles); |
| 218 | + } |
| 219 | + if (argv.chrome) { |
| 220 | + downloadIfNew(binaries.chrome, argv.out_dir, existingFiles, |
| 221 | + function(filename) { |
| 222 | + var zip = new AdmZip(filename); |
| 223 | + // Expected contents of the zip: |
| 224 | + // mac/linux: chromedriver |
| 225 | + // windows: chromedriver.exe |
| 226 | + zip.extractAllTo(argv.out_dir); |
| 227 | + if (os.type() != 'Windows_NT') { |
| 228 | + fs.chmodSync(path.join(argv.out_dir, 'chromedriver'), 0755); |
| 229 | + } |
| 230 | + }); |
| 231 | + } |
| 232 | + if (argv.ie) { |
| 233 | + downloadIfNew(binaries.ie, argv.out_dir, existingFiles, |
| 234 | + function(filename) { |
| 235 | + // Expected contents of the zip: |
| 236 | + // IEDriverServer.exe |
| 237 | + zip.extractAllTo(argv.out_dir); |
| 238 | + }); |
| 239 | + } |
| 240 | + break; |
| 241 | + default: |
| 242 | + console.error('Invalid command'); |
| 243 | + optimist.showHelp(); |
| 244 | +} |
0 commit comments