Protractor - Listen to user navigation

I'm currently testing end-to-end functionality for an Angular 2+ project using Protractor.

My issue comes when the user have to login with an external provider. In the APP there is a button that redirects the user to a external login and if the user logged in successfully the provider returns the user to the APP with a token and angular handles the session.

Example:

  1. User in http://example.com clicks on "Log in" button.
  2. The App redirects the user to http://authproviderexample.com/login.
  3. User has to login with user and password.
  4. If correct, the auth provider redirects the user to the APP with a session token http://example.com/login?token=ABCDEFG123456789
  5. The App handles the token so the user is logged in.

I want to skip that external login and "mock" it so I can check if my App redirects to the external and handles the token properly. This way my App tests are independent from the external login provider.

In some way I want to intercept the navigation so I can tell Angular: If the user is being redirected to "http://authproviderexample.com/login" instead redirect him to "http://example.com/login?token=ABCDEFG123456789". I will store a permanent token so I can login without lying in the external service.

Upvotes: 1

Views: 109

Answers (1)

Sergey Pleshakov
Sergey Pleshakov

Reputation: 8948

why don't you do something simple like this

async function login(username, password) {
    await loginButton.click();
    if (await browser.getCurrentUrl() === "http://authproviderexample.com/login") {
        await browser.get("http://example.com/login?token=ABCDEFG123456789")
        return 
    } else {
        // else logic
    }
}

Upvotes: 2

Related Questions