I need to create a the function below to call in a test case. I believe this needs to be created as a keyword, however, I am not a coder/scripter and needs guidance on how to format this in the keyword function.
function contains(selector, text) {
const elements = document.querySelectorAll(selector);
return Array.from(elements).filter(function(element) {
return RegExp(text).test(element.textContent);
});
}
If you only need to call it in one test case (like you said above) you donât need to create a keyword. However, if you think you might use it in more test cases, then yes, a keyword would be a good idea.
Just so you know and understand, the code you provided is JavaScript, not Groovy.
Katalon can only use JavaScript by asking the browser to execute it â I normally use WebUI.executeJavaScript() to do that.
In your Test Case, add this code:
// Note the js variable uses THREE single quotes!
String js = '''return contains(arguments[0], arguments[1]);
function contains(selector, text) {
const elements = document.querySelectorAll(selector);
return Array.from(elements).filter(function(element) {
return RegExp(text).test(element.textContent);
}); }
'''WebUI.executeJavaScript(js, Arrays.asList(your_selector, your_text))
The NEW code is highlighted in bold. You will need to replace your_selector and your_text with the selector and text in your test.
Let me know if youâd like to make this a keyword or a method you can call from any test case.
Thank you for your response, yes ideally, I would like make it a key word and call if from any test case. For more information, I will need to do the following in a specific test case
Read the below to begin with. See if that helps in creating the ânewâ sheet you need for your keywords. Then itâs a simple copy and paste with some amending to finish it up.
I have the keyword create with this
String js = âââ
return contains(arguments[0], arguments[1]);
function contains(selector, text) {
const elements = document.querySelectorAll(selector);
return Array.from(elements).filter(function(element) {
return RegExp(text).test(element.textContent);
}); }
âââ
I believe the WebUI.executeJavaScript(js, Arrays.asList(your_selector, your_text)) needs to be in the test case that needs to call the method.
I was also hoping for help with the following steps for 1 of my test cases after I call the method:
//Find the contains helper
const label = contains(âlabelâ, âCurrent stateâ)[0]
//Find the âOpenâ button
const control = label.closest(".MuiFormControl-root");
//Click the first item
const openButton = control.querySelector("[title=âOpenâ]").click()
Back to your keyword, can you display your âcallingâ statement and the âheaderâ of your @Keyword so we can review and agree its proper? This is the âamendingâ that I mentioned in my prior post.
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import org.openqa.selenium.JavascriptExecutor as JavascriptExecutor
import internal.GlobalVariable
public class function {
String js = âââ
return contains(arguments[0], arguments[1]);
function contains(selector, text) {
const elements = document.querySelectorAll(selector);
return Array.from(elements).filter(function(element) {
return RegExp(text).test(element.textContent);
}); }
âââ
}
Just a couple of changes perhaps. On the âIntroduction to Custom Keywordâ link that I gave in my last post, there is an example of a custom keyword. Notice the @Keyword at the top. This is necessary for your method to become a Custom Keyword!
@Keyword
def keywordName(parametersâŚ) {
// enter your code here
// you can use either Groovy or Java
}
Also, notice the â(parametersâŚ)â part in the custom keyword example above. You need to âpass intoâ the method here with the parameter list you will have in your âcalling statementâ, like
So, give your Custom Keyword a âmethod nameâ in place of the keywordName in the example above, like I used âMethsâ --but be more descriptive of what your method is supposed to do (and not âfunctionâ either on basic principle).
Hmmm
public class function {
@Keyword
def keywordName(String selector, String text) {
String js = '''
return contains(arguments[0], arguments[1]);
function contains(selector, text) {
const elements = document.querySelectorAll(selector);
return Array.from(elements).filter(function(element) {
return RegExp(text).test(element.textContent);
}); }
'''
}
}
And letâs make a call out to @Russ_Thomas to assist you for your âreturnâ type and parameters.
Sorry for the delay, I completely missed this. (Thanks for the shout out @grylion54)
Make a new package, call it com.davina.meyer
â â or com.your_company_name if you prefer
In the new package, make a new class (custom keyword in Katalon) call it utils
Make the code inside the utils class look like this:
package com.davina.meyer
// imports here
public class utils {
static boolean contains(String selector, String text) {
String js = '''
return contains(arguments[0], arguments[1]);
function contains(selector, text) {
const elements = document.querySelectorAll(selector);
return Array.from(elements).filter(function(element) {
return RegExp(text).test(element.textContent);
});
}
'''
return (boolean) WebUI.executeJavaScript(js, Arrays.asList(selector, text))
} // contains method
} // class utils
In your test caseâŚ
import static com.davina.meyer.utils.*
// other imports here
// Test case steps here
//call your new contains method
if(contains(your_selector, your_text)) {
println "It works!"
} else {
println "nope, not found"
}
You donât need @Keyword because weâre using static code.
Caveat. I did not vet or test the code inside the contains() method - thatâs your code
Everything passes until I try to click on the last def. I can tell that it is not correct because the click() is underlined
Error: Reason:
groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.click() is applicable for argument types: () values: []
Possible solutions: clear(), clear(), clear(), clone(), wait(), size()
I need to click on the button to open the dropdown list, any suggestions?
One concern I have is that your method, contains, returns a boolean data type? If you want label to be a WebElement, then you are going to have to change what you have, like maybe:
@Russ_Thomas
I donât disagree that I donât understand. Again, I am new to automation and scripting. The code I have was provided to me by a developer, I did not come up with it.
I have the keyword configured like your example and import it in my test case.
What I am trying to is find the label of Hometown state, the go to related field and click the open button
start at the label, âHometown stateâ, then move to the first âsiblingâ <div> right below, then down a level to the first <div> with class=âMuiAutocomplete-endAdornmentâ, then down to the second button, with title= âOpenâ