JQuery Error - $ is not defined

Hi, in order to get the value of this component via Jquery in Katalon I am using something like this:

 String script ="return \$(\"[prop='stampMods']\").find('input').is(':checked') ; "
 WebUI.executeJavaScript(script,[])

but I got this error:
Caused by: org.openqa.selenium.JavascriptException: javascript error: $ is not defined

could somebody help me to point out what it’s missing?

@hamen

Are you using Katalon Studio or Katalon Recorder? That looks like a Katalon Studio test case to me.

I will show you 2 codes as example:

Test Cases/visit_page_with_jquery

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser('http://demoaut.katalon.com/')
String script ="""return \$('h1').text();"""
def t = WebUI.executeJavaScript(script,[])
println(t)
WebUI.closeBrowser()

when I ran this, I saw the following message in the console:

2021-10-06 15:53:18.491 DEBUG testcase.visit_page_with_jquery          - 2: script = "return $('h1').text();"
2021-10-06 15:53:18.495 DEBUG testcase.visit_page_with_jquery          - 3: t = executeJavaScript(script, [])
2021-10-06 15:53:18.615 DEBUG testcase.visit_page_with_jquery          - 4: println(t)
CURA Healthcare Service

This worked fine.

Test Cases/visit_page_withoug_jquery

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser('https://example.com/')
String script ="""return \$('h1').text();"""
def t = WebUI.executeJavaScript(script,[])
println(t)
WebUI.closeBrowser()

when I ran this, I saw the following output in the console:

2021-10-06 15:55:02.912 DEBUG testcase.visit_page_without_jquery       - 2: script = "return $('h1').text();"
2021-10-06 15:55:02.914 DEBUG testcase.visit_page_without_jquery       - 3: t = executeJavaScript(script, [])
2021-10-06 15:55:03.478 ERROR c.k.k.core.keyword.internal.KeywordMain  - ❌ Unable to execute JavaScript. (Root cause: com.kms.katalon.core.exception.StepFailedException: Unable to execute JavaScript.
	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)
	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:26)
	at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword.executeJavascript(ExecuteJavascriptKeyword.groovy:42)
	at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword.execute(ExecuteJavascriptKeyword.groovy:37)
	at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.executeJavaScript(WebUiBuiltInKeywords.groovy:4873)
	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$executeJavaScript$0.call(Unknown Source)
	at visit_page_without_jquery.run(visit_page_without_jquery:5)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:430)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:421)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:400)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:392)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:273)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1633503294156.run(TempTestCase1633503294156.groovy:25)
Caused by: org.openqa.selenium.JavascriptException: javascript error: $ is not defined
        ...

It failed with an error messge “$ is not defined”.

This is the same as what @hamen encountered.

Observation

These 2 scripts are almost identical. The only difference is that these scripts visit different URLs.

Why the script Test Cases/visit_page_with_jquery passed? — Because the HTML of the http://demoauto.katalon.com/ has a line:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

Why the script Test Cases/visit_page_without_jquery failed? — Because the HTML of the http://example.com/ does not have a line of <script src=" url of jquery source code">.

Conclusion

@hamen

Your javascript code with $ assumes that the target HTML has <script src=" url of jquery source code">. If the HTML does not have it, then you will encounter an error of $ is not defined.

You have no way to inject jQuery into the target page by your Selenium test script.