实习生 - 功能测试查找元素与匹配的后代
The Intern - functional tests Finding Element with A Descendant that matches
我正在编写一个功能测试脚本来查找具有可以找到的子元素的父元素,如果找到后代元素,return 父元素。例如:
<div class="contentPane">
<h2>Heading 1</h2>
<p id="first">FIRST TEXT</p>
</div>
<div class="contentPane">
<h2>Heading 2</h2>
<p id="second">SECOND TEXT</p>
</div>
<div class="contentPane">
<h2>Heading 2</h2>
<p id="third"></p>
</div>
我想找到能找到id="second"段落的contentPane。我找到父级的测试用例与此类似:
...
findAllCssSelector(".contentPane")
.then(function(array, setContext){
//for every element i in array
//I want to call its findByCssSelector(".second")
//and check if it is found. If it is
//I want to return the ith element in array
// to the command.
})
.findByTagName("h2")
.getVisibleText()
.then(function(text){
assert.strictEqual(text, "Heading 2");
})
....
...
如何遍历每个数组元素并将数组元素return传递到上下文堆栈?
对于复杂的查询,Xpath 通常比手动搜索元素更有效。您可以使用以下内容查询:
.findByXpath('//div[@class="contentPane" and p[@id="second"]]')
这将找到第一个 DIV class "contentPane",其中包含 ID 为 "second" 的 P。
我正在编写一个功能测试脚本来查找具有可以找到的子元素的父元素,如果找到后代元素,return 父元素。例如:
<div class="contentPane">
<h2>Heading 1</h2>
<p id="first">FIRST TEXT</p>
</div>
<div class="contentPane">
<h2>Heading 2</h2>
<p id="second">SECOND TEXT</p>
</div>
<div class="contentPane">
<h2>Heading 2</h2>
<p id="third"></p>
</div>
我想找到能找到id="second"段落的contentPane。我找到父级的测试用例与此类似:
...
findAllCssSelector(".contentPane")
.then(function(array, setContext){
//for every element i in array
//I want to call its findByCssSelector(".second")
//and check if it is found. If it is
//I want to return the ith element in array
// to the command.
})
.findByTagName("h2")
.getVisibleText()
.then(function(text){
assert.strictEqual(text, "Heading 2");
})
....
...
如何遍历每个数组元素并将数组元素return传递到上下文堆栈?
对于复杂的查询,Xpath 通常比手动搜索元素更有效。您可以使用以下内容查询:
.findByXpath('//div[@class="contentPane" and p[@id="second"]]')
这将找到第一个 DIV class "contentPane",其中包含 ID 为 "second" 的 P。