Skip to content

Commit 34b273a

Browse files
committed
test(e2e): allow testing on remote browsers with browserstack
Still not working: failing assertions do not report back to browserstack but they do make the command exit with a failure code
1 parent a79b8be commit 34b273a

File tree

5 files changed

+342
-98
lines changed

5 files changed

+342
-98
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ docs/.vuepress/dist
1111
yarn-error.log
1212
.idea
1313
.vscode/settings.json
14-
keys.env
14+
.env
1515
selenium-server.log
16+
local.log

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@
5656
"babel-plugin-syntax-dynamic-import": "^6.18.0",
5757
"babel-preset-env": "^1.6.1",
5858
"babel-preset-flow-vue": "^1.0.0",
59+
"browserstack-local": "^1.4.0",
5960
"buble": "^0.19.3",
6061
"chromedriver": "^74.0.0",
6162
"conventional-changelog-cli": "^2.0.11",
6263
"cross-spawn": "^6.0.5",
6364
"css-loader": "^2.1.1",
65+
"dotenv": "^8.0.0",
6466
"es6-promise": "^4.2.8",
6567
"eslint": "^4.19.1",
6668
"eslint-plugin-flowtype": "^2.46.1",

test/e2e/nightwatch.browserstack.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Running tests on remote browsers
3+
*/
4+
5+
const BS_USER = process.env.BS_USER
6+
const BS_KEY = process.env.BS_KEY
7+
8+
if (!BS_USER || !BS_KEY) {
9+
console.log(
10+
'Hey!\n',
11+
'You are missing credentials for Browserstack.\n',
12+
'If you are a contributor, this is normal, credentials are private. These tests must be run by a maintainer of vue-router',
13+
'If you are a maintainer, you forgot to `source keys.env`!'
14+
)
15+
process.exit(0)
16+
}
17+
18+
const nwConf = {
19+
src_folders: ['test/e2e/specs'],
20+
output_folder: 'test/e2e/reports',
21+
custom_commands_path: ['node_modules/nightwatch-helpers/commands'],
22+
custom_assertions_path: ['node_modules/nightwatch-helpers/assertions'],
23+
24+
selenium: {
25+
start_process: false,
26+
host: 'hub-cloud.browserstack.com',
27+
port: 80
28+
},
29+
30+
common_capabilities: {
31+
'browserstack.user': BS_USER,
32+
'browserstack.key': BS_KEY,
33+
name: 'Bstack-[Nightwatch] Vue Router Parallel Test',
34+
'browserstack.local': true,
35+
'browserstack.video': false,
36+
acceptSslCerts: true
37+
},
38+
39+
test_settings: {
40+
// default: {},
41+
42+
chrome: {
43+
desiredCapabilities: {
44+
browser: 'chrome'
45+
// name: 'Bstack-[Nightwatch] Vue Router',
46+
}
47+
},
48+
49+
ie: {
50+
desiredCapabilities: {
51+
browser: 'internet explorer',
52+
browser_version: '9'
53+
// name: 'Bstack-[Nightwatch] Vue Router',
54+
// 'browserstack.video': true
55+
}
56+
}
57+
}
58+
}
59+
60+
// Code to copy seleniumhost/port into test settings
61+
for (const setting in nwConf.test_settings) {
62+
const config = nwConf.test_settings[setting]
63+
config['selenium_host'] = nwConf.selenium.host
64+
config['selenium_port'] = nwConf.selenium.port
65+
66+
// merge common_capabilities
67+
for (const key in nwConf.common_capabilities) {
68+
// fallback to common_capabilities
69+
config['desiredCapabilities'][key] =
70+
config['desiredCapabilities'][key] || nwConf.common_capabilities[key]
71+
}
72+
}
73+
74+
module.exports = nwConf

test/e2e/runner.js

Lines changed: 108 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* Running tests
3+
*
4+
* By default tests are run locally on chrome headless
5+
* $ node test/e2e/runner.js
6+
*
7+
* You can run a specific test by passing it, or pass various tests
8+
* $ node test/e2e/runner.js test/e2e/specs/basic.js test/e2e/specs/redirect.js
9+
*
10+
* You can specify a list of browsers to run from nightwatch.config.js with -e separated by a comma
11+
* $ node test/e2e/runner.js -e safari,firefox
12+
*
13+
* If you are already running the dev server with `yarn run serve`, you can pass the --dev option to avoid launching the server
14+
* $ node test/e2e/runner.js --dev
15+
*
16+
* __For maintainers only__
17+
* You can trigger tests on Browserstack on other browsers by passing the --local option
18+
* It's also required to pass the list of browsers to test on to avoid launching too many tests. Available options are located inside nightwatch.browserstack.js
19+
* $ node test/e2e/runner.js --local -e ie,chrome50
20+
*/
21+
22+
require('dotenv').config()
123
const { resolve } = require('path')
224
const Nightwatch = require('nightwatch')
325
const args = process.argv.slice(2)
@@ -6,37 +28,101 @@ const args = process.argv.slice(2)
628
const server =
729
args.indexOf('--dev') > -1 ? null : require('../../examples/server')
830

