nightwatch-api icon indicating copy to clipboard operation
nightwatch-api copied to clipboard

Custom command not working with nightwatch-api

Open BinaryJava opened this issue 5 years ago • 2 comments

I am trying to migrate our older nightwatch-cucumber framework to a nightwatch-api one. Everything works apart from the WaitForText custom command. It works on the old instance however.

This is the error it gives me when it tries to run the custom command:

Error while running "WaitForText" command: Cannot read property 'isES6Async' of undefined
TypeError: Error while running "WaitForText" command: Cannot read property 'isES6Async' of undefined
    at queuedCommandFn (/home/kieran/Documents/automation/updated-framework/node_modules/nightwatch/lib/api/_loaders/command.js:195:35)
    at CommandInstance.WaitForText.check (/home/kieran/Documents/automation/updated-framework/lib/CustomCom/WaitForText.js:34:5)
    at CommandInstance.WaitForText.command (/home/kieran/Documents/automation/updated-framework/lib/CustomCom/WaitForText.js:20:10)
    at /home/kieran/Documents/automation/updated-framework/node_modules/nightwatch/lib/api/_loaders/command.js:154:29
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

This is the WaitForText.js custom command:

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

function WaitForText() {
    events.EventEmitter.call(this);
    this.startTime = null;
}

util.inherits(WaitForText, events.EventEmitter);

WaitForText.prototype.command = function (element, getter, checker, ms) {
    this.startTime = new Date().getTime();
    var self = this;
    var message;

    if (typeof ms !== 'number') {
        ms = 1000;
    }

    this.check(element, getter, checker, function (result, loadedMs) {
        if (result) {
            message = 'Expression was true after ' + (loadedMs - self.startTime) + ' ms.';
        } else {
            message = 'Expression wasn\'t true in ' + ms + ' ms.';
        }
        self.client.assertion(result, 'expression false', 'expression true', message, true);
        self.emit('complete');
    }, ms);
    return this;
};

WaitForText.prototype.check = function (element, getter, checker, cb, maxTime) {
    var self = this;
    getter(element, function (result) {
        var now = new Date().getTime();
        if (result.status === 0 && checker(result.value)) {
            cb(true, now);
        } else if (now - self.startTime < maxTime) {
            setTimeout(function () {
                self.check(element, getter, checker, cb, maxTime);
            }, 1000);
        } else {
            cb(false);
        }
    });
};

module.exports = WaitForText;

I don't know where to begin as I don't understand how the custom command works, it was already included with the previous framework.

Any help would be greatly appreciated.

BinaryJava avatar Jul 30 '20 16:07 BinaryJava

Were you able to make it work? @BinaryJava

spnraju avatar Jan 16 '21 14:01 spnraju

Nope, I never got any further with it.

BinaryJava avatar Jan 16 '21 14:01 BinaryJava