使用nightwatch.js断言webelements列表的文本值 [英] Assert text value of list of webelements using nightwatch.js

查看:44
本文介绍了使用nightwatch.js断言webelements列表的文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用nightwatch.js的新手。
我想得到一个元素列表,并用给定的字符串验证每个元素的文本值。
我试过:

I am new to using nightwatch.js. I want to get a list of elements and verify text value of each and every element with a given string. I have tried :

function iter(elems) {
      elems.value.forEach(function(element) {
        client.elementIdValue(element.ELEMENT)
      })
    };
    client.elements('css selector', 'button.my-button.to-iterate', iter);

另一个stackoverflow问题
但我现在使用的是

For another stackoverflow question But what I am using right now is

waitForElementPresent('elementcss', 5000).assert.containsText('elementcss','Hello')

并且它返回输出

Warn: WaitForElement found 5 elements for selector "elementcss". Only the first one will be checked.

所以我希望它能够验证列表中每个元素的文本值。

So I want that it should verify text value of each and every element of list.

推荐答案

所有的事情都不能通过nightwatch js简单命令来完成,所以他们提供了自定义命令意味着selenium协议。在这里,您可以拥有所有 selenium协议。我使用以下代码来断言具有给定字符串text的每个元素的文本值。希望它能帮到你

All the things can not be done by nightwatch js simple commands , so they have provided the custom command means selenium protocol. Here you can have all the selenium protocol. I have used following code to assert text value of each and every element with a given string "text". Hope it will help you

    module.exports = {
  '1. test if multiple elements have the same text' : function (browser) {
    function iter(elems) {
       elems.value.forEach(function(element) {
        browser.elementIdText(element.ELEMENT, function(result){
          browser.assert.equal(result.value,'text')
        })
       })
     };

    browser
      .url('file:///home/user/test.html')
      .elements('tag name', 'a', iter);

    }

  };

我的HTML代码段

<div id="test">
<a href="google.com" class='red'> text </a>
<a href="#" class='red'> text </a>
<a href="#" class='red'> text 1</a>
</div>

这篇关于使用nightwatch.js断言webelements列表的文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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