1
votes

I have done what is mentioned in this SO question to include a file into my protractor test: How to reuse code in Protractor / AngularJS Testing. The problem is that methods called from inside this included code have no effect on the browser window that I see.

I created a login method in a separate file and included it, but when I call the method from my protractor test the open browser just sits there for a few seconds then continues on as if I had not called anything. I can verify that the method is getting called with a console.log. Below are my code files.

login.js, the included login function:

exports.login = function ()
{
    console.log("Login method called");
    it('logs in', function () {
        browser.get('http://my-url.com/login/');

        // log in to continue with checkout
        element(by.xpath('//*[@id="login-form"]/div[1]/input')).sendKeys("username");
        element(by.xpath('//*[@id="login-form"]/div[2]/input')).sendKeys("password");
        element(by.xpath('//*[@id="login-form"]/div[3]/button')).click();

        // login functions on timeout
        browser.driver.sleep(1000);
    });
}

conf.js, the protractor config file:

exports.config = {
    seleniumAddress: 'http://localhost:4444/wd/hub',
    specs: [/*'specs/overview.js',*/ 'specs/shopping-cart-state.js', 'specs/checkout-state.js', 'specs/buy-item.js'],
    onPrepare: function () {
        var SpecReporter = require('jasmine-spec-reporter');
        // add jasmine spec reporter
        jasmine.getEnv().addReporter(new SpecReporter({ displayStacktrace: 'all' }));
        protractor.login = require('./login.js');
    },
    jasmineNodeOpts: {
        print: function () { },
        defaultTimeoutInterval: 5000000
    }
};

spec.js, the test spec:

describe('Log in with an included function', function () {

    it('logs in', function () {    

        protractor.login.login();

        browser.driver.sleep(5000);
    });
});

Even if I pass the browser object as a parameter to the login function, still nothing happens and I get no error messages. Can anybody tell me what I'm doing wrong?

1

1 Answers

0
votes

Don't wrap your logic inside it() in the "login" module:

exports.login = function ()
{
    this.login = function () {
        console.log("Login method called");

        browser.get('http://my-url.com/login/');

        // log in to continue with checkout
        element(by.xpath('//*[@id="login-form"]/div[1]/input')).sendKeys("username");
        element(by.xpath('//*[@id="login-form"]/div[2]/input')).sendKeys("password");
        element(by.xpath('//*[@id="login-form"]/div[3]/button')).click();

        // login functions on timeout
        browser.driver.sleep(1000);
    };
}