Machtyn
Machtyn

Reputation: 3272

Geting "cannot read property '[method]' of undefined"

This may have a simple solution, but I'm just not seeing it. I'm writing a protractor test and setting a page object file

newstate.js (the page object file)

'use strict';

var newstate = function (url) {
    browser.get(url);
};

newstate.prototype = Object.create({}, {
    dd: { select: function(state) { $('select option[value="'+state+'"]').click(); } }
});

module.exports = newstate;

The spec.js file:

'use strict';

var newstate = require('newstate.js');

describe('Testing newstate', function() {
    var statePage;
    beforeAll(function() {
        statePage = new newstate('http://localhost:8080');
    });

    it('should select a state', function() {
        statePage.dd.select('AK');
    })
})

The conf.js file:

exports.config = {
    framework: 'jasmine',
    specs: ['test-spec.js'],
    useAllAngular2AppRoots: true,
    jasmineNodeOpts: {
        showColors: true
    }
};

When I run protractor, I get:

$ protractor conf.js
Failures:
1) Testing newstate should select a state
  Message:
    Failed: Cannot read property 'select' of undefined

It is launching the browser, opening the webpage, just as it should when I've called new newstate('...') But for some reason it doesn't want to see my dd.select function. What am I missing or doing wrong? Thanks.

Upvotes: 2

Views: 1978

Answers (1)

dfsq
dfsq

Reputation: 193261

The way you are using Object.create is incorrect. Proper notation in your case would be:

var newstate = function (url) {
    browser.get(url);
};

newstate.prototype = Object.create({
    dd: { select: function(state) { $('select option[value="'+state+'"]').click(); } }
});

The reason why select is undefined on the dd object is that the second argument to Object.create is a property descriptor object, not just an object with properties like you provided.

However, in your case you don't really need Object.create at all, as newstate.prototype.dd = function() { /*...*/ } would be enough.

Upvotes: 2

Related Questions