javascript 使用 nightwatch.js 断言网络元素列表的文本值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30193943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:52:28  来源:igfitidea点击:

Assert text value of list of webelements using nightwatch.js

javascriptnode.jsseleniumnightwatch.js

提问by vibhor

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 :

我是使用 nightwatch.js 的新手。我想获取元素列表并使用给定字符串验证每个元素的文本值。我努力了 :

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

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

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

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

and it is returning me the output

它正在返回我的输出

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.

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

回答by Juhi Saxena

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

所有的事情都是 nightwatch js 简单的命令无法完成的,所以他们提供了自定义命令手段 selenium 协议。在这里你可以拥有所有的 selenium 协议。我使用以下代码来断言每个元素的文本值与给定的字符串“文本”。希望它会帮助你

    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);

    }

  };

My HTML snippet

我的 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>

回答by vibhor

I was able to do it as :

我能够做到:

.elements('css selector', 'cssValue', function (elements) {
        for(var i=0;i<elements.value.length;i++){
        var elementCss = 'div.search-results-item:nth-child(' + (i+1) + ') span';
            client.assert.containsText(elementCss,'textValue');
        }
        })

回答by Ashish-BeJovial

Put your function iter in a for loop and before that use

将您的函数 iter 放入 for 循环中并在使用之前

client.elements('css selector', '#CollectionClass', function (result) {
  if(result.value.length > 1) {   
     var count;
     for(count=1; count<result.value.length; count++) {
       result.value.forEach(function(element) {
       client.elementIdValue(element.ELEMENT);
       client.elementIdText(selectedHighlight.ELEMENT, function(resuddlt) {
         this.assert.equal(typeof resuddlt, "object");
         this.assert.equal(resuddlt.status, 0);
         this.assert.equal(resuddlt.value, "your value");
      });
     }     
   }
  }
};

回答by E.C.Mumford

You have stated what you have tried (which is good) but you haven't presented us with sanitized HTML that demonstrates the problem (which reduces precision in possible answers).

您已经说明了您尝试过的内容(这很好),但您没有向我们展示说明问题的经过消毒的 HTML(这会降低可能答案的准确性)。

There are many ways in HTML to contain information, and the built-in Nightwatch containsTextwill serialize any text it finds within a structure that contains substructures.

HTML 中有很多方法可以包含信息,内置的 NightwatchcontainsText将序列化它在包含子结构的结构中找到的任何文本。

So for example, if you have as Juhi suggested,

例如,如果你按照 Juhi 的建议,

<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>

Then the assertions

然后是断言

.verify.containsText('#test', ' text ') // first one
.verify.containsText('#test', ' text ') // second one
.verify.containsText('#test', ' text 1') // third one

will pass, because they each verify the specific information without the need for writing a loop. Nightwatch will look at the testelement and serialize the elements into the string text text text 1

会通过,因为他们各自验证了具体信息,不需要写循环。Nightwatch 会查看test元素并将元素序列化为字符串text text text 1

Now if you need a loop for other reasons this is all academic, but your original question seemed to be targeted at how to get the text information out, not necessarily how to execute one possible solution to the problem (which is writing a loop).

现在,如果您出于其他原因需要循环,这完全是学术性的,但是您最初的问题似乎是针对如何获取文本信息,而不一定是如何执行该问题的一种可能解决方案(即编写循环)。