Arun K Dey said:
crokatalontest said:
John said:
How to select xpath for a checkbox with dynamic ID and Name?
<input type="hidden" name="user._17092"><input type="checkbox" **name="user.17092" id="user.17092"** class="" style="">
<a href=".......11641/users/17092/edit" style="">john.ty+fordeletion@email1.com</a>
</td>
xpath generated is → id(“user.17092”)
and this should be dynamic.
I assume you want to check/uncheck your checkbox.
Since it is changing you are not able to.
I have done it like this:
1. Import this into your script: import com.kms.katalon.core.testobject.ConditionType
2. use jquery to get values of your elemet attributes:
I cant check if this selectoris ok, but you can try it in your chrome console.
After you find right selector write it in your script
newName = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“name”)’, )
newId = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“id”)’, )
Pleas validate selector above.
After you get your values then you go to step 3
myTestObject = new TestObject(“customObject”)
List properties = new ArrayList()
properties.add(new TestObjectProperty(“name”, ConditionType.EQUALS, newName))
properties.add(new TestObjectProperty(“id”, ConditionType.EQUALS, newId))
//Set object
myTestObject.setProperties(properties)
//and finally check your box
WebUI.check(myTestObject)
Hi,
Can you please explain step 2 and 3 in brief, and how to use of the following two lines:
newName = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“name”)’, [])
newId = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“id”)’, [])
_Can you please help me out?
_
Sorry for my late response.
When you load your page that you are testing you need to right click on your checkbox (chrome or firefox browser) and select option INSPECT.
After that you can see html code of your element.
It is something like this:
Now if you look @ step 2 i got the value for “name” and i got value for “id”
newName will return SomeNAme
and newID will return SomeID
ID is unique and that means you only need to get ID.
You don’t need to fetch NAME value.
_newId = WebUI.executeJavaScript(‘return $(“input[type=“checkbox””]").attr(“id”)’, )
_newId will have value “SomeId” without quotes of course.
If you don’t get the NAME value than you need to skip this step:
properties.add(new TestObjectProperty(“name”, ConditionType.EQUALS, newName))
In step 3 now we create new test object (you don’t need to have your test object in object repository with this) and that new object is going to have
attributes of your checkbox.
And then you do whatever you need to do with this new object (click, check, uncheck whatever).
With this method you don’t need any object in your object repository.
You can do this for any object on your page.
You just need to get your element ID value, create new object, and do whatever you want to tho with it
And you don’t need to worry if your checkbox id (or any other id or any other attribute) constantly changes because you will always get the right value for “id” and “name” (or any other) attribute.
checkbox.PNG