Merge "Parser: Add Title type hints"
[lhc/web/wiklou.git] / tests / selenium / wdio.conf.js
1 const fs = require( 'fs' ),
2 path = require( 'path' ),
3 startChromedriver = !process.argv.includes( '--skip-chromedriver' ),
4 logPath = process.env.LOG_DIR || path.join( __dirname, '/log' );
5
6 let ffmpeg;
7
8 // get current test title and clean it, to use it as file name
9 function fileName( title ) {
10 return encodeURIComponent( title.replace( /\s+/g, '-' ) );
11 }
12
13 // build file path
14 function filePath( test, screenshotPath, extension ) {
15 return path.join( screenshotPath, `${fileName( test.parent )}-${fileName( test.title )}.${extension}` );
16 }
17
18 /**
19 * For more details documentation and available options,
20 * see <https://webdriver.io/docs/configurationfile.html>
21 * and <https://webdriver.io/docs/options.html>.
22 */
23 exports.config = {
24 // ======
25 // Custom conf keys for MediaWiki
26 //
27 // Access via `browser.config.<key>`.
28 // Defaults are for MediaWiki-Vagrant
29 // ======
30 mwUser: process.env.MEDIAWIKI_USER || 'Admin',
31 mwPwd: process.env.MEDIAWIKI_PASSWORD || 'vagrant',
32
33 // ==================
34 // Runner Configuration
35 // ==================
36 runner: 'local',
37 // The standalone chromedriver (also used by WMF CI) uses "/wd/hub".
38 // The one provided by wdio uses "/".
39 path: startChromedriver ? '/' : '/wd/hub',
40
41 // ======
42 // Sauce Labs
43 // ======
44 // See http://webdriver.io/guide/services/sauce.html
45 // and https://github.com/bermi/sauce-connect-launcher#advanced-usage
46 user: process.env.SAUCE_USERNAME,
47 key: process.env.SAUCE_ACCESS_KEY,
48 sauceConnect: true,
49
50 // ==================
51 // Test Files
52 // ==================
53 specs: [
54 './tests/selenium/wdio-mediawiki/specs/*.js',
55 './tests/selenium/specs/**/*.js'
56 ],
57
58 // ============
59 // Capabilities
60 // Define the different browser configurations to use ("capabilities") here.
61 // ============
62 maxInstances: 1,
63 capabilities: [ {
64 // For Chrome/Chromium https://sites.google.com/a/chromium.org/chromedriver/capabilities
65 browserName: 'chrome',
66 'goog:chromeOptions': {
67 // If DISPLAY is set, assume developer asked non-headless or CI with Xvfb.
68 // Otherwise, use --headless.
69 args: [
70 ...( process.env.DISPLAY ? [] : [ '--headless' ] ),
71 // Chrome sandbox does not work in Docker
72 ...( fs.existsSync( '/.dockerenv' ) ? [ '--no-sandbox' ] : [] )
73 ]
74 }
75 } ],
76
77 // ===================
78 // Test Configurations
79 // Define all options that are relevant for the WebdriverIO instance here
80 // ===================
81 // Level of logging verbosity: trace | debug | info | warn | error | silent
82 logLevel: 'error',
83 // Stop after this many failures, or 0 to run all tests before reporting failures.
84 bail: 0,
85 // Base for browser.url() and wdio-mediawiki/Page#openTitle()
86 baseUrl: ( process.env.MW_SERVER || 'http://127.0.0.1:8080' ) + (
87 process.env.MW_SCRIPT_PATH || '/w'
88 ),
89 services: [
90 ...( startChromedriver ? [ 'chromedriver' ] : [] ),
91 ...( process.env.SAUCE_ACCESS_KEY ? [ 'sauce' ] : [] )
92 ],
93 // See also: https://webdriver.io/docs/frameworks.html
94 framework: 'mocha',
95 // See also: https://webdriver.io/docs/dot-reporter.html
96 reporters: [
97 'dot',
98 // See also: https://webdriver.io/docs/junit-reporter.html#configuration
99 [ 'junit', {
100 outputDir: logPath
101 } ]
102 ],
103 // See also: http://mochajs.org/
104 mochaOpts: {
105 ui: 'bdd',
106 timeout: 60 * 1000
107 },
108
109 // =====
110 // Hooks
111 // =====
112 /**
113 * Executed before a Mocha test starts.
114 * @param {Object} test Mocha Test object
115 */
116 beforeTest: function ( test ) {
117 if ( process.env.DISPLAY && process.env.DISPLAY.startsWith( ':' ) ) {
118 const videoPath = filePath( test, logPath, 'mp4' );
119 const { spawn } = require( 'child_process' );
120 ffmpeg = spawn( 'ffmpeg', [
121 '-f', 'x11grab', // grab the X11 display
122 '-video_size', '1280x1024', // video size
123 '-i', process.env.DISPLAY, // input file url
124 '-loglevel', 'error', // log only errors
125 '-y', // overwrite output files without asking
126 '-pix_fmt', 'yuv420p', // QuickTime Player support, "Use -pix_fmt yuv420p for compatibility with outdated media players"
127 videoPath // output file
128 ] );
129
130 const logBuffer = function ( buffer, prefix ) {
131 const lines = buffer.toString().trim().split( '\n' );
132 lines.forEach( function ( line ) {
133 console.log( prefix + line );
134 } );
135 };
136
137 ffmpeg.stdout.on( 'data', ( data ) => {
138 logBuffer( data, 'ffmpeg stdout: ' );
139 } );
140
141 ffmpeg.stderr.on( 'data', ( data ) => {
142 logBuffer( data, 'ffmpeg stderr: ' );
143 } );
144
145 ffmpeg.on( 'close', ( code, signal ) => {
146 console.log( '\n\tVideo location:', videoPath, '\n' );
147 if ( code !== null ) {
148 console.log( `\tffmpeg exited with code ${code} ${videoPath}` );
149 }
150 if ( signal !== null ) {
151 console.log( `\tffmpeg received signal ${signal} ${videoPath}` );
152 }
153 } );
154 }
155 },
156 /**
157 * Executed after a Mocha test ends.
158 * @param {Object} test Mocha Test object
159 */
160 afterTest: function ( test ) {
161 if ( ffmpeg ) {
162 // stop video recording
163 ffmpeg.kill( 'SIGINT' );
164 }
165
166 // if test passed, ignore, else take and save screenshot
167 if ( test.passed ) {
168 return;
169 }
170 // save screenshot
171 const screenshotfile = filePath( test, logPath, 'png' );
172 browser.saveScreenshot( screenshotfile );
173 console.log( '\n\tScreenshot location:', screenshotfile, '\n' );
174 }
175 };