Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jasmine在量角器中使用javascript原型(继承)概念_Javascript_Jasmine_Protractor_Pom.xml - Fatal编程技术网

使用jasmine在量角器中使用javascript原型(继承)概念

使用jasmine在量角器中使用javascript原型(继承)概念,javascript,jasmine,protractor,pom.xml,Javascript,Jasmine,Protractor,Pom.xml,大家好,我正在尝试使用POM概念,我的代码是第一个文件basefile.JS var basefile = function() { }; basefile.prototype.elementClick = function(locator) { element(by.xpath(locator)).click(); }; //module.exports = new basefile(); require("./basefile.js"); var tempPageOne = funct

大家好,我正在尝试使用POM概念,我的代码是第一个文件basefile.JS

var basefile = function() {
};
basefile.prototype.elementClick = function(locator) {
 element(by.xpath(locator)).click();
};
//module.exports = new basefile();
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();
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");
 });

});
此文件包含用户可以在任何web应用程序上执行的常用方法。 第二个js文件tempPageOne.js我的代码是

var basefile = function() {
};
basefile.prototype.elementClick = function(locator) {
 element(by.xpath(locator)).click();
};
//module.exports = new basefile();
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();
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");
 });

});
在这里,我调用我的basefile.JS并使用thirdd JS文件testMethod.JS中定义的方法,我的代码是

var basefile = function() {
};
basefile.prototype.elementClick = function(locator) {
 element(by.xpath(locator)).click();
};
//module.exports = new basefile();
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();
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:未定义基本文件 堆栈: ReferenceError:未定义基本文件 在新的tempPageOne上(D:\eclipseProject\JavaScriptInheritation\tempPageOne.js:4:23) 反对。(D:\eclipseProject\JavaScriptInheritation\tempPageOne.js:10:18) 根据需要(module.js:385:17) 在套房。(D:\eclipseProject\JavaScriptInheritation\testMethod.js:3:16)
在addSpecsToSuite(C:\Users\rajnish\AppData\Roaming\npm\node_modules\Gragrator\node_modules\jasmine\node_modules\jasmine core\lib\jasmine core\jasmine.js:743:25)

您还可以扩展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();

保持所有内容不变在任何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();
注意:第四组请看下面的答案