Kishan Patel Kishan Patel - 22 days ago 6
jQuery Question

How to use Javascript namespacing in Protractor automation?

I have 3 files i.e. conf.js , actionwords.js, project_test.js.
Actionwords.js and project_test.js are the files generated from hiptest tool.So i need to use this structure for automating test case. When I run through cmd, I am getting error.

I ran:

protractor conf.js


Message:
Failed: Cannot read property 'theApplicationURL' of undefined

Stack:
TypeError: Cannot read property 'theApplicationURL' of undefined

// conf.js

exports.config = {
framework: 'jasmine2',
directConnect: true,
seleniumAddress: 'http://localhost:4444/wd/hub',
specs:['path to/project_test.js'],
capabilities: { 'browserName': 'chrome' }
};


//actionwords.js

var Actionwords = {
theApplicationURL: function () {
browser.get('localhost');
browser.driver.manage().window().maximize();
browser.sleep(5000);
},
};


//project_test.js

describe('Test', function () {
beforeEach(function () {
this.actionwords = Object.create(Actionwords);
});

it('Login_Test (uid:fe6d6670-a864-4d0f-a867-3faf9f51ff8d)', function () {
// Given the application URL
this.actionwords.theApplicationURL();
});
});


Can anyone Help me in this ?

Answer

Change it this way:

var actionwords = {
  theApplicationURL: function () {
    browser.get('localhost');
    browser.driver.manage().window().maximize();
    browser.sleep(5000);
  },
};

module.exports = actionwords;

test:

var actionwords = require("actionwords.js")

describe('Test', function () {
  it('Login_Test (uid:fe6d6670-a864-4d0f-a867-3faf9f51ff8d)', function () {
    // Given the application URL
    actionwords.theApplicationURL();
  });
});

REACTION ON COMMENT about this:

You can assign it to this scope in beforeEach:

var actionwords = require("actionwords.js")

describe('Test', function () {
  beforeEach(function () {
    this.actionwords = actionwords;
  });
  it('Login_Test (uid:fe6d6670-a864-4d0f-a867-3faf9f51ff8d)', function () {
    // Given the application URL
    this.actionwords.theApplicationURL();
  });
});