JavaScript executor

Hello,

I want to use jQuery selector to get objects from html instead of using xpath. I’ve found some scripts in web, but when I use them, I am getting an error.

This is my JavaScript Executor:

public class Java {
	@Keyword
	def executeJavaScript(String javascript) {
		((JavascriptExecutor) DriverFactory.getWebDriver()).executeScript(javascript)
	}
}

And this is the test case:

String myJsScript = "jQuery('div.sc-bdVaJa sc-bwzfXH ldFBFf').text()"
def k = CustomKeywords.'apilayer.Java.executeJavaScript'(myJsScript)

println k

I’m getting error like this:

Reason:
groovy.lang.MissingPropertyException: No such property: CustomKeywords for class: custom.Script1559547421027

I want to get inner text of that div.

Now, I’m getting this error:

Test Cases/sda FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.annotation.Keyword.apilayer.Java.exeJS() is applicable for argument types: (java.lang.String) values: [jQuery('div.sc-bdVaJa sc-bwzfXH ldFBFf').text()]

what did you change it dosen’t look like the same code as before (the same code structure)
how are excuting the command ?

public class Java {
	@Keyword
	def exeJS(String javascript) {
		WebDriver driver = DriverFactory.getWebDriver()
		JavascriptExecutor jse = (JavascriptExecutor)driver;
		jse.executeScript(javascript)
	}
}

TC:

String myJsScript = "jQuery('div.sc-bdVaJa sc-bwzfXH ldFBFf').text()"
def k = CustomKeywords.'apilayer.Java.exeJS'(myJsScript)

println k

i see no problem
did you try adding the keyword using the manual mode ?

What’s weird is that it’s considering it as a katalon builtin keyword normally it should say

No signature of method: apilayer.Java.exeJS()

Yes, I did but output is the same.

Test Cases/sda FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.annotation.Keyword.apilayer.Java.exeJS() is applicable for argument types: (java.lang.String) values: [jQuery('div.sc-bdVaJa sc-bwzfXH ldFBFf').text()]

Do you have any Ideas ?

I have no idea why :thinking: try this

public class Java {
	@Keyword
	static void exeJS(String javascript) {
		WebDriver driver = DriverFactory.getWebDriver()
		JavascriptExecutor jse = (JavascriptExecutor)driver;
		jse.executeScript(javascript)
	}
}

or this

public class Java {
	@Keyword
	static void exeJS(def javascript) {
		WebDriver driver = DriverFactory.getWebDriver()
		JavascriptExecutor jse = (JavascriptExecutor)driver;
		jse.executeScript(javascript)
	}
}

or this

public class Java {
	@Keyword
	def exeJS(def javascript) {
		WebDriver driver = DriverFactory.getWebDriver()
		JavascriptExecutor jse = (JavascriptExecutor)driver;
		jse.executeScript(javascript)
	}
}

try calling it like this

def myJsScript = "jQuery('div.sc-bdVaJa sc-bwzfXH ldFBFf').text()"
def k = CustomKeywords.'apilayer.Java.exeJS'(myJsScript)

i don’t see why would the brackets brake it they are not a special character but try this

def myJsScript = "test"
def k = CustomKeywords.'apilayer.Java.exeJS'(myJsScript)

you can try to rename your class but i don’t see why the name would brake the code
you can also try using another package name

Now, I get an error: jQuery is not defined.

Also, I’ve found WebUI.executeJavascript keyword, but it does not runs, says: Unable to execute JavaScript.

Suggestion for point 2: JavaScript is not ready yet.

Suggestion for point 1: jQuery is not loaded and ready yet.

Be sure that jQuery is loaded and ready. If your page uses jQuery then it is highly likely that jQuery will become very busy the moment it is ready. JavaScript is single-threaded. jQuery uses JavaScript. If you’re trying to use JavaScript/jQuery the moment jQuery is ready, you might create a race condition.

So, be patient. Slow your test down and wait for things to be ready.

When I use WebUI.waitForjqueryload is says that: the page does not use jquery.

P.S
Can you please see my other question ? :smiley:

Well, if the page doesn’t use it, you don’t need to test it.

?

No, I want to use JQuery selector instead of xpath.

This is my other question, WebUI.click with xpath from webelement

You said…

If jQuery is not present, you should not try to use it in your tests.

Note: You could inject jQuery into the page from your test case. But that would be a very bad thing to do.

My advice: DON’T DO IT.

You can use “jQuery” selectors without using jQuery:

// Using jQuery:
$("#my-button").click()

// Using standard DOM APIs
document.querySelector("#my-button").click();

Oh, I understand now, thanks.

What about my other question ? Can you help me with that ? I’ve created new topic.