I am trying to execute a JavaScript from karate feature file for implementing a dynamic wait of web element on browsers
As described in documentation, I am using script method to execute the script below script.
* def waitForElementToDisplay =
"""
function (selector, checkFrequencyInMs, timeoutInMs) {
var startTimeInMs = Date.now();
(function loopSearch() {
if (document.evaluate(selector, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE,
null).singleNodeValue != null) {
return ;
}
else {
setTimeout(function () {
if (timeoutInMs && Date.now() - startTimeInMs > timeoutInMs)
return;
loopSearch();
}, checkFrequencyInMs);
}
})();
}
"""
I have tried directly calling this js using script method, * script(waitForElementToDisplay("//*[text()='Show card number']",1000,10000)) inside my feature file but I am getting below error
109 * script(waitForElementToDisplay("//*[text()='Show card number']",1000,10000))
14:33:22.972 src/test/java/tests/android/imageText.feature:109
* script(waitForElementToDisplay("//*[text()='Show card number']",1000,10000))
js failed:
>>>>
01: script(waitForElementToDisplay("//*[text()='Show card number']",1000,10000))
<<<<
org.graalvm.polyglot.PolyglotException: ReferenceError: "document" is not defined
- <js>.loopSearch(Unnamed:4)
src/test/java/tests/android/imageText.feature:109