【问题标题】:nightwatch custom command prototypenightwatch 自定义命令原型
【发布时间】:2022-03-03 23:04:14
【问题描述】:

我按照here(nightwatch 中的暂停命令)的说明,创建了一个自定义命令来获取当前使用的选择器(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); 
    })
    

我还尝试按照here 的建议使用“self.perform”,不幸的是没有任何运气。

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

我错过了什么?

【问题讨论】:

    标签: node.js prototype nightwatch.js


    【解决方案1】:

    “通知完成事件需要在异步操作中完成”action"self.emit('complete');,根据 Nightwatch 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 调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 2014-03-17
      • 2010-12-15
      相关资源
      最近更新 更多