Protractor how to run login test script first

Vishnu Sureshkumar picture Vishnu Sureshkumar · Apr 18, 2016 · Viewed 11.9k times · Source

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.

Answer

TarunG picture TarunG · Apr 20, 2016

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.