量角器:类 继承

Protractor: classes inheritance

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

1-st class:

 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;

2-nd class:

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

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

不完全确定你在找什么,但也许是这些方面的东西......?

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

var PsComponent = function() {};

util.inherits(PsComponent, ElementArrayFinder);

PsComponent.prototype = util;
PsComponent.prototype = psDateTimePicker;
module.exports = new PsComponent();

然后...

describe('lala', function () {
  var psComponents = require('../lib/psComponents');

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

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