量角器:函数重载

Protractor: functions overloading

我尝试扩展 ElementFinder 库。我想知道如何要求具有相同名称的不同方法? 我想制作类似的东西:

 // spec.js
 var ef1 = require('./ef_extend1.js');
 var ef2 = require('./ef_extend2.js');

 expect(column_resizer.ef1.getWidth()).toEqual(18);
 expect(column_resizer.ef2.getWidth()).toEqual(18);

现在我有一个错误:

TypeError: Cannot read property 'getWidth' of undefined

我需要的库:

// ef_extend1.js
var ElementFinder = $('').constructor;
ElementFinder.prototype.getWidth = function() {
    return this.getSize().then(function(size) {
                                  return size.width + 1;
                               });
};

第二个:

// ef_extend2.js
var ElementFinder = $('').constructor;
ElementFinder.prototype.getWidth = function() {
    return this.getSize().then(function(size) {
                                  return size.width;
                               });
};

我猜你使用了 Protractor issue #1102, but now it can be accomplished a bit easier after PR#1633 的解决方案,因为 ElementFinder 现在暴露在 protractor 全局变量中:

protractor.ElementFinder.prototype.getWidth = function () {
    return this.getSize().then(function (size) {
        return size.width;
    });
};

expect($('body').getWidth()).toBe(100);

更新:

正如我在评论中所说,ElementFinder只能一次又一次地延长。如果您已经有一个方法 getWidth,并且您用另一个 getWidth 实现扩展 ElementFinder,那么第一个将被覆盖,应该不会有任何冲突。但是你必须严格按照你想要使用的方法集来排列它们:

require('./ef_extend1.js');

expect(column_resizer.getWidth()).toEqual(18);

require('./ef_extend2.js');

expect(column_resizer.getWidth()).toEqual(18);

实际上我已经有了一些替代方法,但我不认为它会很好用,但无论如何。这是一个带有扩展方法的示例模块:

// ef_extend1.js

// shortcut
var EF = protractor.ElementFinder;

// holds methods you want to add to ElementFinder prototype
var extend = {
    getWidth: function () {
        return this.getSize().then(function (size) {
            return size.width;
        });
    }
};

// will hold original ElementFinder methods, if they'll get overriden
// to be able to restore them back
var original = {};

// inject desired methods to prototype and also save original methods
function register() {
    Object.keys(extend).forEach(function (name) {
        original[name] = EF.prototype[name]; // save original method
        EF.prototype[name] = extend[name]; // override
    });
}

// remove injected methods and return back original ones
// to keep ElementFinder prototype clean after each execution
function unregister() {
    Object.keys(original).forEach(function (name) {
        if (typeof original[name] === 'undefined') {
            // if there was not such a method in original object
            // then get rid of meaningless property
            delete EF.prototype[name];
        } else {
            // restore back original method
            EF.prototype[name] = original[name];
        }
    });
    original = {};
}

// pass a function, which will be executed with extended ElementFinder
function execute(callback) {
    register();

    callback();

    unregister();
}

module.exports = execute;

您将像那样使用它们,能够在 "isolated" 环境中 运行 量角器命令,其中每个环境都有自己的一组方法 ElementFinder:

var ef1 = require('./ef_extend1.js');
var ef2 = require('./ef_extend2.js');

ef1(function () {
    expect(column_resizer.getWidth()).toEqual(18);
});

ef2(function () {
    expect(column_resizer.getWidth()).toEqual(18);
});

我不确定,也许我在这里过度设计了,而且有比这更容易的解决方案。