angularjs - Protractor :类继承

标签 angularjs node.js protractor prototypal-inheritance

我有 3 级继承类: 第一个类继承ElementArrayFinder,第二个类继承第一个类。这是我的代码:

一级:

 var util = require('util');
 var ElementArrayFinder = require('C:/Users/Lilia.Sapurina/AppData/Roaming/npm/node_modules/protractor/lib/element').ElementArrayFinder;

 var psComponent = function() {};

 util.inherits(psComponent, ElementArrayFinder);

 module.exports = psComponent;

二级:

var util = require('util');
var psComponent = require('../lib/psComponent');

var psDateTimePicker = function() {};

util.inherits(psDateTimePicker, psComponent);

psDateTimePicker.prototype.getField = function() {
  return element(by.xpath('//div[1]/table/tbody/tr[2]/td[1]/div/input'));
};

psDateTimePicker.prototype.getButton = function() {
  return element(by.css('td > a.b-button.b-button_only_icon'));
};

exports.psDateTimePicker = new psDateTimePicker();

我可以通过这种方式在我的代码中使用它:

  fdescribe('lala', function () {

     var psDateTimePicker = require('../lib/psDateTimePicker').psDateTimePicker;

    beforeEach(function(){
      browser.get('ng-components/examples/ps-date-time-picker.html');
    });

    it('test', function () {
             expect(psDateTimePicker.getField().getAttribute("value")).toEqual(6);

  });
});

但我想制作全局界面并使代码构造看起来像:

fdescribe('lala', function () {

  var psComponents = require('../lib/psComponent');

  beforeEach(function(){
    browser.get('ng-components/examples/ps-date-time-picker.html');
  });

  it('test', function () {
       expect(psComponents.psDateTimePicker.getField().getAttribute("value")).toEqual(6); 
  });
});

有人知道我该如何组织吗?我尝试使用 getInstanceOf(),但它不起作用。而且也不是很方便。

最佳答案

如果您想为所有组件创建一个命名空间,您只需创建一个额外的模块来负责:

// psComponents.js
module.exports = {
    psDateTimePicker: require('../lib/psDateTimePicker').psDateTimePicker,
    psAnotherComponent: require('../lib/psAnotherComponent').psAnything
};

并按照您想要的方式使用它:

var psComponents = require('../lib/psComponents');
expect(psComponents.psDateTimePicker.getField().getAttribute("value")).toEqual(6);

这比您尝试做的事情简单得多 - 尝试从父级访问子级 - 没有简单的方法来实现它,并且通常没有理由这样做。原型(prototype)只是创建新的独立实例的样板,它们不应该相互连接。与原型(prototype)继承几乎相同:您只需扩展该样板,但子实例和父实例将保持独立。

关于angularjs - Protractor :类继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32004348/

相关文章:

javascript - Angular 应用程序中关于 ng-include 的模块化与性能

angularjs - 如何使用 AngularJS 指定 optionLabelTemplate Kendo UI DropDownList

php - 如何从服务器接收更新而不每隔 n 秒请求一次

javascript - 如何在 ng-if 中传递 ng-repeat 变量?

javascript - 是否有一种纯粹的基于 Promise 的映射/连接集合的方法?

Javascript promise 好奇心

node.js - 如何使用 laravel 5.4 中的 LRedis 和 nodejs 中的 socket.io 向同一 channel 中的特定用户发送 apns 通知或消息?

javascript - Protractor 等待模态对话框

jasmine - 所有测试完成后如何制作分片 Protractor 测试报告(而不是每个文件)?

javascript - Protractor 页面对象 - 未调用方法