nightwatch 声明所有元素,而不是任何元素

nightwatch assert ALL elements, not ANY

我正在尝试测试,当在空表单上单击“提交”按钮时,所有 "please fill in this field" 标签都会显示。

我正在这样做:

page.click('@btn_submit');
page.expect.element('@validation_label_required').to.be.visible;

其中@validation_label_required 由 CSS 选择器表示:

input[required] ~ p.error-message-required

但是,如果任何验证标签可见,则此测试通过。测试应该只有在它们都通过时才能通过。

我怎样才能做到这一点?

您需要创建自定义断言,通过 selenium 命令定位所有元素,然后循环验证条件。它应该看起来像这样

var util = require('util');
exports.assertion = function (elementSelector, expectedValue, msg) {

    this.message = msg || util.format('Testing if elements located by "%s" are visible', elementSelector);

    this.expected = expectedValue;

    this.pass = function (value) {
        return value === this.expected;
    };

    this.value = function (result) {
        return result;
    };

    this.command = function (callback) {
        var that = this.api;
        this.api.elements('css selector',elementSelector, function (elements) {
            elements.value.forEach(function(element){
              that.elementIdDisplayed(element.ELEMENT,function(result){
                if(!result.value){
                  callback(false);
                 }
               });
             });
            callback(true);
        });
        return this;
    };

};

我刚刚结束了另一个自定义断言,它检查给定的 css 选择器可以看到多少元素。

/**
 * Check how many elements are visible by given css selector.
 * 
 */

var util = require('util');
exports.assertion = function(elementSelector, expectedCount, msg) {

this.message = msg || util.format('Asserting %s elements located by css selector "%s" are visible', expectedCount, elementSelector);

this.expected = expectedCount;

this.count = 0;

this.pass = function(value) {
    return value === this.expected;
};

this.value = function(result) {
    return this.count;
};

this.command = function(callback) {
    var me = this, elcount = 0; 
    this.count = 0;
    this.api.elements('css selector', elementSelector,  function(elements) {

        if(elements.value && elements.value.length > 0){
            elcount = elements.value.length;                
        }else{
            return callback(false);
        }
        elements.value.forEach(function(element) {
            me.api.elementIdDisplayed(element.ELEMENT, function(result) {
                if (result.value) {
                    me.count++;
                }
                elcount--;
                if (elcount === 0) {
                    callback(me.count);
                }
            });
        });
    });
};
};