英文:
How to use scriptAll to find all the <div> with id or style attributes in Karate
问题
我试图编写一个测试用例来验证页面上的上下文。
有几个类似这样迭代的<div>元素
<form>
<div id="line1_R1" style="display:none;">text line 1 </div>
<div class="labelText nonLabelText pos y15 x1" style="white-space: nowrap; width:20.00ex;">text&nbsp;line&nbsp;1&nbsp</div>
<div id="line1_R2" style="display:none;">text line 1 </div>
<div class="labelText nonLabelText pos y16 x1" style="white-space: nowrap; width:20.00ex;">text&nbsp;line&nbsp;1&nbsp</div>
...
</form>
有没有办法提取所有id以<line1_R + index>开头的<div>元素中的文本并将它们合并在一起?或者是否可以使用<style>属性作为定位符?
非常感谢
英文:
I was trying to write a test case to verify context on the page.
There are several <div> iterating like this
<form>
<div id="line1_R1" style="display:none;">text line 1 </div>
<div class="labelText nonLabelText pos y15 x1" style="white-space: nowrap; width:20.00ex;">text&nbsp;line&nbsp;1&nbsp</div>
<div id="line1_R2" style="display:none;">text line 1 </div>
<div class="labelText nonLabelText pos y16 x1" style="white-space: nowrap; width:20.00ex;">text&nbsp;line&nbsp;1&nbsp</div>
...
</form>
Is there a way I could extract all the string from the div whose id starting with <line1_R + index> and merge them together? Or is it possible to use <style> attribute instead as locator?
Many thanks
答案1
得分: 1
以下是翻译好的内容:
我无法测试它,但类似这样的代码应该可以工作:
* def list = locateAll('form div', x => { let id = x.attribute('id'); return id ? id.startsWith('line1_R') : false })
* def data = list.map(x => x.text.trim())
这是相关文档链接:https://github.com/karatelabs/karate/tree/master/karate-core/#locateall-with-filter
英文:
I can't test it, but something like this should work:
* def list = locateAll('form div', x => { let id = x.attribute('id'); return id ? id.startsWith('line1_R') : false })
* def data = list.map(x => x.text.trim())
This is the relevant documentation: https://github.com/karatelabs/karate/tree/master/karate-core/#locateall-with-filter
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论