Is there a before() function in Protractor?

4.1k views Asked by At

I am aware there is a beforeEach() function which I am currently using.

However, I would like to be able to run a before function for all my parent level 'describes' and not the 'it' functions within them.

For example my test looks like this (with an x in front of the functions beforeEach executes):

    beforeEach: login/out stuffs
x   describe: create first case
x       it: make selections
x       it: add details
x       it: change confidentiality settings
x       it: etc
x   describe: create second case
x       it: make selections
x       it: add details
x       it: change confidentiality settings
x       it: etc

Now below is how I want my test to look (with an x in front of the functions I WANT beforeEach to execute):

beforeEach: login/out stuffs
x   describe: create first case
        it: make selections
        it: add details
        it: change confidentiality settings
        it: etc
x   describe: create second case
        it: make selections
        it: add details
        it: change confidentiality settings
        it: etc

Please suggest some solutions.

3

There are 3 answers

0
Dylface On BEST ANSWER

My solution to this problem was to split the two describe blocks in to 2 separate spec files. I figured this made more sense anyway, as each test had different conditions it needed to meet, and it isn't much hassle to have extra spec files.

1
Richard Szalay On

As I understand it, it is an intention design choice that tests are run from scratch for each it. Otherwise, each it could make state changes that would cause a sibling to fail.

1
Andres D On

The name of the function you are looking for is beforeAll(). You can add a before all inside each describe. Make sure that you are using Jasmine 2.