From 1c302a6981decfee6be481617a279824671a8660 Mon Sep 17 00:00:00 2001 From: Lei Zhu Date: Mon, 24 Apr 2017 17:21:56 -0700 Subject: [PATCH 1/2] add cmd line args to switch mocha test explicitly --- README.md | 2 +- package.json | 3 +- src/help.js | 5 + src/logger.js | 38 -------- src/profile.js | 163 +++++++++++++++++++-------------- test/src/configuration.test.js | 10 -- test/src/executor.test.js | 13 +-- test/src/profile.test.js | 84 +++++++++++++++-- 8 files changed, 177 insertions(+), 141 deletions(-) delete mode 100644 src/logger.js diff --git a/README.md b/README.md index 659fcea..3681ba9 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Please follow the steps ] ``` - 3. `./node_modules/.bin/magellan ----help` to see if you can see the following content printed out + 3. `./node_modules/.bin/magellan --help` to see if you can see the following content printed out ``` Executor-specific (testarmada-magellan-local-executor) --local_browser=browsername Run tests in chrome, firefox, etc (default: phantomjs). diff --git a/package.json b/package.json index 3be96d5..a44c752 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "testarmada-magellan-local-executor", - "version": "1.0.0", + "version": "1.1.0", "description": "test executor for magellan test to run in local selenium env", "main": "index.js", "scripts": { @@ -34,6 +34,7 @@ "babel-preset-es2015": "^6.18.0", "cli-color": "^1.1.0", "lodash": "^4.17.4", + "testarmada-logger": "^1.1.1", "yargs": "^6.6.0" }, "devDependencies": { diff --git a/src/help.js b/src/help.js index 7293564..8f8bf2c 100644 --- a/src/help.js +++ b/src/help.js @@ -1,4 +1,9 @@ export default { + "local_mocha":{ + "visible": true, + "type": "boolean", + "description": "Run test via magellan-mocha-plugin (exclusive with list_browser and list_browsers)" + }, "local_browser": { "visible": true, "type": "string", diff --git a/src/logger.js b/src/logger.js deleted file mode 100644 index d748dc3..0000000 --- a/src/logger.js +++ /dev/null @@ -1,38 +0,0 @@ - - -import { argv } from "yargs"; -import util from "util"; -import clc from "cli-color"; - -const debug = argv.debug; -const PREFIX = "Local Executor"; - -export default { - output: console, - - debug(msg) { - if (debug) { - const deb = clc.blueBright("[DEBUG]"); - this.output.log(util.format("%s [%s] %s", deb, PREFIX, msg)); - } - }, - - log(msg) { - const info = clc.greenBright("[INFO]"); - this.output.log(util.format("%s [%s] %s", info, PREFIX, msg)); - }, - - warn(msg) { - const warn = clc.yellowBright("[WARN]"); - this.output.warn(util.format("%s [%s] %s", warn, PREFIX, msg)); - }, - - err(msg) { - const err = clc.redBright("[ERROR]"); - this.output.error(util.format("%s [%s] %s", err, PREFIX, msg)); - }, - - loghelp(msg) { - this.output.log(msg); - } -}; diff --git a/src/profile.js b/src/profile.js index a5242f5..00d91c9 100644 --- a/src/profile.js +++ b/src/profile.js @@ -1,7 +1,8 @@ import path from "path"; import _ from "lodash"; import { argv } from "yargs"; -import logger from "./logger"; +import logger from "testarmada-logger"; +logger.prefix = "Local Executor"; export default { getNightwatchConfig: (profile) => { @@ -19,93 +20,113 @@ export default { if (argvMock) { runArgv = argvMock; } + if (runArgv.local_mocha + || opts.settings.testFramework.name + && opts.settings.testFramework.name === "testarmada-magellan-mocha-plugin") { + // do nothing for mocha + return new Promise((resolve) => resolve([{ executor: "local", id: "mocha" }])); + } else { + const configPath = opts.settings.testFramework.settings.nightwatchConfigFilePath; + + /*eslint-disable global-require*/ + const nightwatchConfig = require(path.resolve(configPath)); + const browsers = nightwatchConfig.test_settings; + + return new Promise((resolve) => { + if (runArgv.local_browser) { + const localBrowser = runArgv.local_browser; + if (browsers[localBrowser]) { + const b = browsers[localBrowser]; - const configPath = opts.settings.testFramework.settings.nightwatchConfigFilePath; - - /*eslint-disable global-require*/ - const nightwatchConfig = require(path.resolve(configPath)); - const browsers = nightwatchConfig.test_settings; - - return new Promise((resolve) => { - if (runArgv.local_browser) { - const localBrowser = runArgv.local_browser; - if (browsers[localBrowser]) { - const b = browsers[localBrowser]; - - b.executor = "local"; - b.nightwatchEnv = localBrowser; - b.id = localBrowser; + b.executor = "local"; + b.nightwatchEnv = localBrowser; + b.id = localBrowser; - logger.debug(`detected profile: ${ JSON.stringify(b)}`); + logger.debug(`detected profile: ${JSON.stringify(b)}`); - resolve([b]); - } - } else if (runArgv.local_browsers) { - const tempBrowsers = runArgv.local_browsers.split(","); - const returnBrowsers = []; + resolve([b]); + } + } else if (runArgv.local_browsers) { + const tempBrowsers = runArgv.local_browsers.split(","); + const returnBrowsers = []; - _.forEach(tempBrowsers, (browser) => { - if (browsers[browser]) { - const b = browsers[browser]; + _.forEach(tempBrowsers, (browser) => { + if (browsers[browser]) { + const b = browsers[browser]; - b.executor = "local"; - b.nightwatchEnv = browser; - b.id = browser; + b.executor = "local"; + b.nightwatchEnv = browser; + b.id = browser; - returnBrowsers.push(b); - } - }); + returnBrowsers.push(b); + } + }); - logger.debug(`detected profiles: ${ JSON.stringify(returnBrowsers)}`); + logger.debug(`detected profiles: ${JSON.stringify(returnBrowsers)}`); - resolve(returnBrowsers); - } else { - resolve(); - } - }); + resolve(returnBrowsers); + } else { + resolve(); + } + }); + } }, /*eslint-disable global-require*/ getCapabilities: (profile, opts) => { - const configPath = opts.settings.testFramework.settings.nightwatchConfigFilePath; - const nightwatchConfig = require(path.resolve(configPath)); - const browsers = nightwatchConfig.test_settings; - - return new Promise((resolve, reject) => { - if (browsers[profile]) { - const b = browsers[profile]; - - b.executor = "local"; - b.nightwatchEnv = profile; - b.id = profile; - - resolve(b); - } else { - reject(`profile: ${ profile } isn't found`); - } - }); + if (argv.local_mocha + || opts.settings.testFramework.name + && opts.settings.testFramework.name === "testarmada-magellan-mocha-plugin") { + // do nothing for mocha + return new Promise((resolve) => resolve({ executor: "local", id: "mocha" })); + } else { + const configPath = opts.settings.testFramework.settings.nightwatchConfigFilePath; + const nightwatchConfig = require(path.resolve(configPath)); + const browsers = nightwatchConfig.test_settings; + + return new Promise((resolve, reject) => { + if (browsers[profile]) { + const b = browsers[profile]; + + b.executor = "local"; + b.nightwatchEnv = profile; + b.id = profile; + + resolve(b); + } else { + reject(`profile: ${profile} isn't found`); + } + }); + } }, /*eslint-disable global-require*/ listBrowsers: (opts, callback) => { - const configPath = opts.settings.testFramework.settings.nightwatchConfigFilePath; - const nightwatchConfig = require(path.resolve(configPath)); - const browsers = nightwatchConfig.test_settings; - - const OMIT_BROWSERS = ["default", "sauce"]; - const listedBrowsers = []; - - - _.forEach(browsers, (capabilities, browser) => { - if (OMIT_BROWSERS.indexOf(browser) < 0) { - logger.debug(` browser: ${ browser}`); - logger.debug(` capabilities: ${ JSON.stringify(capabilities)}`); - listedBrowsers.push(browser); - } - }); + if (argv.local_mocha + || opts.settings.testFramework.name + && opts.settings.testFramework.name === "testarmada-magellan-mocha-plugin") { + // do nothing for mocha + return callback(); + } else { + const configPath = opts.settings.testFramework.settings.nightwatchConfigFilePath; + const nightwatchConfig = require(path.resolve(configPath)); + const browsers = nightwatchConfig.test_settings; + + const OMIT_BROWSERS = ["default", "sauce"]; + const listedBrowsers = []; + + + _.forEach(browsers, (capabilities, browser) => { + if (OMIT_BROWSERS.indexOf(browser) < 0) { + logger.debug(` browser: ${browser}`); + logger.debug(` capabilities: ${JSON.stringify(capabilities)}`); + listedBrowsers.push(browser); + } + }); - logger.log(`Available browsers from file ${ configPath }: ${ listedBrowsers.join(",")}`); + logger.log(`Available browsers from file ${configPath}: ${listedBrowsers.join(",")}`); - callback(); + return callback(); + } } }; diff --git a/test/src/configuration.test.js b/test/src/configuration.test.js index d077e9c..f27f23d 100644 --- a/test/src/configuration.test.js +++ b/test/src/configuration.test.js @@ -3,16 +3,6 @@ import chai from "chai"; import chaiAsPromise from "chai-as-promised"; import _ from "lodash"; -import logger from "../../lib/logger"; - -// eat console logs -// logger.output = { -// log() { }, -// error() { }, -// debug() { }, -// warn() { } -// }; - chai.use(chaiAsPromise); const expect = chai.expect; diff --git a/test/src/executor.test.js b/test/src/executor.test.js index 4dd9155..79256be 100644 --- a/test/src/executor.test.js +++ b/test/src/executor.test.js @@ -3,16 +3,6 @@ import chai from "chai"; import chaiAsPromise from "chai-as-promised"; import _ from "lodash"; -import logger from "../../lib/logger"; - -// eat console logs -// logger.output = { -// log() { }, -// error() { }, -// debug() { }, -// warn() { } -// }; - chai.use(chaiAsPromise); const expect = chai.expect; @@ -63,4 +53,7 @@ describe("Executor", () => { expect(r).to.equal(1); }); + it("summerizeTest", (done) => { + executor.summerizeTest(null, null, () => done()); + }); }); \ No newline at end of file diff --git a/test/src/profile.test.js b/test/src/profile.test.js index bfd60ea..a46a089 100644 --- a/test/src/profile.test.js +++ b/test/src/profile.test.js @@ -3,16 +3,6 @@ import chai from "chai"; import chaiAsPromise from "chai-as-promised"; import _ from "lodash"; -import logger from "../../lib/logger"; - -// eat console logs -// logger.output = { -// log() { }, -// error() { }, -// debug() { }, -// warn() { } -// }; - chai.use(chaiAsPromise); const expect = chai.expect; @@ -20,6 +10,47 @@ const assert = chai.assert; describe("Profile", () => { describe("getProfiles", () => { + it("mocha via cmd", () => { + let argvMock = { + local_mocha: true + }; + + let opts = { + settings: { + testFramework: { + } + } + }; + + return profile + .getProfiles(opts, argvMock) + .then((p) => { + expect(p.length).to.equal(1); + expect(p[0].executor).to.equal("local"); + expect(p[0].id).to.equal("mocha"); + }) + .catch(err => assert(false, "getProfile isn't functional" + err)); + }); + + it("mocha via plugin", () => { + let opts = { + settings: { + testFramework: { + name: "testarmada-magellan-mocha-plugin" + } + } + }; + + return profile + .getProfiles(opts) + .then((p) => { + expect(p.length).to.equal(1); + expect(p[0].executor).to.equal("local"); + expect(p[0].id).to.equal("mocha"); + }) + .catch(err => assert(false, "getProfile isn't functional" + err)); + }); + it("with local_browser", () => { let argvMock = { local_browser: "chrome" @@ -100,6 +131,24 @@ describe("Profile", () => { }); describe("getCapabilities", () => { + it("mocha", () => { + let opts = { + settings: { + testFramework: { + name: "testarmada-magellan-mocha-plugin" + } + } + }; + + return profile + .getCapabilities(null, opts) + .then((p) => { + expect(p.executor).to.equal("local"); + expect(p.id).to.equal("mocha"); + }) + .catch(err => assert(false, "getProfile isn't functional" + err)); + }); + it("succeed", () => { let opts = { settings: { @@ -155,5 +204,20 @@ describe("Profile", () => { done(); }); }); + + it("mocha listBrowsers", (done) => { + let opts = { + settings: { + testFramework: { + name: "testarmada-magellan-mocha-plugin" + } + } + }; + + return profile + .listBrowsers(opts, () => { + done(); + }); + }); }); }); \ No newline at end of file From a6710591cb0bed368ccd5b62a858f6b9957b8df7 Mon Sep 17 00:00:00 2001 From: Lei Zhu Date: Mon, 24 Apr 2017 17:26:45 -0700 Subject: [PATCH 2/2] fix lint --- src/help.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/help.js b/src/help.js index 8f8bf2c..5c172d3 100644 --- a/src/help.js +++ b/src/help.js @@ -1,8 +1,9 @@ export default { - "local_mocha":{ + "local_mocha": { "visible": true, "type": "boolean", - "description": "Run test via magellan-mocha-plugin (exclusive with list_browser and list_browsers)" + "description": "Run test via magellan-mocha-plugin " + + "(exclusive with list_browser and list_browsers)" }, "local_browser": { "visible": true,