2016-04-22 27 views
2

相同元素可能会更改为不同 ID或名称取决于许多因素,我将能够对此元素做出准确的断言。Nightwatchjs可以根据参考准确地执行断言吗?

Doest nighwatchjs允许做一个基于相对位置的断言,就像可以做SAHI一样吗? (左边这个元素...,下一个div等)

我想避免Xpath解决方案,它基于元素类型(div,id,name等),如果我将它设置为all类型:

//*[contains(text(),'hello world')] 

我会得到许多事件,无法知道哪一个我试图断言。

e.g:运行在同一个页面上相同的测试,我就能够找到这个“世界你好”,即使DIV ID发生变化或其他因素。

<div id="homebutton"> 
    <p> 
     <a href=#> 
      <span name="hm">Home</span> 
     <a> 
    </p> 
</div> 
<div id=[0-9]> 
    <p> 
     <a href=#> 
      <span name="hw">hello world</span> 
     <a> 
    </p> 
</div> 
[...] 
<div id=[0-9]> 
    <p> 
     <a href=#> 
      <span name="hw">hello world</span> 
     <a> 
    </p> 
</div> 
<div id="logoutbutton"> 
    <p> 
     <a href=#> 
      <span name="lo">Logout</span> 
     <a> 
    </p> 
</div> 

测试例如:断言元素包含字符串“Hello World”,不是一个这是附近的注销按钮,但其中之一是附近的home键

+0

你可以只有一个基于ID的选择器?另外请记住,ID选择器不应以数字开头以实现向后兼容性 - https://developer.mozilla.org/zh-CN/docs/Web/HTML/Global_attributes/id –

回答

0

中的XPath的解决方案是好的,但这里是我所需要的解决方案,更通用,并给予更多的选择:

使用elements和管理返回childrend元素的数组

我选用返回数组

[{ID:与数据的匹配我的需求的对象webElementId,尺寸:{宽度:,高度:},...},{ID:webElementId,...}等]

有了这些信息,我可以做很多事情:

  • 查找与元素一个特定的文本,属性或cssproperty和 对其执行任何操作,如断言或通过计算其大小点击它的右侧。

  • 鼠标悬停匹配每个元素(如果你想用浏览子菜单 UL LI /醇李标签)

更多数据被填充时,更可以执行断言。

0

中的XPath选择"//div[contains(text(), 'hello world')]" 将同时匹配还有你的元素。如果元素本身可以改变,你可以使用通配符:"//*[contains(text(), 'hello world')]"

对于比赛,任何元件上,准确的文字:

"//*[.='hello world']" 

A great source, a "Rosetta stone", for selector construction

要与nightwatch使用XPath选择:

"some test": function(client){ 
client 
    .useXpath().waitForElementPresent("//div[contains(text(), 'hello world')]", this.timeout) 
} 
+0

感谢您的回答,我编辑了我的帖子给你更多关于我的问题的信息。问题是下一步,如果我发现许多事件,如何声明特定的元素? –

+0

您会在上面的Rosetta stone链接中看到一些解决方案。另外,您可能想使用:not css选择器。我现在淹没了,或者我只写了它。另外:为什么xpath不适合你?你可以用xpath索引到特定的一个,你也不需要使用通配符。 – QualiT

1

扩展我以前的答案,你有两个选择,如果你想要的Hello World总是第二个到最后,出现你想第二个到最后一个类型就在这时,之前退出按钮,你可以使用XPath选择这样的:

"//*[.='hello world'][last()-1]" 

这是正确的罗塞塔DOC我与您共享,让你应该知道,由现在

另一种选择是获取所有匹配的集合。对于这一点,我会写一个辅助函数,像这样:

module.exports = { 

    getCountOfElementsUseXpath : function (client, selector, value) { 
     // set an empty variable to store the count of elements 
     var elementCount; 
     // get a collection of all elements that match the passed selector 
     client.getEls(selector, function(collection) { 
      // set the variable to be that collection's length  
      elementCount = collection.length; 
      // log the count of elements to the terminal 
      console.log("There were " + elementCount + " question types")  

      return elementCount;   
     }); 
    }, 

}; 

然后你可以使用一些公式为您选择多远是最后一个元素。

+1

感谢您的回答,这可能是nightwatchjs的最佳解决方案。但它也很棘手,本身不支持它,所以请编辑你的答案,最终回答我的最初问题:“这是不可能的,但有其他选择......” –