webdriverio icon indicating copy to clipboard operation
webdriverio copied to clipboard

Possibility to configure test spec process spawning?

Open timbru31 opened this issue 10 years ago • 41 comments

The problem

According to https://github.com/webdriverio/webdriverio/issues/1106#issuecomment-184728530 each test spec gets executed in a single child process. We have currently 101 individual .feature files (we are using Cucumber) and cannot put them into one big file. When we launch our test script, my Selenium Server is trying to spawn 101 process, this results in a very heavy lag and I need to kill the processes.

Is there a way to configure this behaviour? Do we need to adjust our wdio.conf?

Environment

updated on 07/05/16

  • WebdriverIO version: 4.1.1
  • Node.js version: 4.4.6
  • Selenium Server 2.53.0
  • using wdio testrunner
  • running synchronous or asnychronous tests: does not matter
  • Additional wdio packages used (if applicable): wdio-cucumber-framework: 0.2.0

Details

Even with the cucumber option tags like -cucumberOpts.tags=@foo, it will spawn 101 processes, even though only 1 feature was tagged with @foo (Note: The setup is working fine with wdio 3.4.0)

timbru31 avatar Apr 14 '16 09:04 timbru31

I have the same issue +1 to fix this.

AndrewKeig avatar Apr 14 '16 15:04 AndrewKeig

@timbru31 @AndrewKeig please checkout the maxInstance property. You can apply them globally for all capabilities or a specific one per capability:

https://github.com/webdriverio/webdriverio/blob/master/examples/wdio.conf.js#L52-L60 https://github.com/webdriverio/webdriverio/blob/master/examples/wdio.conf.js#L69-L72

christian-bromann avatar Apr 14 '16 15:04 christian-bromann

hi @christian-bromann, we have this set to maxInstance:1, but regardless of tags set, it will still spawn a process, and open a blank browser window, for each feature file, run 0 tests and close it. It does run the tests tagged once it reaches the feature file tagged.

Even if a feature file is empty it opens an empty browser window and does nothing, then closes it, so it appears that it opens a new browser window regardless of the tests enabled in a feature file.

Hope this makes sense..

AndrewKeig avatar Apr 14 '16 16:04 AndrewKeig

@AndrewKeig sounds like we can improve the cucumber integration here. Can you file an issue in the wdio-cucumber-project with a detailed description so I can reproduce and understand it? Thanks!

christian-bromann avatar Apr 14 '16 16:04 christian-bromann

