Add beforeRunners/afterRunners hooks for running setup that only needs to happen once per launcher
When shardTestFiles is true, everything set in the protractor config file gets sandboxed to the single testfile/thread being run, and methods such as onPrepare and onComplete get called at the beginning of each testfile/thread as opposed to before and after all testfiles/threads are run. This means there’s no way for test files to communicate to each other via global variables initialized in onPrepare, as well as the ability to chain custom properties onto the global browser object and use them across testfiles.
I set up a simple test. Here's the pseudo code:
// in protractor config...
onPrepare: function(){
global.myGlobal = {};
}
onComplete: function() {
console.log(myGlobal);
},
// In foo.js...
myGlobal.foo = “fooValue”;
// In bar.js...
myGlobal.bar = “barValue”;
With shardTestFiles:false, the console outputs
{ foo: fooValue, bar:barValue };
When shardTestFiles:true, the console outputs
PID: 26780 (capability: firefox #1a)
Specs: testing/bar.js
{ bar: 'barValue’ }
[launcher] 1 instance(s) of WebDriver still running
------------------------------------
PID: 26722 (capability: firefox #1b)
Specs: testing/foo.js
{ foo: 'fooValue’ }
Seems like you could have launcher manage the protractor config as opposed to delegating it to the threads.
This is all working as intended - each capability should be run separately, so onPrepare and onComplete should fire once per instance.
Is there documentation that is unclear on this?
Thanks for the quick reply! Was looking at the reference conf linked from the getting started guide. https://github.com/angular/protractor/blob/master/docs/referenceConf.js, particularly:
// A callback function called once tests are finished.
onComplete: function() {
// At this point, tests will be done but global objects will still be
// available.
},
Dug into the code and see that you guys are forking node processes, so yeah, I can see why globals won’t work here. Originally thought the browser threads were delegated under a single node process, but forking makes sense for splitting across multiple vm's.
It would be really useful if we could configure handlers in the config for before and after all tests are run. That way setup and teardown that’s not specific to the test files being run can be fired only once. Thinking that we could handle something like “beforeAllTests()" in the init portion of the launcher, and something like “afterAllTests()” in the launcher right after you call reporter.reportSummary(), because at that point, all processes have exited. Would you guys consider something like that?
Yes, having a before launcher / after launcher function would be reasonable for global setup. Changing the issue name to track that.
Great! Thanks! That will really help :)
Do you have a rough estimate of the timeline of this feature request?
It's not super high priority and we're trying to work on stability stuff before 1.0, so not in the next couple weeks.
closed with https://github.com/angular/protractor/pull/1408
Fantastic! Thank you!
is there any other way to share parameters among .spec files when shardtestfiles is set to true ?
You mean the command line parameters? They should all end up in the config, which (if I recall correctly) should be copied into each process.
does that mean if i update a paramerter in one spec file , the updated value will not be available for next spec file ?
what parameters are you talking about?
let me put it this way , I need a variable to be available across multiple spec files and updates done by one spec file should be available for next spec file ? I have tried using global.variable , protractor.variable and browser.params but nothing seems to be working. Updated value is not available .
Protractor doesn't have any special tools for cross process communication, but you could always try something like dnode