我的规格是这样的:
describe('test', function() {
function CreateApple(type,color, width){
this.type= type
this.color=color;
this.width=width;
}
CreateApple.prototype.eat= function(){
return console.log("eat mniam, mniam");
}
CreateApple.prototype.height = 55;
CreateApple.prototype.xxx={
module1: 1,
hhh: 2,
tablice:{
tab1: function(num){return num},
tab2: 44,
}
}
it('test1', function() {
var apple1 = new CreateApple("goodone", "red", 34);
apple1.eat();
console.log(apple1.type);
console.log(apple1.height);
var ttt= apple1.xxx.hhh;
var uuu= apple1.xxx.tablice.tab1(4);
console.log(ttt+" "+uuu);
});
});
当我使用conf.js运行它时,一切都很好。现在我想使用Page Object,只留下我的spec.js' test1'这意味着构造函数' CreateApple'它的原型应该转到另一个文件。任何人都可以告诉我我的新页面对象'文件应该看起来像?我有类似下面的东西,但我不工作:
spec.js
require('../jgh/page4.js');
describe('test', function() {
it('test1', function() {
var apple1 = new CreateApple("goodone", "red", 34);
apple1.eat();
console.log(apple1.type);
console.log(apple1.height);
var ttt= apple1.xxx.hhh;
var uuu= apple1.xxx.tablice.tab1(4);
console.log(ttt+" "+uuu);
});
});
和我的page4.js
modules.exports =function CreateApple(type,color, width){
this.type= type
this.color=color;
this.width=width;
}
CreateApple.prototype.eat= function(){
return console.log("eat mniam, mniam");
}
CreateApple.prototype.height = 55;
CreateApple.prototype.xxx={
module1: 1,
hhh: 2,
tablice:{
tab1: function(num){return num},
tab2: 44,
}
}
我明白了:
Error: ReferenceError: CreateApple is not defined
答案 0 :(得分:2)
var CreateApple = require('../jgh/page4.js');
describe('test', function() {
it('test1', function() {
var apple1 = new CreateApple("goodone", "red", 34);
apple1.eat();
console.log(apple1.type);
console.log(apple1.height);
var ttt = apple1.xxx.hhh;
var uuu = apple1.xxx.tablice.tab1(4);
console.log(ttt + " " + uuu);
});
});
//------------------------------------------
function CreateApple(type, color, width) {
this.type = type
this.color = color;
this.width = width;
}
CreateApple.prototype.eat = function() {
return console.log("eat mniam, mniam");
}
CreateApple.prototype.height = 55;
CreateApple.prototype.xxx = {
module1: 1,
hhh: 2,
tablice: {
tab1: function(num) { return num },
tab2: 44,
}
}
modules.exports = CreateApple;