javascript Nightwatch js 如何断言多个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27116103/
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 06:58:03  来源:igfitidea点击:

Nightwatch js how to assert multiple elements

javascriptseleniumaccessibilityfunctional-testingnightwatch.js

提问by Stefanie

i'm trying to assert/verify multiple elements for an attribute with Nightwatch.js.

我正在尝试使用 Nightwatch.js 断言/验证属性的多个元素。

I tried to use the "elements" command by selenium but it seems to not actually return a tag.

我尝试使用 selenium 的“元素”命令,但它似乎实际上并未返回标签。

browser.elements('css selector','icon_checkmark', function (result) {
    this.verify.attributeEquals(result.value, 'aria-hidden', 'true');
})

The console outputs this error:

控制台输出此错误:

Testing if attribute aria-hidden of <[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]> equals "true". 
Element could not be located.  - expected "true" but got: null
ERROR: Unable to locate element: "[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]" using: css selector

It seems like it finds the right things because there should be 6 but then somehow I read out the wrong thing? Console.log of result.value[0] gives just { ELEMENT: '19' } which seems correct.

似乎它找到了正确的东西,因为应该有 6 个,但不知何故我读出了错误的东西?result.value[0] 的 Console.log 只给出了 { ELEMENT: '19' } 这似乎是正确的。

Any idea how could I make this work? I want to check if all elements with the class icon_checkmark have an attribute aria-hidden="true".

知道我怎么能让这个工作吗?我想检查是否所有带有 icon_checkmark 类的元素都具有属性 aria-hidden="true"。

采纳答案by Tinple

attributeEqualslocate element using css selector, while elementsreturns IDof element, so you can't locate the element that way. You can use elementIdAttributeto get the element and verify it.

attributeEquals使用 css 选择器定位元素,而elements返回ID元素,因此您无法以这种方式定位元素。您可以使用elementIdAttribute获取元素并验证它。

browser.elements('css selector','icon_checkmark', function (result) {
  result.value.map(function (v, k) {
    browser.elementIdAttribute(v.ELEMENT, 'aria-hidden', function (res) {
      // true
      return browser.assert.equal(res.value, 'expected value');
    });
  });
})

回答by BrandenB171

result.value is an array. so you would need to loop through the array result.value[x]

result.value 是一个数组。所以你需要遍历数组 result.value[x]