My Mobile xpath is not working

Hi all,I am new to Katalon
I try to download app in play store.
In Katalon studio, I use spy mobile to get the object. And it work properly.

Then, problem is when I choose the way detect object by xpath , and it will show me can’t find the object.
What should I do?
Because I want to do test on many kind of device, so I can’t choose the way detect by text.I think xpath is better and also absolutely on most device.

Does anyone know what wrong with xpath?
Maybe my idea is wrong, could you explain it?

Thank you

Hi @hoihoi000a,

You’re correct that xpath is a good way to find elements. One of the advantages of Katalon Studio is that it will generate an xpath for you from the selected properties of the Test Object. So in the first screenshot, you can choose class and text (I would advise against selecting instance) and Katalon will create an xpath when it runs the tests. You don’t have to select the xpath property - in fact, that could lead to more problems and brittle tests.

Here is a screenshot of one of my Test Objects and the xpath in the dark grey area at the bottom that it will automatically create for me:

Hope this helps,

Chris

Thank you Chris
It’s helpful.

Cheers
Newman

1 Like

Hi @Chris_Trevarthen:

Katalon is not detecting mobile object when it have same name, value and label.
In my mobile app there are 5 questions on the page and I have to answer then by clicking Yes or No radio button. In object property all these Yes and No have same Text, label and Value. So katalon is clicking on the first found element repeatedly and not clicking on the rest 4 answers. please suggest how to proceed with this problem. Attaching object property for one of the Yes radio button.

@katalon_qa_tester : Can you please help me in above case.

Hi @shitaltadas,

It sounds like you need to get a collection of all of the “Yes” buttons and loop through them to set each one individually.

You could create and edit test objects for each of the “Yes” buttons in the test code:

// Create the base object
TestObject yesObject = new TestObject("Yes Button")

// Set the object so that it refers to the first "Yes" button
yesObject.addProperty("xpath", ConditionType.EQUALS, "//*[@type = 'XCUIElementTypeStaticText' and @name = 'Yes'][1]", true)

// Tap the first "Yes" button
Mobile.tap(yesObject)

// Set the object so that it refers to the second "Yes" button
yesObject.addProperty("xpath", ConditionType.EQUALS, "//*[@type = 'XCUIElementTypeStaticText' and @name = 'Yes'][2]", true)

// Tap the second "Yes" button
Mobile.tap(yesObject)

Another option would be to use a library I wrote with some convenience methods for accessing individual objects out of an entire group. Specifically, it might be helpful for you to look at the section for finding an element by index.

One thing to note is that for the the library to work effectively, it’s expected that you structure your Object Repository in a certain way.

Hope this helps,

Chris

1 Like

@Chris_Trevarthen : Above code didn’t worked… it clicked on first Yes radio button but didn’t clicked on Second Yes radio button. Will try with Library… But for this I need to structure my object repository as you mentioned.

Hi @shitaltadas,

I think that calling addProperty to an existing object isn’t replacing the property, so we can’t reuse the first yesObject. I also had an error in the xpaths that I gave you before - it’s better if they are wrapped in parentheses and then we access their array index.

Here’s some revised code:

// Create the base object
TestObject yesObject = new TestObject("Yes Button")

// Set the object so that it refers to the first "Yes" button
yesObject.addProperty("xpath", ConditionType.EQUALS, "(//*[@type = 'XCUIElementTypeStaticText' and @name = 'Yes'])[1]", true)

// Tap the first "Yes" button
Mobile.tap(yesObject)

// Create the second yes object
TestObject yesObject2 = new TestObject("Yes Button")

// Set the object so that it refers to the second "Yes" button
yesObject2.addProperty("xpath", ConditionType.EQUALS, "(//*[@type = 'XCUIElementTypeStaticText' and @name = 'Yes'])[2]", true)

// Tap the second "Yes" button
Mobile.tap(yesObject2)

Hope this helps,

Chris