Protractor how to run login test script first

All we need is an easy explanation of the problem, so here it is.

I am trying to test an Angular Single Page application with protractor. I need to run the login script first. Only then I can move to other routes since there is a check for token in localStorage on route change.

Is this testing approach correct?. In that case I need to run the login script first. Does protractor allows to control the spec file order.

Or should I run the each script independently by hardcoding the token in localStorage (Should I do login api call independently before each test).

My login script contains the following

it('Login with wrong email', function() {

})

it('Login with correct email', function() {

})

So after running the Login with correct mail I will get the accessToken which will get stored in localStorage and I can continue testing other routes. Is this the correct approach. If not how do I test a single application with login from end to end.

In protractor Style Guide it is mentioned as

Make your tests independent from each other

So should I use beforeAll, beforeEach to get the access token and store in localStorage before each test. In that case please explain me how to do it.

Any help is greatly appreciated.

Thanks.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

As highlighted in the Protractor FAQs, you can specify your login code in the onPrepare section of your conf file. Here is an example.

You can also achieve this in beforeAll but that adds unnecessary overhead to your test scripts. Altering the localStorage is possible, but not in the spirit of e2e testing since a lot can go wrong wrt LS, and you will end up wondering if it is your app or your LS modification code that caused this.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply