如何使用scriptAll在Karate中查找所有带有id或style属性的

huangapple go评论79阅读模式
英文:

How to use scriptAll to find all the <div> with id or style attributes in Karate

问题

我试图编写一个测试用例来验证页面上的上下文。

有几个类似这样迭代的<div>元素

&lt;form&gt;
&lt;div id=&quot;line1_R1&quot; style=&quot;display:none;&quot;&gt;text line 1 &lt;/div&gt;
&lt;div class=&quot;labelText nonLabelText pos y15 x1&quot; style=&quot;white-space: nowrap; width:20.00ex;&quot;&gt;text&amp;nbsp;line&amp;nbsp;1&amp;nbsp&lt;/div&gt;

&lt;div id=&quot;line1_R2&quot; style=&quot;display:none;&quot;&gt;text line 1 &lt;/div&gt;
&lt;div class=&quot;labelText nonLabelText pos y16 x1&quot; style=&quot;white-space: nowrap; width:20.00ex;&quot;&gt;text&amp;nbsp;line&amp;nbsp;1&amp;nbsp&lt;/div&gt;
...
&lt;/form&gt;

有没有办法提取所有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

&lt;form&gt;
&lt;div id=&quot;line1_R1&quot; style=&quot;display:none;&quot;&gt;text line 1 &lt;/div&gt;
&lt;div class=&quot;labelText nonLabelText pos y15 x1&quot; style=&quot;white-space: nowrap; width:20.00ex;&quot;&gt;text&amp;nbsp;line&amp;nbsp;1&amp;nbsp&lt;/div&gt;

&lt;div id=&quot;line1_R2&quot; style=&quot;display:none;&quot;&gt;text line 1 &lt;/div&gt;
&lt;div class=&quot;labelText nonLabelText pos y16 x1&quot; style=&quot;white-space: nowrap; width:20.00ex;&quot;&gt;text&amp;nbsp;line&amp;nbsp;1&amp;nbsp&lt;/div&gt;
...
&lt;/form&gt;

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(&#39;form div&#39;, x =&gt; { let id = x.attribute(&#39;id&#39;); return id ? id.startsWith(&#39;line1_R&#39;) : false })
* def data = list.map(x =&gt; x.text.trim())

This is the relevant documentation: https://github.com/karatelabs/karate/tree/master/karate-core/#locateall-with-filter

huangapple
  • 本文由 发表于 2023年5月22日 00:14:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定