Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

We have several Protractor end to end tests for our AngularJS app in several JS files and they work great. But, there is a lot of duplicated code throughout the tests and we would like to DRY that up.

For instance, every time we log in, we have to click on the text elements, type in the username and password, and then click enter. And right now every single JS file has its own copy of the login function which is called before every test.

It would be nice to refactor those out into modules that we can then import. I have been searching for hours, but not found a good solution.

How should we do this?

share|improve this question
up vote 12 down vote accepted

You can create nodejs modules and include them in protractor configuration

login-helpers.js

exports.loginToPage = function () {
    //nodejs code to login
};

protractor.conf.js

exports.config = {
    //...
    onPrepare: function () {
        protractor.loginHelpers = require('./helpers/login-helpers.js');
    }
    //...
};

page.spec.js

it('should do smth', () => {
    protractor.loginHelpers.loginToPage()

    //expect(...).toBe(...);
});
share|improve this answer
3  
Are you sure login-helpers shouldn't be module.exports.loginToPage = function... – Mark0978 Aug 24 '14 at 12:40
    
Can you let us know why you bind the loginToPage function onto exports? – Blaise Jan 29 '15 at 21:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.