selenium: Create local ./log directory if needed
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 2 May 2018 22:47:16 +0000 (23:47 +0100)
committerZfilipin <zfilipin@wikimedia.org>
Wed, 9 May 2018 14:30:55 +0000 (14:30 +0000)
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

.gitignore
tests/selenium/wdio.conf.js

index 0112cf3..d440e72 100644 (file)
@@ -49,6 +49,7 @@ sftp-config.json
 npm-debug.log
 node_modules/
 /tests/phpunit/phpunit.phar
+/tests/selenium/log
 
 # Composer
 /vendor
index 5399fa4..260d188 100644 (file)
@@ -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' );
        }