1
  • def dropdownBtn = locate("//div[starts-with(@class,'DropdownWidget')]/descendant::span")
    • dropdownBtn.click()
    • delay(8000)
    • def dropdownOptions = locateAll("//li[@role='option']") Then karate.log('Number of dropdown options: ' + dropdownOptions.length)

I have to click the webElement one by one in Dropdown List.

  • def dropdownBtn = locate("//div[starts-with(@class,'DropdownWidget')]/descendant::span")
    • dropdownBtn.click()
    • delay(8000)
    • def dropdownOptions = locateAll("//li[@role='option']") Then karate.log('Number of dropdown options: ' + dropdownOptions.length)

1 Answers1

0

First, you can easily wrap some operations into a JS function and use karate.repeat(). Also if you do a locateAll() and return an array of elements, you can loop over the elements using something like data.forEach(x => <your code>)

But it is better to use the built-in functions such as waitUntil(). Here is an example:

* def delete = 
"""
function() {
  if (!exists('.border-bottom div')) {
    return true;
  }
  click('.text-end button');
}
"""
* waitUntil(delete)

How this works is as long as the function does not return a value, waitUntil() will loop. The click('.text-end button') is deleting the first row of records in the HTML. So the code above very neatly performs the loop and also exits the loop when there are no more records, and that is why we have the check for !exists('.border-bottom div').

Refer to the docs: https://github.com/karatelabs/karate/tree/master/karate-core/#waituntilfunction

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I have used this code to loop and performed operations in a JS function. I don't see any errors in reporting but while executing the script browser is not able to perform click. * def dropdownBtn = locate("//div[starts-with(@class,'DropdownWidget')]/descendant::span") * dropdownBtn.click() * def dropdownOptions = locateAll("//li[@role='option']") * def operation = """ function(option) { option.click(); var text = option.getText(); karate.log('Element text: ' + text); } """ * karate.forEach(dropdownOptions, operation) – Sreenija Reddy May 17 '23 at 09:00
  • @SreenijaReddy sorry I can't help more than this. maybe you need a delay after the click – Peter Thomas May 17 '23 at 09:41
  • @PeterThomas: If ii use the above code am getting below exception waitUntil(delete) js failed: >>>> 01: waitUntil(delete) <<<< org.graalvm.polyglot.PolyglotException: SyntaxError: Unnamed:1:16 Expected an operand but found ) waitUntil(delete) full code * driver 'https://www.saucedemo.com/' * def delete = """ function() { if (!exists('#user-name')) { return true; } click('#user-name'); } """ * waitUntil(delete) – Dinesh Tejaj May 23 '23 at 13:40
  • @DineshTejaj sorry I give up. I can't make it any more clearer that in the documentation here: https://github.com/karatelabs/karate/tree/master/karate-core#looping – Peter Thomas May 23 '23 at 13:45
  • @PeterThomas: Can you also please give one example on js funcation where we can click, input some values by passing locators – Dinesh Tejaj May 23 '23 at 13:46
  • @DineshTejaj here you go: https://stackoverflow.com/a/71907670/143475 – Peter Thomas May 23 '23 at 13:49