将因LocateAll()而收到的元素数组转换成空手道中的文本? [英] Convert array of elements received as a result of LocateAll() into text in Karate?
问题描述
我正在尝试从表中获取项目名称的列表.在哪里可以使用locateAll()方法获得元素列表,但是当我尝试将它们转换为文本值时,结果为空.
I am trying to get the list of project names from a table. Where by using locateAll() method I am able to get the list of elements but when I try to convert them into text value the result is null.
* def ProjectNames = locateAll("//div[@id='Projects']/@somePath")
* print ProjectNames
以上代码显示
[DriverElement@aef32g2
DriverElement@ahf38g2
DriverElement@ayf12gj
DriverElement@ae032f2]
但是期望得到如下结果:
But expectation is to get result as below:
[Project1
Project2
Project3
Project4]
我尝试过- *打印ProjectNames.text.trim()
,但这什么也不显示,并且步骤已通过.相反,当我针对特定索引值执行它时,它将显示该 * print ProjectNames [0] .text.trim()
的文本.我要如何为收到的完整清单做准备?预先感谢!
For which I tried - * print ProjectNames.text.trim()
but this displays nothing and step is passed. Instead when I execute it for particular index value it displays the text for that * print ProjectNames[0].text.trim()
. How can I do it for complete list received?
Thanks in advance!
推荐答案
给出以下HTML:
<body>
<div>first</div>
<div>second</div>
</body>
如果您有任何数组,则可以在该数组上进行 map
转换.请注意,我在空手道1.0中使用了新的JS引擎:)
If you have an array of anything, you can map
over the array to transform it. Note that I'm using the new JS engine in Karate 1.0 :)
* def temp = locateAll('div')
* def vals1 = temp.map(x => x.text)
* match vals1 == ['first', 'second']
做第二件事的第二种方法是 scriptAll()
,请参考文档:
And a second way to do what you need is scriptAll()
, refer the docs: https://github.com/intuit/karate/tree/master/karate-core#scriptall
* def vals2 = scriptAll('div', '_.textContent')
* match vals2 == ['first', 'second']
这篇关于将因LocateAll()而收到的元素数组转换成空手道中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!