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

时间:2015-09-25 15:26:06

标签: nightwatch.js

我正在尝试测试,当在空表单上单击“提交”按钮时,将显示所有“请填写此字段”标签。

我这样做:

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

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

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

但是,如果任何验证标签可见,则此测试通过。如果ALL都是,则测试应该通过。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

您将需要为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;
    };

};

答案 1 :(得分:0)

我刚刚结束另一个自定义断言,检查给定的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);
                }
            });
        });
    });
};
};