@AndrewKeig @christian-bromann experiencing the same issue. Can you tag me if you create an issue? (ref https://github.com/webdriverio/wdio-cucumber-framework/issues/8)

timbru31 avatar Apr 14 '16 16:04 timbru31

@christian-bromann I just tested this with mocha and I am getting the same bug. I used fgrep in mochaOpts to run only specific tests and found that a new session is initialised in selenium regardless there are tests to run or not.

After all, it does not seem a test framework specific issue. Is it possible in the framework adapter API to assemble/compile test files files before a selenium session is initiated, based on framework specific criteria in the adapter, e.g cucumber feature tags or mocha grep/fgrep configuration values?

I think this issue could be reopened here too to elaborate on this further?

attila avatar May 11 '16 17:05 attila

I think this issue could be reopened here too to elaborate on this further?

Agree, I think this can't be solved on the framework level because the Selenium session already started if the framework adapter starts working. Any suggestions are welcome. I was thinking it might be the best to have a wdio specific grep / fgrep functionality and ignore the framework ones. Question is how to efficiently check the test files for these.

christian-bromann avatar May 11 '16 18:05 christian-bromann

I think it would be worth thinking about adding another step to the framework adapter api in which adapters should parse and return the required feature/spec files using their own framework-specific logic. Based on that (and maxInstances, etc) webdriverio could determine how many processes it needs to spawn?

When a framework adapter does not implement this method then it could fall back to the default (current) behaviour.

attila avatar May 12 '16 08:05 attila

@attila good idea, hopefully the methods that determine which files to run are somehow publicly available.

christian-bromann avatar May 12 '16 16:05 christian-bromann

I've the same issue and I know how to solve it locally.

My workaround

npm test --suite="login" --grep="oauth"
  1. Parse command line arguments (--grep value)
  2. Get all files in our suite
  3. Parse our test cases like new RegExp(describe\s_(\s_${name}\s*));
  4. Pass all filtered specs to the Launcher
new WebDriverIO.Launcher('conf.js', {
    spec: [FILTERED_FILES] 
});

@christian-bromann, could we pass our specs to the Launcher?

It's really pain for my developers.

monolithed avatar Jul 03 '16 13:07 monolithed

@christian-bromann I've found the culprit. It's in the https://github.com/webdriverio/webdriverio/blob/master/lib/launcher.js#L194 while loop. Since the spec is a big glob it's looped and each time (with the first spec popped off) called again to execute.

Instead it should be called once with all specs, since cucumber handles execution itself with tags and not tags (~@foo tags), shouldn't it?

Any idea how to fix/change this code?

(3.4.0 was https://github.com/webdriverio/webdriverio/blob/1109adab9a5dbd1449fd035b20693d3c68d1d557/lib/launcher.js#L34)

Edit Lines 220-223 could look like this

            this.startInstance(schedulableCaps[0].specs, schedulableCaps[0].caps, schedulableCaps[0].cid)
            schedulableCaps[0].availableInstances--
            schedulableCaps[0].runningInstances++
            schedulableCaps[0].specs = []

(passing all specs to the instance and then make the whole specs empty so the filter with specs.length can be applied)

Edit 2 This took me like an hour or more, but be careful when using process.send with the whole specs object. In my case the length is >100, resulting in a bigger message than IPC can handle (see e.g. cat /proc/sys/kernel/msgmax (http://www.linuxinsight.com/proc_sys_kernel_msgmax.html). (possible idea: http://stackoverflow.com/q/24582213/1902598) (Talking about this line https://github.com/webdriverio/webdriverio/blob/master/lib/launcher.js#L291)

timbru31 avatar Jul 05 '16 20:07 timbru31

If someone wants to know how to implement a custom --grep option:

helpers.js

let path = require('path'),
    fs = require('fs');

let merge = require('deepmerge'),
    glob = require('glob');

module.exports = {
    /**
     * @returns {string|undefined}
     */
    options () {
        for (let option of process.argv) {  
            let [key, value] = option.split('=');

            if (key === name) {
                return value;
            }
        }
    },

    /**
     * { <suite>: [ <files> ] }
     *
     * @param {string} directory
     * @param {string} additional
     * @returns {Object}
     */
    suites (directory, additional = {}) {
        let files = fs.readdirSync(directory),
            cases = {};

        let grep = this.options('--grep');

        files.forEach(suite => {
            let files = path.join(directory, suite, '**/*.js'),
                debug = false;

            if (process.env.NODE_DEBUG) {
                debug = true;
            }

            files = glob.sync(files, {
                cache: true,
                debug
            });

            cases[suite] = files.filter(file => {
                if (grep) {
                    let match = new RegExp(grep);

                    if (grep && match.test(file)) {
                        return file;
                    }
                } else {
                    return file;
                }
            });
        });

        return this.extend(cases, additional);
    }

    /**
     * @param {Object} x
     * @param {Object} y
     * @returns {Object}
     */
    extend (x, y) {
        return merge(...arguments);
    }
}

wdio.config.js

let helpers = require('./helpers');

exports.config = {
    suites: helpers.suites('tests')
}

tests

tests 
     foo
         test_1.js
         test_2.js

package.json

{
    "scripts": {
        "test"   : "wdio wdio.config.js",
    },

    "dependencies": {
        "deepmerge"           : "^0.2.10",
        "glob"                : "^7.0.5",
        "webdriverio"         : "^4.1.1",
        "wdio-spec-reporter"  : "^0.0.1",
        "wdio-mocha-framework": "^0.3.4",
    }
}

shell

npm test -- --suite=foo --grep=test_1*;

monolithed avatar Jul 06 '16 22:07 monolithed

Is this fix going to be put into the main codebase?

wirsingj avatar Jul 18 '16 15:07 wirsingj

@wirsingj maybe. I was hoping to use the frameworks internals to do that. Haven't investigated that though.

christian-bromann avatar Jul 18 '16 15:07 christian-bromann

@christian-bromann Cool, thanks. I've found this issue also messes with wdio-cucumber-framework's debug support.. it opens a debug port for each of the child processes. :\

wirsingj avatar Jul 18 '16 17:07 wirsingj

I've been having the same problem with multiple browsers opening up. Is everyone still using @monolithed's work around?

ghost avatar Aug 18 '16 14:08 ghost

Ive just been specifying the feature file (using cucumber) for now. We've been waiting to see if this fix makes it into a release before using the more creative solution :)

wirsingj avatar Aug 18 '16 14:08 wirsingj

Thanks @wirsingj I take it when you say you're using cucumber you're not using the wdio test runner? and running wdio in async mode?

ghost avatar Aug 18 '16 22:08 ghost

@cheapaschips We are using the wdio-cucumber test runner in async mode.

wirsingj avatar Aug 20 '16 23:08 wirsingj

any progress on this issue?

si-mikey avatar Sep 19 '16 20:09 si-mikey