protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Add beforeRunners/afterRunners hooks for running setup that only needs to happen once per launcher

Open erikat opened this issue 12 years ago • 14 comments

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.

erikat avatar Jun 28 '14 18:06 erikat

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?

juliemr avatar Jun 29 '14 23:06 juliemr

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?

erikat avatar Jul 02 '14 04:07 erikat

Yes, having a before launcher / after launcher function would be reasonable for global setup. Changing the issue name to track that.

juliemr avatar Jul 07 '14 18:07 juliemr

Great! Thanks! That will really help :)

erikat avatar Jul 09 '14 02:07 erikat

Do you have a rough estimate of the timeline of this feature request?

erikat avatar Jul 09 '14 21:07 erikat

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.

juliemr avatar Jul 09 '14 23:07 juliemr

closed with https://github.com/angular/protractor/pull/1408

hankduan avatar Oct 07 '14 18:10 hankduan

Fantastic! Thank you!

erikat avatar Oct 08 '14 14:10 erikat

is there any other way to share parameters among .spec files when shardtestfiles is set to true ?

ProtHelp avatar Dec 15 '15 15:12 ProtHelp

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.

sjelin avatar Dec 15 '15 23:12 sjelin

does that mean if i update a paramerter in one spec file , the updated value will not be available for next spec file ?

ProtHelp avatar Dec 16 '15 05:12 ProtHelp

what parameters are you talking about?

sjelin avatar Dec 17 '15 00:12 sjelin

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 .

ProtHelp avatar Dec 18 '15 09:12 ProtHelp

Protractor doesn't have any special tools for cross process communication, but you could always try something like dnode

sjelin avatar Dec 18 '15 22:12 sjelin