0

I have an application on which I want to implement multiple E2E testing scenarios, each specific to a section of the application. The problem is that my application requires a login. I have created a login scenario, it all works fine. For describing a different scenario, I need to be able to reuse the code for the login one. How can I do that?

describe('login page flow', function () {
    it('should open the login page', function () {
        browser().navigateTo('/#/login');
        sleep(1);
        expect(browser().window().hash()).toBe('/login');
    });

    it('should have login elements', function () {
        expect(element('#username').count()).toBe(1);
        expect(element('#password').count()).toBe(1);
    });           

    it('should be able to login successfully', function () {
        input('ui.username').enter('user');
        input('ui.password').enter('pass');

        element('#signin').click();
        sleep(1);
        expect(browser().window().hash()).toBe('/welcome/');
    });
});

The only thing that I could think of was to write this in a beforeEach, but I don't think that's quite a clean solution. Any ideas?

Adrian Marinica
  • 2,191
  • 5
  • 29
  • 53

1 Answers1

0

I think that beforeEach() is the perfect place for that.

Additionally you can create your own page fragment e.g.

function homePage()
{
    return {
        loginForm: {
            email: input("credentials.email"),
            password: input("credentials.password"),
            login: element("#login-form button[type=submit]")
        }
    };
}

and reuse it like this:

      describe("and user logs in", function ()
            {
                describe("successfully", function ()
                {
                    beforeEach(function ()
                    {                        
                        homePage().loginForm.email.enter("username");
                        homePage().loginForm.password.enter("password");
                        homePage().loginForm.login().click();
                    });
      ...

Here you can read about the idea of page fragments.

Piotr Kozlowski
  • 899
  • 1
  • 13
  • 25