Geting“不能读取属性''未定义的”
这可能有一个简单的解决scheme,但我只是没有看到它。 我正在写一个量angular器testing并设置页面对象文件
newstate.js(页面对象文件)
'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;
spec.js文件:
'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'); }) })
conf.js文件:
exports.config = { framework: 'jasmine', specs: ['test-spec.js'], useAllAngular2AppRoots: true, jasmineNodeOpts: { showColors: true } };
当我运行量angular器时,我得到:
$ protractor conf.js Failures: 1) Testing newstate should select a state Message: Failed: Cannot read property 'select' of undefined
它启动浏览器,打开网页,就像我已经调用new newstate('...')
但由于某种原因,它不希望看到我的dd.select函数。 我错过什么或做错了什么? 谢谢。
您使用Object.create的方式不正确。 在你的情况下适当的符号将是:
var newstate = function (url) { browser.get(url); }; newstate.prototype = Object.create({ dd: { select: function(state) { $('select option[value="'+state+'"]').click(); } } });
dd
对象上的select
是未定义的原因是Object.create
的第二个参数是一个属性描述符对象 ,而不仅仅是一个具有像您提供的属性的对象。
然而,在你的情况下,你根本不需要Object.create,因为newstate.prototype.dd = function() { /*...*/ }
就足够了。