In each spec I have beforeEach
and afterEach
statements. Is it possible to add it somehow globally to avoid code duplication between specs ?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Purpose of
beforeEach()
andafterEach()
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 -describe
), then you can usebeforeAll()
andafterAll()
functions that jasmine provides.onPrepare()
andonComplete()
function.beforeLaunch
andafterLaunch
.So it all depends on the scenario that you want to use them in. Hope it helps.
My team has the same desire, to run bits of boilerplate code at the start of every test file. From the discussion here, it doesn't sound like there are hooks to globally add to the
beforeEach()
,afterEach()
, etc.However, we do use the
onPrepare()
function to abbreviate the amount of before/after boilerplate code that gets repeated in each spec file. Below is abeforeAll()
example, but the pattern could be used forbeforeEach()/afterEach()
. In this case, we're setting up test users in the database with a DataSeeder class, which we do in the outer-mostdescribe()
block in every spec file. (I'm also leaving in mycatchProtractorErrorInLocation
pattern, because it's super useful for us.)In protractor.conf.ts add boilerplate code to
browser.params
object.With that in place, we can easily do
beforeAll()
setup code in an abbreviated set of lines.You obviously need to do different things in your setup, but you can see how the pattern can apply.