在 Protractor using jasmine 中使用 javascript 原型(继承)概念

Using javascript prototype (inheritance) concept in Protractor using jasmine

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

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 并使用在第三个 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:基本文件未定义 堆: ReferenceError:基本文件未定义 在新的 tempPageOne (D:\eclipseProject\JavaScriptInheritance\tempPageOne.js:4:23) 在对象。 (D:\eclipseProject\JavaScriptInheritance\tempPageOne.js:10:18) 在要求 (module.js:385:17) 在套房。 (D:\eclipseProject\JavaScriptInheritance\testMethod.js:3:16) 在 addSpecsToSuite(C:\Users\rajnish\AppData\Roaming\npm\node_modules\protractor\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();

第四次合作的注意事项请看下面@Brine 的回答