Merge "Replace sorting classes with better naming convention"
[lhc/web/wiklou.git] / tests / selenium / wdio.conf.js
1 const fs = require( 'fs' ),
2 path = require( 'path' ),
3 logPath = process.env.LOG_DIR || path.join( __dirname, '/log' );
4
5 let ffmpeg;
6
7 // get current test title and clean it, to use it as file name
8 function fileName( title ) {
9 return encodeURIComponent( title.replace( /\s+/g, '-' ) );
10 }
11
12 // build file path
13 function filePath( test, screenshotPath, extension ) {
14 return path.join( screenshotPath, `${fileName( test.parent )}-${fileName( test.title )}.${extension}` );
15 }
16
17 // relative path
18 function relPath( foo ) {
19 return path.resolve( __dirname, '../..', foo );
20 }
21
22 exports.config = {
23 // ======
24 // Custom WDIO config specific to MediaWiki
25 // ======
26 // Use in a test as `browser.options.<key>`.
27 // Defaults are for convenience with MediaWiki-Vagrant
28
29 // Wiki admin
30 username: process.env.MEDIAWIKI_USER || 'Admin',
31 password: process.env.MEDIAWIKI_PASSWORD || 'vagrant',
32
33 // Base for browser.url() and Page#openTitle()
34 baseUrl: ( process.env.MW_SERVER || 'http://127.0.0.1:8080' ) + (
35 process.env.MW_SCRIPT_PATH || '/w'
36 ),
37
38 // ======
39 // Sauce Labs
40 // ======
41 // See http://webdriver.io/guide/services/sauce.html
42 // and https://docs.saucelabs.com/reference/platforms-configurator
43 services: [ 'sauce' ],
44 user: process.env.SAUCE_USERNAME,
45 key: process.env.SAUCE_ACCESS_KEY,
46
47 // Default timeout in milliseconds for Selenium Grid requests
48 connectionRetryTimeout: 90 * 1000,
49
50 // Default request retries count
51 connectionRetryCount: 3,
52
53 // ==================
54 // Test Files
55 // ==================
56 specs: [
57 relPath( './tests/selenium/wdio-mediawiki/specs/*.js' ),
58 relPath( './tests/selenium/specs/**/*.js' ),
59 relPath( './extensions/*/tests/selenium/specs/**/*.js' ),
60 relPath( './extensions/VisualEditor/modules/ve-mw/tests/selenium/specs/**/*.js' ),
61 relPath( './extensions/Wikibase/repo/tests/selenium/specs/**/*.js' ),
62 relPath( './skins/*/tests/selenium/specs/**/*.js' )
63 ],
64 // Patterns to exclude
65 exclude: [
66 relPath( './extensions/CirrusSearch/tests/selenium/specs/**/*.js' )
67 ],
68
69 // ============
70 // Capabilities
71 // ============
72
73 // How many instances of the same capability (browser) may be started at the same time.
74 maxInstances: 1,
75
76 capabilities: [ {
77 // For Chrome/Chromium https://sites.google.com/a/chromium.org/chromedriver/capabilities
78 browserName: 'chrome',
79 maxInstances: 1,
80 chromeOptions: {
81 // If DISPLAY is set, assume developer asked non-headless or CI with Xvfb.
82 // Otherwise, use --headless (added in Chrome 59)
83 // https://chromium.googlesource.com/chromium/src/+/59.0.3030.0/headless/README.md
84 args: [
85 ...( process.env.DISPLAY ? [] : [ '--headless' ] ),
86 // Chrome sandbox does not work in Docker
87 ...( fs.existsSync( '/.dockerenv' ) ? [ '--no-sandbox' ] : [] )
88 ]
89 }
90 } ],
91
92 // ===================
93 // Test Configurations
94 // ===================
95
96 // Enabling synchronous mode (via the wdio-sync package), means specs don't have to
97 // use Promise#then() or await for browser commands, such as like `brower.element()`.
98 // Instead, it will automatically pause JavaScript execution until th command finishes.
99 //
100 // For non-browser commands (such as MWBot and other promises), this means you
101 // have to use `browser.call()` to make sure WDIO waits for it before the next
102 // browser command.
103 sync: true,
104
105 // Level of logging verbosity: silent | verbose | command | data | result | error
106 logLevel: 'error',
107
108 // Enables colors for log output.
109 coloredLogs: true,
110
111 // Warns when a deprecated command is used
112 deprecationWarnings: true,
113
114 // Stop the tests once a certain number of failed tests have been recorded.
115 // Default is 0 - don't bail, run all tests.
116 bail: 0,
117
118 // Setting this enables automatic screenshots for when a browser command fails
119 // It is also used by afterTest for capturig failed assertions.
120 // We disable it since we have our screenshot handler in the afterTest hook.
121 screenshotPath: null,
122
123 // Default timeout for each waitFor* command.
124 waitforTimeout: 10 * 1000,
125
126 // Framework you want to run your specs with.
127 // See also: http://webdriver.io/guide/testrunner/frameworks.html
128 framework: 'mocha',
129
130 // Test reporter for stdout.
131 // See also: http://webdriver.io/guide/testrunner/reporters.html
132 reporters: [ 'spec', 'junit' ],
133 reporterOptions: {
134 junit: {
135 outputDir: logPath
136 }
137 },
138
139 // Options to be passed to Mocha.
140 // See the full list at http://mochajs.org/
141 mochaOpts: {
142 ui: 'bdd',
143 timeout: 60 * 1000
144 },
145
146 // =====
147 // Hooks
148 // =====
149 // See also: http://webdriver.io/guide/testrunner/configurationfile.html
150
151 /**
152 * Function to be executed before a test (in Mocha/Jasmine) or a step (in Cucumber) starts.
153 * @param {Object} test test details
154 */
155 beforeTest: function ( test ) {
156 if ( process.env.DISPLAY && process.env.DISPLAY.startsWith( ':' ) ) {
157 var logBuffer;
158 let videoPath = filePath( test, logPath, 'mp4' );
159 const { spawn } = require( 'child_process' );
160 ffmpeg = spawn( 'ffmpeg', [
161 '-f', 'x11grab', // grab the X11 display
162 '-video_size', '1280x1024', // video size
163 '-i', process.env.DISPLAY, // input file url
164 '-loglevel', 'error', // log only errors
165 '-y', // overwrite output files without asking
166 '-pix_fmt', 'yuv420p', // QuickTime Player support, "Use -pix_fmt yuv420p for compatibility with outdated media players"
167 videoPath // output file
168 ] );
169
170 logBuffer = function ( buffer, prefix ) {
171 let lines = buffer.toString().trim().split( '\n' );
172 lines.forEach( function ( line ) {
173 console.log( prefix + line );
174 } );
175 };
176
177 ffmpeg.stdout.on( 'data', ( data ) => {
178 logBuffer( data, 'ffmpeg stdout: ' );
179 } );
180
181 ffmpeg.stderr.on( 'data', ( data ) => {
182 logBuffer( data, 'ffmpeg stderr: ' );
183 } );
184
185 ffmpeg.on( 'close', ( code, signal ) => {
186 console.log( '\n\tVideo location:', videoPath, '\n' );
187 if ( code !== null ) {
188 console.log( `\tffmpeg exited with code ${code} ${videoPath}` );
189 }
190 if ( signal !== null ) {
191 console.log( `\tffmpeg received signal ${signal} ${videoPath}` );
192 }
193 } );
194 }
195 },
196
197 /**
198 * Save a screenshot when test fails.
199 *
200 * @param {Object} test Mocha Test object
201 */
202 afterTest: function ( test ) {
203 if ( ffmpeg ) {
204 // stop video recording
205 ffmpeg.kill( 'SIGINT' );
206 }
207
208 // if test passed, ignore, else take and save screenshot
209 if ( test.passed ) {
210 return;
211 }
212 // save screenshot
213 let screenshotfile = filePath( test, logPath, 'png' );
214 browser.saveScreenshot( screenshotfile );
215 console.log( '\n\tScreenshot location:', screenshotfile, '\n' );
216 }
217 };