Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global beforeEach and afterEach in protractor

In each spec I have beforeEach and afterEach statements. Is it possible to add it somehow globally to avoid code duplication between specs ?

like image 453
Michal Avatar asked Nov 06 '15 11:11

Michal


1 Answers

Purpose of beforeEach() and afterEach() functions are to add a block of repetitive code that you would need to execute every time you start or complete executing each spec(it). There are other ways to add generalised code to avoid code repetition, here are few -

  • If you have a piece of code that you would require to run only once before starting a test suite(describe), then you can use beforeAll() and afterAll() functions that jasmine provides.
  • If you want to run a piece of code that you want to run only once when the execution starts before starting all the test scripts, then add it in your onPrepare() and onComplete() function.
  • If you want to add a piece of code that should run even before protractor has started instantiating itself or after it has shut itself down, then use beforeLaunch and afterLaunch.

So it all depends on the scenario that you want to use them in. Hope it helps.

like image 103
giri-sh Avatar answered Sep 28 '22 05:09

giri-sh