2016-01-20 74 views
1

嗨,我想使用POM的概念,第一个文件basefile.JS我的代码是量角器中使用JavaScript原型(继承)的概念,用茉莉

var basefile = function() { 
}; 
basefile.prototype.elementClick = function(locator) { 
element(by.xpath(locator)).click(); 
}; 
//module.exports = new basefile(); 

此文件包含用户可以在任何执行的常见方法Web应用程序。 二js文件tempPageOne.js我的代码是

require("./basefile.js"); 

var tempPageOne = function(){ 

var BaseMethod = new basefile(); 
this.ddselection = function(locOne,locTwo){ 
    BaseMethod.elementClick(locOne); 
    BaseMethod.elementClick(locTwo); 
}; 
}; 
module.exports = new tempPageOne(); 

在这里,我打电话给我的basefile.JS和使用thirdd的js文件中定义的方法有我testMethod.js代码是

describe("sample to check inheritance",function(){ 
var testOne = require("./tempPageOne.js"); 

it("working with inheritance",function(){ 
    browser.get("http://www.protractortest.org/#/"); 
    testOne.ddselection("html/body/nav/div/div[2]/div/ul/li[3]/a","html/body/nav/div/div[2]/div/ul/li[3]/ul/li[4]/a"); 
    console.log("Working fine"); 
}); 

}); 

这是我的规范文件的一个简单的测试,但得到错误不知道该怎么办 失败: 1)样本检查继承遇到声明异常 消息: ReferenceError:basefile未定义 堆栈: ReferenceError:未在基于对象的新tempPageOne(D:\ eclipseProject \ JavaScriptInheritance \ tempPageOne.js:4:23) 处定义basefile 。 (D:\ eclipseProject \ JavaScriptInheritance \ tempPageOne.js:10:18) at要求(module.js:385:17) at Suite。 (d:\ eclipseProject \ JavaScriptInheritance \ testMethod.js:3:16) 在addSpecsToSuite(C:\用户\ rajnish \应用程序数据\漫游\ NPM \ node_modules \量角器\ node_modules \茉莉\ node_modules \茉莉核\ lib中\茉莉-core \ jasmine.js:743:25)

回答

1

保留一切同在任何没有变化Js文件只改变这样的tempPageOne.js,它正在工作

var basefile = require("./basefile.js"); 

var tempPageOne = function(){ 

/* // Comination One : works this way also 
    var BaseMethod = Object.create(basefile); 
    this.ddselection = function(locOne,locTwo){ 
     BaseMethod.elementClick(locOne); 
     BaseMethod.elementClick(locTwo); 
    };*/ 


    /* 
    // Comination Two :works this way also 
    var BaseMethod = basefile; 
    this.ddselection = function(locOne,locTwo){ 
     BaseMethod.elementClick(locOne); 
     BaseMethod.elementClick(locTwo); 
    }; 
    */ 

    // Comination Three :works this way also 
    //var BaseMethod = basefile; 
    this.ddselection = function(locOne,locTwo){ 
     basefile.elementClick(locOne); 
     basefile.elementClick(locTwo); 
    }; 

}; 

module.exports = new tempPageOne(); 

注意事项Comination四请看下面

1

您还可以扩展basefile正是如此...

var basefile = function() { 
}; 

basefile.prototype.elementClick = function(locator) { 
    element(by.xpath(locator)).click(); 
}; 
module.exports = new basefile(); 

则...

var basefile = require("./basefile.js"); 

var tempPageOne = function(){ 
    this.ddselection = function(locOne,locTwo){ 
    this.elementClick(locOne); 
    this.elementClick(locTwo); 
    }; 
}; 
tempPageOne.prototype = basefile; // extend basefile 
module.exports = new tempPageOne(); 
+0

感谢@Brine答案@卤水其工作 –