gpt4 book ai didi

javascript - 获取 "cannot read property ' [方法 ]' of undefined"

转载 作者:行者123 更新时间:2023-11-29 21:28:19 25 4
gpt4 key购买 nike

这可能有一个简单的解决方案,但我只是没有看到。我正在编写一个 Protractor 测试并设置一个页面对象文件

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
}
};

当我运行 Protractor 时,我得到:

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

它正在启动浏览器,打开网页,就像我调用 new newstate('...') 时一样,但由于某种原因它不想看到我的dd.选择功能。我错过了什么或做错了什么?谢谢。

最佳答案

您使用的方式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 的第二个参数是 property descriptor object ,而不仅仅是具有您提供的属性的对象。

但是,在您的情况下,您根本不需要 Object.create,因为 newstate.prototype.dd = function() {/*...*/} 就足够了。

关于javascript - 获取 "cannot read property ' [方法 ]' of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36948053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com