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

标签 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:未定义基本文件 堆: 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)

最佳答案

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

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

关于javascript - 使用 jasmine 在 Protractor 中使用 javascript 原型(prototype)(继承)概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34893204/

相关文章:

angularjs - 如何使用 Protractor 识别 ionic 标签?

javascript - 如何在 Protractor/ Selenium 中实现间隔

php - 在线酒店预订系统,同时预订?

javascript - AngularJS:如何显示数组以及是否选择了要显示的任何值?

Javascript - 获取一行中的最后一个词

javascript - 监视 Karma 和 Jasmine 中的 setTimeout 和 clearTimeout

javascript - AngularJS 和 Protractor - 使 http 请求持续更长时间

javascript - 使用 api.updateRowData(transaction) 删除一行对数据源没有影响

javascript - 无限 Jasmine 超时

javascript - 元素上的 Angular 模拟方法