Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular e2e testing with login non-Angular

I have an angular app working with a non angular login page. The angular app is loaded only if user is logged in.

What I've done:

I have e2e-tested my non-angular login page with protractor&jasmine by following this post, running my test-code located in e2e/login.spec.js and using browser.ignoreSynchronization = true to prevent that there is no angular loaded.

What I want to be done:

I would like now to run some test against my angular app but I can't because the backend redirect to the login page. That's means I need to log a testingUser for each angular view test. Not really effective if there is a lot of test.

I know there is a solution when the login page is in the angular side by mocking my backend API (see same post) but can't work with what I have.

Question:

Is there a way to keep the user logged in? What is it? Or any other work around? Do I need to implement a dev login page inside the angular app just for testing?

like image 413
louis amoros Avatar asked Apr 03 '15 22:04

louis amoros


2 Answers

In my config file I have an onPrepare object with lots of variables. this includes a login function, as so:

global.loginFn = function loginFn() {

  usernameInput.sendKeys(username);
  passwordInput.sendKeys(password);
  login.click();
};

you can add the browser.ignoreSynchronization if needed, as so:

global.loginFn = function loginFn() {
  browser.ignoreSynchronization = true;
  usernameInput.sendKeys(username);
  passwordInput.sendKeys(password);
  login.click();
};

then in my spec.js files I can do this:

beforeAll(function () {
 browser.get('myUrl');
 loginFn();
};

in your it blocks you can then add the

browser.ignoreSynchronization = false;

so it knows to wait for angular again. you could even try putting

beforeAll(function () {
 browser.get('myUrl');
 loginFn();
 browser.ignoreSynchronization = false;
};

so you don't have to repeat yourself, I haven't personally had to do it that way, but it should work.

this way any tests you need to run in that spec file, the user will be logged in, and it doesn't have to log them in before each test, just before all the tests in that specific test suite.

or if all your tests need the user logged in, you can just log the user in using the onPrepare, instead of just creating a function to be called at a specific time.

like image 88
jsuser Avatar answered Sep 28 '22 11:09

jsuser


I have exactly the same use case, there several possible ways which I went through:

  1. log in every time using beforeEach() - it is not really productive as you noticed, especially when you have lot of test cases
  2. implement 'fast login' in your application (for example via URL params)
  3. for example you have test suite which has 20 it blocks with separate test cases, you could add login step in the 1st 'it' block. It is not the best way, but it should work. I have about 150 test and execution time is about 4-5 minutes using this approach.
like image 44
Sergey Teplyakov Avatar answered Sep 28 '22 12:09

Sergey Teplyakov