From 9ddeeae0e02e1a02503f813a5d472360ec922787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Macias?= Date: Mon, 19 Dec 2016 22:21:01 +0100 Subject: [PATCH] v0.2.0: upgrade selenium-webdriver to v3.0.1, use babel-preset-env and require nodejs >= 6.9 --- .babelrc | 18 +++++++++++++ makefile | 5 +++- package.json | 20 +++++++------- src/driver/index.js | 16 ++++++------ src/driver/local.js | 64 +++++++++++++++++++++++++++++++++------------ 5 files changed, 88 insertions(+), 35 deletions(-) create mode 100644 .babelrc diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..f2d70df --- /dev/null +++ b/.babelrc @@ -0,0 +1,18 @@ +{ + "presets": [ + [ + "env", + { + "targets": { + "node": "6.9" + } + } + ] + ], + "plugins": [ + "transform-object-rest-spread" + ], + "ignore": [ + "*.spec.js" + ] +} diff --git a/makefile b/makefile index bb544ab..f27e123 100644 --- a/makefile +++ b/makefile @@ -2,5 +2,8 @@ install: @echo "Installing Node dependencies" @npm install +build-dev: + ./node_modules/.bin/babel --watch -d lib/ src/ + build: - ./node_modules/.bin/babel --presets es2015,stage-2 -d lib/ src/ + ./node_modules/.bin/babel -d lib/ src/ diff --git a/package.json b/package.json index a7f7e8e..0d41e1f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nodium", - "version": "0.1.5", + "version": "0.2.0", "description": "Pure nodejs selenium webdriver functional helpers", "homepage": "https://github.com/jeromemacias/nodium", "repository": { @@ -21,26 +21,26 @@ "url": "https://github.com/jeromemacias/nodium/issues" }, "engines": { - "node": "~4.0" + "node": "~6.9" }, "engine-strict": true, "main": "lib/driver/index.js", "dependencies": { - "selenium-webdriver": "~2.53.2" + "selenium-webdriver": "~3.0.1" }, "devDependencies": { - "babel-cli": "~6.9.0", - "babel-eslint": "~6.0.4", - "babel-preset-es2015": "~6.9.0", - "babel-preset-stage-2": "~6.5.0", - "eslint": "~2.11.1" + "babel-cli": "~6.18.0", + "babel-eslint": "~7.1.1", + "babel-preset-env": "~1.1.4", + "babel-plugin-transform-object-rest-spread": "~6.16.0", + "eslint": "~3.12.2" }, "peerDependencies": { "chai": "~3.5", - "chai-as-promised": "~5.3", + "chai-as-promised": "^5.*", "pm2": "*", "request": "2.*", - "json-server": "0.8.*", + "json-server": "0.*", "body-parser": "1.*" } } diff --git a/src/driver/index.js b/src/driver/index.js index 2f39541..1a07b09 100644 --- a/src/driver/index.js +++ b/src/driver/index.js @@ -17,10 +17,10 @@ if (process.env.BROWSERSTACK) { } else { switch (browser.name.toLowerCase()) { case 'chrome': - browser.version = '49'; + browser.version = '54'; break; case 'firefox': - browser.version = '45'; + browser.version = '49'; break; default: throw new Error(`Cannot set default version for browser ${browser.name}`); @@ -57,13 +57,13 @@ if (process.env.BROWSERSTACK) { if (process.env.VERBOSE_MODE) { options.verbose = true; } - if ('firefox' !== browser.toLowerCase()) { - const binaryPath = process.env.SELENIUM_BROWSER_BINARY_PATH; - if (!binaryPath) { - throw new Error(`You must provide a browser binary path using "SELENIUM_BROWSER_BINARY_PATH" env var.`); - } - options.binaryPath = binaryPath; + + const binaryPath = process.env.SELENIUM_BROWSER_BINARY_PATH; + if (!binaryPath) { + throw new Error(`You must provide a browser binary path using "SELENIUM_BROWSER_BINARY_PATH" env var.`); } + options.binaryPath = binaryPath; + driver = getLocalDriver(browser.toLowerCase(), options); console.log(`Use ${browser.toLowerCase()} browser`); } diff --git a/src/driver/local.js b/src/driver/local.js index 0353ee5..a876eec 100644 --- a/src/driver/local.js +++ b/src/driver/local.js @@ -1,42 +1,74 @@ -import chrome from 'selenium-webdriver/chrome'; -import firefox from 'selenium-webdriver/firefox'; +import { + ServiceBuilder as ChromeServiceBuilder, + Driver as ChromeDriver, + Options as ChromeOptions, +} from 'selenium-webdriver/chrome'; +import { + ServiceBuilder as FirefoxServiceBuilder, + Driver as FirefoxDriver, + Options as FirefoxOptions, +} from 'selenium-webdriver/firefox'; function getChromeService(binaryPath) { const chromeBinary = binaryPath || __dirname + '/../node_modules/webdriver-manager/selenium/chromedriver'; - return (new chrome.ServiceBuilder(chromeBinary)).build(); + return new ChromeServiceBuilder(chromeBinary); } function getChromeOptions() { - return new chrome.Options().addArguments(['--no-sandbox']); + return new ChromeOptions().addArguments(['--no-sandbox']); } function getChromeDriver(binaryPath) { - return new chrome.Driver(getChromeOptions(), getChromeService(binaryPath)); + return ChromeDriver.createSession(getChromeOptions(), getChromeService(binaryPath).build()); } -function getChromeDriverWithVerboseLogging(filePath) { - const service = getChromeService() +function getChromeDriverWithVerboseLogging(binaryPath, logPath) { + const service = getChromeService(binaryPath) .enableVerboseLogging() - .loggingTo(filePath || __dirname + '/../chromedriver.log') + .loggingTo(logPath || __dirname + '/../chromedriver.log') .build(); - return new chrome.Driver(getChromeOptions(), service); + return ChromeDriver.createSession(getChromeOptions(), service); } -function getFirefoxDriver() { - return new firefox.Driver(); +function getFirefoxService(binaryPath) { + const firefoxBinary = binaryPath || __dirname + '/../node_modules/webdriver-manager/selenium/geckodriver'; + + return new FirefoxServiceBuilder(firefoxBinary); +} + +function getFirefoxOptions() { + return new FirefoxOptions(); } -export default function getLocalDriver(browser, options = {}) { +function getFirefoxDriver(binaryPath) { + return FirefoxDriver.createSession(getFirefoxOptions(), getFirefoxService(binaryPath).build()); +} + +function getFirefoxDriverWithVerboseLogging(binaryPath, logPath) { + const service = getFirefoxService(binaryPath) + .enableVerboseLogging() + .build(); + + return FirefoxDriver.createSession(getFirefoxOptions(), service); +} + +export default function getLocalDriver(browser, { binaryPath, verbose, logPath }) { + let driver; switch (browser) { case 'chrome': - if (options.verbose) { - return getChromeDriverWithVerboseLogging(options.logPath) + if (verbose) { + return getChromeDriverWithVerboseLogging(binaryPath, logPath); } - return getChromeDriver(options.binaryPath); + + return getChromeDriver(binaryPath); case "firefox": - return getFirefoxDriver(); + if (verbose) { + return getFirefoxDriverWithVerboseLogging(binaryPath, logPath); + } + + return getFirefoxDriver(binaryPath); default: throw new Error(`No local driver found for "${browser}"`); } }