守夜人自定义命令原型

nightwatch custom command prototype

我遵循了 here 中的说明(守夜人中的暂停命令),以创建一个自定义命令来获取当前使用的选择器(css 或 xpath)。

var util = require('util');
var events = require('events');

function GetSelector() {
    events.EventEmitter.call(this);
}

util.inherits(GetSelector, events.EventEmitter);

GetSelector.prototype.command = function (callback) {
    callback(this.client.locateStrategy);
};

module.exports = GetSelector;

虽然程序在调用自定义命令时卡住了,但实现获取了当前选择器。

  browser
    .getSelector(function (currentSelector) { 
      console.log('getSelector: ' + currentSelector); 
    })
    

我也试过按照建议用“self.perform”环绕,不幸的是没有任何运气。

GetSelector.prototype.command = function (browser, callback) {
    browser.perform(function () {
        callback(this.client.locateStrategy);
    })
};

我错过了什么?

"Signaling the complete event needs to be done inside an asynchronous action"action"self.emit('complete');,根据守夜人homepage

GetSelector.prototype.command = function (cb) {
    var self = this;

    process.nextTick(function () {
        cb(self.client.locateStrategy);

        //Signaling the complete event needs to be done inside an asynchronous action.
        self.emit('complete');
    });

    return this;
};

process.nextTick 确保 self.emit('complete'); 将被 Node 处理的事件循环的下一个 tick 调用。