From: Timo Tijhof Date: Wed, 2 May 2018 22:47:16 +0000 (+0100) Subject: selenium: Create local ./log directory if needed X-Git-Tag: 1.34.0-rc.0~5484 X-Git-Url: http://git.cyclocoop.org/%22.%24h.%22?a=commitdiff_plain;h=e52b056322ec74b41484401f37501014bbfe1dcb;p=lhc%2Fweb%2Fwiklou.git selenium: Create local ./log directory if needed Without this, the tests sometimes fail like this: > Error: ENOENT: no such file or directory, open './log/should-be-creatable.png' > at screenshot() - saveScreenshot.js:52:17 > at saveScreenshot("./log/should-be-creatable.png") This seems to race with the junit plugin, which uses mkdirp to create it if missing, but the screenshot handling is separate from that. WebdriverIO's own screenshot handling also does this so it makes sense for ours to do that, too. I considered trying to re-use WebdriverIO's save mechanism, directly but it's not publicly exposed and only used for the crash scenario, so for now we'll have to keep our own. Also: * Add to gitignore. * Update default to use __dirname instead of './' because the latter will somtimes be mediawiki/ and sometimes be selenium/ depending on whether you run all tests or some tests. * Remove trailing slash from default logPath, and instead add the slash in filePath. Reason: - The LOG_DIR used by Jenkins doesn't end in a slash either (currently not failing because we no longer use that job, and let quibble run the tests instead, which doesn't set the LOG_DIR). - The WDIO docs and example also use screenshotPath without trailing slash. - Without this, setting LOG_DIR=/tmp/something results in filenames like /tmp/somethingexample.png. Bug: T193088 Change-Id: I6550f9315bae89f96a791f7ae8cc2fbec5ee8dd5 --- diff --git a/.gitignore b/.gitignore index 0112cf31a6..d440e728ca 100644 --- a/.gitignore +++ b/.gitignore @@ -49,6 +49,7 @@ sftp-config.json npm-debug.log node_modules/ /tests/phpunit/phpunit.phar +/tests/selenium/log # Composer /vendor diff --git a/tests/selenium/wdio.conf.js b/tests/selenium/wdio.conf.js index 5399fa4938..260d1884d1 100644 --- a/tests/selenium/wdio.conf.js +++ b/tests/selenium/wdio.conf.js @@ -1,6 +1,6 @@ const fs = require( 'fs' ), path = require( 'path' ), - logPath = process.env.LOG_DIR || './log/'; + logPath = process.env.LOG_DIR || __dirname + '/log'; function relPath( foo ) { return path.resolve( __dirname, '../..', foo ); @@ -257,11 +257,16 @@ exports.config = { if ( test.passed ) { return; } - // get current test title and clean it, to use it as file name + // Create sane file name for current test title filename = encodeURIComponent( test.title.replace( /\s+/g, '-' ) ); - // build file path - filePath = this.screenshotPath + filename + '.png'; - // save screenshot + filePath = `${browser.options.screenshotPath}/${filename}.png`; + // Ensure directory exists, based on WebDriverIO#saveScreenshotSync() + try { + fs.statSync( browser.options.screenshotPath ); + } catch ( err ) { + fs.mkdirSync( browser.options.screenshotPath ); + } + // Create and save screenshot browser.saveScreenshot( filePath ); console.log( '\n\tScreenshot location:', filePath, '\n' ); }