Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I divide my tests into separate specs and then call them from another or is it better to use helper functions?

Tags:

protractor

Just got started with Protractor for E2E testing and I am having a bit of trouble with the test case structure.

Not sure if can I divide my tests into separate specs and then call them from another or how can I make nice helper functions to handle this.

I am finding elements by a repeater and then I would like to make tests for each of the operation for each of the element in the repeater. Sort of like this:

describe('tasty', function () {
    'use strict';
    var ptor;

    beforeEach(function () {
        ptor = protractor.getInstance();
        ptor.get('http://localhost:8000/');
    });

    it('Should sample three tasty fruits of every kind on my shopping list.', function () {
        ptor.findElement(protractor.By.className('fruitstore')).click();
        var fruitshelves = ptor.findElements(protractor.By.repeater('fruit in fruits').column('header'));

        fruitshelves.then(function(arr) {
            for (var i=0;i<arr.length; i++) { 
                // Pick up three fruits of this kind from the shelf and put in shopping cart
                // Should be listed on my shopping list 
                // Open the wallet
                // Should have money
                // Pay for the fruits and put it in your shopping bag
                // Should be able to complete the transaction

                // For each one of the fruits in your shopping bag
                // Take a bite
                // Should be tasty
            }
        });
    });
});
like image 255
Annie Avatar asked Nov 15 '13 09:11

Annie


2 Answers

Based on the @langliman answer, I've managed to achieve the desired behaviour.

Note login.spec.js and Login.page.js should be located in the same folder.

Login.page.js file:

var LoginPage = function (ptor) {
    //following PageObject pattern define the functions here.
}

module.exports.getLoginPage = function (ptor) {
    return new LoginPage(ptor);
};

login.spec.js file:

(function () {
    'use strict';

  describe('login page', function () {

        var ptor = protractor.getInstance();
        var loginPageBuilder = require('./Login.page.js');
        var loginPage = loginPageBuilder.getLoginPage(ptor);

        it('should login as admin', function () {
            loginPage.visit();
            loginPage.enterUsername('user');
            loginPage.enterPassword('password');
            loginPage.login();
        });
  });

}());
like image 111
WeMakeSoftware Avatar answered Sep 27 '22 17:09

WeMakeSoftware


I came to this question looking for a way to have helper functions shared between spec files in Protractor. In case others are looking for the same, turns out since Protractor is just running in Node, all you need to do is var helpers = require('./your-helper-file').

like image 38
iangilman Avatar answered Sep 27 '22 15:09

iangilman