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 ( )
1
23
const { resolve } = require ( 'path' )
2
24
const Nightwatch = require ( 'nightwatch' )
3
25
const args = process . argv . slice ( 2 )
@@ -6,37 +28,101 @@ const args = process.argv.slice(2)
6
28
const server =
7
29
args . indexOf ( '--dev' ) > - 1 ? null : require ( '../../examples/server' )
8
30
31
+ // allow running browserstack local
32
+ const isLocal = args . indexOf ( '--local' ) > - 1
33
+
9
34
const DEFAULT_CONFIG = './nightwatch.json'
35
+ const NW_CONFIG = isLocal
36
+ ? resolve ( __dirname , './nightwatch.browserstack.js' )
37
+ : resolve ( __dirname , './nightwatch.config.js' )
10
38
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 ) {
13
45
// take every remaining argument and treat it as a test file
14
46
// this allows to run `node test/e2e/runner.js test/e2e/basic.js`
15
47
argv . test = argv [ '_' ] . slice ( 0 )
16
48
17
- // add a configuration by default if not provided
18
49
if ( argv . c === DEFAULT_CONFIG && argv . config === DEFAULT_CONFIG ) {
19
- argv . config = resolve ( __dirname , './nightwatch.config.js' )
50
+ argv . config = argv . c = NW_CONFIG
20
51
}
21
52
// Nightwatch does not accept an array with one element
22
53
if ( argv . test . length === 1 ) argv . test = argv . test [ 0 ]
23
54
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 ) {
39
100
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