Katalon Recorder Javascript random click


click | javascript{[‘id=QR~QID42~16~1’,‘id=QR~QID42~16~2’,‘id=QR~QID42~16~3’,‘id=QR~QID42~16~4’,‘id=QR~QID42~16~5’,‘id=QR~QID42~16~6’,‘id=QR~QID42~16~7’][Math.floor(Math.random()*7)]}

How can I make this work in Katalon Recorder?Thanks.


Try this.

Command | Target | Value |
runScript | window.linkArray = ["id=QR~QID42~16~1", "id=QR~QID42~16~2", "id=QR~QID42~16~3", "id=QR~QID42~16~4", "id=QR~QID42~16~5", "id=QR~QID42~16~6", "id=QR~QID42~16~7"]  | |
runScript | window.linkToClick = window.linkArray[Math.floor(Math.random()*window.linkArray.length)] | |
runScript | return window.linkToClick | linkToClick |
click | ${linkToClick} ||

Note that you could combine all the runScript commands into one line separated by semi-colons, but I find it easier to read and understand if they are on separate lines.

Also note the use of the window.linkArray.length instead of the hard-coded number. This just makes it easier to maintain if you add or remove values from the array.