Merge "Do not output wikitext in maintenance script"
[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://github.com/bermi/sauce-connect-launcher#advanced-usage
43 services: process.env.SAUCE_ACCESS_KEY ? [ 'sauce' ] : [],
44
45 // ==================
46 // Test Files
47 // ==================
48 specs: [
49 relPath( './tests/selenium/wdio-mediawiki/specs/*.js' ),
50 relPath( './tests/selenium/specs/**/*.js' )
51 ],
52
53 // ============
54 // Capabilities
55 // ============
56
57 // How many instances of the same capability (browser) may be started at the same time.
58 maxInstances: 1,
59
60 capabilities: [ {
61 // For Chrome/Chromium https://sites.google.com/a/chromium.org/chromedriver/capabilities
62 browserName: 'chrome',
63 maxInstances: 1,
64 chromeOptions: {
65 // If DISPLAY is set, assume developer asked non-headless or CI with Xvfb.
66 // Otherwise, use --headless (added in Chrome 59)
67 // https://chromium.googlesource.com/chromium/src/+/59.0.3030.0/headless/README.md
68 args: [
69 ...( process.env.DISPLAY ? [] : [ '--headless' ] ),
70 // Chrome sandbox does not work in Docker
71 ...( fs.existsSync( '/.dockerenv' ) ? [ '--no-sandbox' ] : [] )
72 ]
73 }
74 } ],
75
76 // ===================
77 // Test Configurations
78 // ===================
79
80 // Enabling synchronous mode (via the wdio-sync package), means specs don't have to
81 // use Promise#then() or await for browser commands, such as like `brower.element()`.
82 // Instead, it will automatically pause JavaScript execution until th command finishes.
83 //
84 // For non-browser commands (such as MWBot and other promises), this means you
85 // have to use `browser.call()` to make sure WDIO waits for it before the next
86 // browser command.
87 sync: true,
88
89 // Level of logging verbosity: silent | verbose | command | data | result | error
90 logLevel: 'error',
91
92 // Enables colors for log output.
93 coloredLogs: true,
94
95 // Warns when a deprecated command is used
96 deprecationWarnings: true,
97
98 // Stop the tests once a certain number of failed tests have been recorded.
99 // Default is 0 - don't bail, run all tests.
100 bail: 0,
101
102 // Setting this enables automatic screenshots for when a browser command fails
103 // It is also used by afterTest for capturig failed assertions.
104 // We disable it since we have our screenshot handler in the afterTest hook.
105 screenshotPath: null,
106
107 // Default timeout for each waitFor* command.
108 waitforTimeout: 10 * 1000,
109
110 // Framework you want to run your specs with.
111 // See also: http://webdriver.io/guide/testrunner/frameworks.html
112 framework: 'mocha',
113
114 // Test reporter for stdout.
115 // See also: http://webdriver.io/guide/testrunner/reporters.html
116 reporters: [ 'dot', 'junit' ],
117 reporterOptions: {
118 junit: {
119 outputDir: logPath
120 }
121 },
122
123 // Options to be passed to Mocha.
124 // See the full list at http://mochajs.org/
125 mochaOpts: {
126 ui: 'bdd',
127 timeout: 60 * 1000
128 },
129
130 // =====
131 // Hooks
132 // =====
133 // See also: http://webdriver.io/guide/testrunner/configurationfile.html
134
135 /**
136 * Function to be executed before a test (in Mocha/Jasmine) or a step (in Cucumber) starts.
137 * @param {Object} test test details
138 */
139 beforeTest: function ( test ) {
140 if ( process.env.DISPLAY && process.env.DISPLAY.startsWith( ':' ) ) {
141 var logBuffer;
142 const videoPath = filePath( test, logPath, 'mp4' );
143 const { spawn } = require( 'child_process' );
144 ffmpeg = spawn( 'ffmpeg', [
145 '-f', 'x11grab', // grab the X11 display
146 '-video_size', '1280x1024', // video size
147 '-i', process.env.DISPLAY, // input file url
148 '-loglevel', 'error', // log only errors
149 '-y', // overwrite output files without asking
150 '-pix_fmt', 'yuv420p', // QuickTime Player support, "Use -pix_fmt yuv420p for compatibility with outdated media players"
151 videoPath // output file
152 ] );
153
154 logBuffer = function ( buffer, prefix ) {
155 const lines = buffer.toString().trim().split( '\n' );
156 lines.forEach( function ( line ) {
157 console.log( prefix + line );
158 } );
159 };
160
161 ffmpeg.stdout.on( 'data', ( data ) => {
162 logBuffer( data, 'ffmpeg stdout: ' );
163 } );
164
165 ffmpeg.stderr.on( 'data', ( data ) => {
166 logBuffer( data, 'ffmpeg stderr: ' );
167 } );
168
169 ffmpeg.on( 'close', ( code, signal ) => {
170 console.log( '\n\tVideo location:', videoPath, '\n' );
171 if ( code !== null ) {
172 console.log( `\tffmpeg exited with code ${code} ${videoPath}` );
173 }
174 if ( signal !== null ) {
175 console.log( `\tffmpeg received signal ${signal} ${videoPath}` );
176 }
177 } );
178 }
179 },
180
181 /**
182 * Save a screenshot when test fails.
183 *
184 * @param {Object} test Mocha Test object
185 */
186 afterTest: function ( test ) {
187 if ( ffmpeg ) {
188 // stop video recording
189 ffmpeg.kill( 'SIGINT' );
190 }
191
192 // if test passed, ignore, else take and save screenshot
193 if ( test.passed ) {
194 return;
195 }
196 // save screenshot
197 const screenshotfile = filePath( test, logPath, 'png' );
198 browser.saveScreenshot( screenshotfile );
199 console.log( '\n\tScreenshot location:', screenshotfile, '\n' );
200 }
201 };