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

时间:2016-01-20 06:44:52

标签: 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();

此文件包含用户可以在任何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:未定义basefile   堆:     ReferenceError:未定义basefile         在新的tempPageOne(D:\ eclipseProject \ JavaScriptInheritance \ tempPageOne.js:4:23)         在对象。 (d:\ eclipseProject \ JavaScriptInheritance \ tempPageOne.js:10:18)         at require(module.js:385:17)         在套房。 (d:\ eclipseProject \ JavaScriptInheritance \ testMethod.js:3:16)         at addSpecsToSuite(C:\ Users \ rajnish \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ jasmine \ node_modules \ jasmine-core \ lib \ jasmine-core \ jasmine.js:743:25)

2 个答案:

答案 0 :(得分:1)

你也可以这样扩展基本文件......

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();

答案 1 :(得分: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();

Comining Four的注意事项请看下面的@Brine答案