0

I have created a test project using page object model. When I run, it creates "Cannot find module" error. I have tried several answers but didn't work.
Here is my code

Config.js

exports.config = 
{
seleniumAddress: 'http://localhost:4444/wd/hub',
//specs: ['spec.js']
specs: ['./testSpec/**/*.js'],
jasmineNodeOpts: {
    defaultTimeoutInterval: 12000
},
onPrepare: function () {
    browser.manage().window().maximize();
    browser.manage().timeouts().implicitlyWait(5000);
}
};            

login.po.js

var loginPageLocator = function()
{
var userName = Element(by.model('vm.username'));
var password = Element(by.model('vm.password'));
var signInButtonText = Element(by.buttonText('Sign in'));

this.get = function()
{
    browser.get('http://20.150.10.160/login');

};

this.typeUserName = function(userName)
{
    this.userName.sendKeys(userName);

};

this.typePassword = function(password)
{
    this.password.sendKeys(password);

};

this.clickSignIn = function()
{
    this.signInButtonText.click();

};

};
module.exports = new loginPageLocator();      

LoginTest.js

var logins = require('./../pageObjects/login.po.js');

describe('login to the system',function()
{
it('login as a dealer',function()
{
    var loginPage = new logins();
    loginPage.typeUserName('test');
    loginPage.typePassword('222');
    loginPage.clickSignIn();

});

});        

Here is the folder structure
folder structure

Package.json
package.json

2
  • Please ensure npm, protractor version. Thanks! Jun 6, 2019 at 11:04
  • @Rohan I have define protractor and jasmine versions in package.json file
    – Syrus
    Jun 6, 2019 at 11:09

3 Answers 3

1

In your LoginTest.js , you have given incorrect reference of page object. change it to:

var logins = require('../../pageObjects/login.po.js');

and then you do not need to create a page object, instead you should directly use the object that you have created earlier like below

logins.typeUserName('test');

update your conf.js file, a sample is below:

    exports.config = {
        directConnect: true,
        capabilities: {
            'browserName': 'chrome'
        },
        framework: 'jasmine',
        baseUrl: "http://url.com",
        specs: ['./*_specs.js'],

        onPrepare: function() {
            browser.driver.manage().window().maximize();
            //create a global variable like below
global.variableName = "hello";
        }
}
9
  • still it gives the same error. If I imported the package/module correctly it should show methods inside it right? But I don't get suggestions
    – Syrus
    Jun 6, 2019 at 11:16
  • Please copy your LoginTest.js under testSpec folder and check. Jun 6, 2019 at 11:22
  • path you mentioned works. Now I can access methods. New error appeared "Error: ReferenceError: Element is not defined"
    – Syrus
    Jun 6, 2019 at 11:33
  • Browser doesnt navigate to login page
    – Syrus
    Jun 6, 2019 at 11:34
  • That's because you haven't called the get method in your test script. call the get method in your it block of LoginTest.js Jun 6, 2019 at 11:39
0

Check this, briefly explained here, and you can fix it https://youtu.be/idJHHxA02l8

1
  • 1
    Please edit your answer to explain why the linked video answers the user's question. Please also indicate that the video is on your channel or you risk your answer being deleted as spam.
    – Kate Paulk
    Feb 21, 2020 at 20:52
0

My issue got resolved when I corrected the module name to match exactly with the names of folders and files. It is case sensitive in Linux systems but not in Windows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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