Global beforeEach and afterEach in protractor
12,077
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 usebeforeAll()
andafterAll()
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()
andonComplete()
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
andafterLaunch
.
So it all depends on the scenario that you want to use them in. Hope it helps.
Author by
Michal
Updated on June 18, 2022Comments
-
Michal almost 2 years
In each spec I have
beforeEach
andafterEach
statements. Is it possible to add it somehow globally to avoid code duplication between specs ?