2

I would like to include a module/library in a protractor test case, however, as soon as I add the line

var lib = require('./loginactions.js');

All the references to protractor and related objects are lost. In other words, if I don't have the require line, the 'protractor' and 'browser' variables are found and test runs fine (using the functions within the file), but after adding that line the variables are not found any more.

Here is a minimal test case:

 var lib = require('./loginactions.js'); //problematic line
 describe('Login / Logout to Application',function(){
    var ptor;

    beforeEach(function(){
        ptor = protractor.getInstance(); //protractor reference lost
        browser.get('http://localhost:80'); //browser reference lost
    });

    it('should login and then logout successfully', function(){
       //Do things here
       lib.login(user, pass, ptor);
    });
});

I export the functions in this way:

module.exports.Login = Login;
module.exports.Logout = Logout;

//Params: ptor - protractor instance
function Login(user, pass, ptor)
{
    //stuff
}

function Logout(ptor)
{
    //stuff
}

I also wonder, is this even the correct way of including the own libraries into the project. So my question is, how to properly include libraries into a protractor test case?

Esch
  • 79
  • 1
  • 5
  • is ./loginactions.js in the same directory as your test file? – Andres D Mar 12 '14 at 16:13
  • Yes, it is in the same directory. – Esch Mar 13 '14 at 11:07
  • Should'nt you omit the extension `.js`: `var lib = require('./loginactions');`? It should work without. – glepretre Mar 14 '14 at 08:20
  • I did try omitting the .js extension and require works fine. The problem is, as soon as I want to require an external module/lib, the reference to protractor/browser (iirc this is part of protractor lib?) is lost, so I cannot do anything with protractor. – Esch Mar 14 '14 at 10:10

1 Answers1

0

To answer my own question, using protractor as a library method worked, this way the references to protractor were restored. So adding these two requires solved my issue:

var protractor = require('/path/to/protractor');
require('/path/to/protractor/jasminewd');

So my test looked similar to the updated code in 'no method expect' when using Protractor as a library question.

However, I am not entirely sure about the global browser object. It is a wrapper around the WebDriver object according to http://www.ng-newsletter.com/posts/practical-protractor.html, but so is protractor instance. So I decided to replace all 'browser' variables with 'ptor' and so far no complaints. This may backfire, but as I said, I'm not entirely sure whether the global browser object that is created along with the global protractor object, when running protractor normally and not as library.

Community
  • 1
  • 1
Esch
  • 79
  • 1
  • 5