1

I was trying to write a test case to verify context on the page.

There are several 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 attribute instead as locator?

Many thanks

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
Jackson
  • 91
  • 1
  • 6

1 Answers1

1

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

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Thanks so much for the answer. This has perfectly resolved the issue. – Jackson May 21 '23 at 20:43
  • Hi Peter, the content of this is actually cut off and across multiple pages. I have attached further details on https://stackoverflow.com/questions/76301962/how-to-repeat-customized-function-with-conditional-logic-in-karate-ui. Could you please advice any solution on this? Thanks in advance. – Jackson May 21 '23 at 21:22