31+
// allow running browserstack local
32+
const isLocal = args.indexOf('--local') > -1
33+
934
const DEFAULT_CONFIG = './nightwatch.json'
35+
const NW_CONFIG = isLocal
36+
? resolve(__dirname, './nightwatch.browserstack.js')
37+
: resolve(__dirname, './nightwatch.config.js')
1038

11-
// read the CLI arguments
12-
Nightwatch.cli(function (argv) {
39+
// add a configuration by default if not provided
40+
if (args.indexOf('-c') < -1) {
41+
args.push('-c', NW_CONFIG)
42+
}
43+
44+
function adaptArgv (argv) {
1345
// take every remaining argument and treat it as a test file
1446
// this allows to run `node test/e2e/runner.js test/e2e/basic.js`
1547
argv.test = argv['_'].slice(0)
1648

17-
// add a configuration by default if not provided
1849
if (argv.c === DEFAULT_CONFIG && argv.config === DEFAULT_CONFIG) {
19-
argv.config = resolve(__dirname, './nightwatch.config.js')
50+
argv.config = argv.c = NW_CONFIG
2051
}
2152
// Nightwatch does not accept an array with one element
2253
if (argv.test.length === 1) argv.test = argv.test[0]
2354

24-
// create the Nightwatch CLI runner
25-
const runner = Nightwatch.CliRunner(argv)
26-
27-
// setup and run tests
28-
runner
29-
.setup()
30-
.startWebDriver()
31-
.then(() => runner.runTests())
32-
.then(() => {
33-
runner.stopWebDriver()
34-
server && server.close()
35-
process.exit(0)
36-
})
37-
.catch(err => {
38-
server && server.close()
55+
// debugging easily
56+
// console.log(argv)
57+
// process.exit(0)
58+
}
59+
60+
if (isLocal) {
61+
process.mainModule.filename = resolve(
62+
__dirname,
63+
'../../node_modules/.bin/nightwatch'
64+
)
65+
let bsLocal
66+
const browserstack = require('browserstack-local')
67+
Nightwatch.bs_local = bsLocal = new browserstack.Local()
68+
bsLocal.start({ key: process.env.BS_KEY }, function (error) {
69+
if (error) throw error
70+
71+
console.log('Connected. Now testing...')
72+
try {
73+
Nightwatch.cli(function (argv) {
74+
adaptArgv(argv)
75+
console.log(argv)
76+
Nightwatch.CliRunner(argv)
77+
.setup(null, function () {
78+
// NOTE: I don't know when this is running or if it does
79+
// Code to stop browserstack local after end of parallel test
80+
bsLocal.stop(function () {
81+
server && server.close()
82+
process.exit(0)
83+
})
84+
})
85+
.runTests()
86+
.then(() => {
87+
// Code to stop browserstack local after end of single test
88+
bsLocal.stop(function () {
89+
server && server.close()
90+
process.exit(0)
91+
})
92+
})
93+
.catch(() => {
94+
server && server.close()
95+
// fail execution
96+
process.exit(1)
97+
})
98+
})
99+
} catch (err) {
39100
console.error(err)
40-
process.exit(1)
41-
})
42-
})
101+
bsLocal.stop(() => {
102+
process.exit(1)
103+
})
104+
}
105+
})
106+
} else {
107+
// create the Nightwatch CLI runner
108+
Nightwatch.cli(function (argv) {
109+
adaptArgv(argv)
110+
const runner = Nightwatch.CliRunner(argv)
111+
112+
// setup and run tests
113+
runner
114+
.setup()
115+
.startWebDriver()
116+
.then(() => runner.runTests())
117+
.then(() => {
118+
runner.stopWebDriver()
119+
server && server.close()
120+
process.exit(0)
121+
})
122+
.catch(err => {
123+
server && server.close()
124+
console.error(err)
125+
process.exit(1)
126+
})
127+
})
128+
}

0 commit comments

Comments
 (0)