检查一个div是否在nightwatch中包含我的值 [英] Check if one of divs contains my values in nightwatch

查看:89
本文介绍了检查一个div是否在nightwatch中包含我的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用nightwatch.js测试我的webapp时遇到问题。我需要遍历页面上的所有div元素,以检查是否存在包含我之前添加的所有子元素的那个元素。例如,我有:

I have problem with testing my webapp with nightwatch.js. I need to iterate over all div elements on the page to check if there is the one who contains all child elements I added before. For example I have:

<div className = 'myclass'>
    <h2> text1 </h2>
    <h3> second_text1 </h3>
</div>
<div className = 'myclass'>
    <h2> text2 </h2>
    <h3> second_text2 </h3>
</div>

我想检查其中一个div是否包含:'text2'和'second_text2'。我试着在这里添加iter功能:断言文本值使用nightwatch.js 的webelements列表,但我不知道如何检查这个div中的子元素,并且只有在没有div包含我的值时才断言。

And I want to check if one of divs contains both: 'text2' and 'second_text2'. I tried to add iter function like here: Assert text value of list of webelements using nightwatch.js but I don't know how to check child elements in this div and assert only if none of divs contains my values.

推荐答案

不幸的是,nightwatch不能很好地支持承诺或一般异步。 此处的博客文章总结了一些难点。

Unfortunately, nightwatch does not support promises or general asyncness very well. The blog post here summarises some of the pain points.

幸运的是,可以使用 browser.elements browser.perform 的组合。

Fortunately, it is possible using a combination of browser.elements and browser.perform.

var assert = require('assert');

module.exports = {
    'foobar': function (browser) {
        var isTextFound = false;
        browser
            .url('http://localhost:3000')
            .waitForElementVisible('body', 1000);

        browser.elements('css selector', '.myclass', function (res) {
            res.value.forEach(function (jsonWebElement) {
                var jsonWebElementId = jsonWebElement.ELEMENT;
                browser.elementIdText(jsonWebElementId, function (jsonElement) {
                    var text = jsonElement.value;
                    if (text.indexOf('text1') !== -1 && text.indexOf('second_text') !== -1) {
                        isTextFound = true;
                    }
                });
            });
        });
        browser.perform(function () {
            assert.ok(isTextFound, 'Text found');
        });


        browser.end();
    }
}

让我们来看看一些关键点

Let's go through some of the key points

require('assert')

这是必需的,因为nightwatch断言仅对DOM元素进行操作,因此您无法断言布尔值。这也意味着断言不会出现在报告上,但是如果它是假的,它会抛出一个错误,这就会出现。

This is required because nightwatch asserts only operate on DOM elements and hence you can not assert a boolean. This also means that the assertion will not show up on the report, however if it is false, it will throw an error and this will come up.

browser.perform(function(){...})

这会将断言置于命令队列中,以便断言将事后发生。如果不存在,评估将立即发生,断言将失败。

This puts the assertion onto the command queue so that the assertion will happen afterwards. If this was not there, the evaluation will happen immediately and the assert will fail.

browser.elements('css selector,'。myclass' ,function(res){...})

这是使用 elements api 。请注意,webdriver协议部分下的api不会直接返回web元素,而是返回Web元素的Selenium表示(WebElement JSON对象),因此我们必须使用特殊方法,例如 elementIdText 从中获取信息。

This is using the elements api. Please note that the api under the webdriver protocol section do not return web elements directly but return the Selenium representation of web elements (WebElement JSON objects) and hence we have to use special methods such as elementIdText to get information out of them.

这篇关于检查一个div是否在nightwatch中包含我的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