All setText inputs go into the same field despite different XPaths in Mobile Testing

Hello everyone,

I’m currently facing an issue when automating my mobile app testing with Katalon Studio. I use the following XPath format for all input fields:

//android.view.View[.//android.widget.TextView[@text='hint_on_textfield']]//android.widget.EditText

example for column Name / “Nama” :

//android.view.View[.//android.widget.TextView[@text='Nama']]//android.widget.EditText

However, the problem is that all the data I try to input into different fields (e.g., Name, Age, Address, etc.) always end up being entered into the same field, namely the “No HP” (Phone Number) field.

Could someone please explain why this happens? Am I using the wrong XPath approach, or is there something I need to adjust in my locator strategy?

my XPath hierarchy :

here is my test script :

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpointimport static com.kms.katalon.core.testcase.TestCaseFactory.findTestCaseimport static com.kms.katalon.core.testdata.TestDataFactory.findTestDataimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObjectimport static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObjectimport com.kms.katalon.core.checkpoint.Checkpoint as Checkpointimport com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKWimport com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobileimport com.kms.katalon.core.model.FailureHandling as FailureHandlingimport com.kms.katalon.core.testcase.TestCase as TestCaseimport com.kms.katalon.core.testdata.TestData as TestDataimport com.kms.katalon.core.testng.keyword.TestNGBuiltinKeywords as TestNGKWimport com.kms.katalon.core.testobject.TestObject as TestObjectimport com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUIimport com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windowsimport internal.GlobalVariable as GlobalVariableimport org.openqa.selenium.Keys as Keys
Mobile.startApplication(‘App-test\app-debug.apk’, true)

Mobile.tap(findTestObject(‘Object Repository/TC-02/android.widget.Button’), 0)

Mobile.tap(findTestObject(‘Object Repository/TC-02/android.widget.TextView - Pendaftaran’), 0)

Mobile.setText(findTestObject(‘Object Repository/TC-05/No HP’), ‘’, 0)

Mobile.setText(findTestObject(‘Object Repository/TC-05/Nama’), ‘Asep Surasep’, 0)

Mobile.setText(findTestObject(‘Object Repository/TC-05/Usia’), ‘21’, 0)

Mobile.setText(findTestObject(‘Object Repository/TC-05/Alamat Rumah’), ‘Jl. Jendral Sudirman N0.50’, 0)

Mobile.setText(findTestObject(‘Object Repository/TC-05/Nama Institusi Pendidikan’), ‘Universitas Testing’, 0)

Mobile.hideKeyboard() Mobile.setText(findTestObject(‘Object Repository/TC-05/Semester’), ‘5’, 0)

Mobile.hideKeyboard() 

Mobile.setText(findTestObject(‘Object Repository/TC-05/NPM’), ‘21123123’, 0)

Mobile.hideKeyboard() 

Mobile.setEncryptedText(findTestObject(‘Object Repository/TC-05/Sandi’), ‘p4y+y39Ir5PJb2ispxT0Ew==’, 0)

Mobile.hideKeyboard() 

Mobile.setEncryptedText(findTestObject(‘Object Repository/TC-05/Konfirmasi Sandi’), ‘p4y+y39Ir5PJb2ispxT0Ew==’, 0)

Mobile.hideKeyboard()

Mobile.tap(findTestObject(‘Object Repository/TC-02/Button Kirim Pendaftaran’), 0)

Mobile.closeApplication()

Thank you in advance for your help.

2 Likes

I think, Katalon Studio interpretes the xpath correctly as it is defined.

//android.view.View[.//android.widget.TextView[@text='Nama']]
    //android.widget.EditText

this will be interpreted to be the same as

//android.view.View[.//android.widget.TextView[@text='Nama']]
    //android.widget.EditText[1]

which will point the “no HP” (Pone Number) field.


If you what to achieve something different, you need to change the xpath.

For example, try:

//android.widget.TextView[@text='Nama']/parent::android.widget.EditText
2 Likes

Reason due to your XPath locator selecting the first matching EditText within a general hierarchy, instead of uniquely targeting each intended input field

Before running the full test, validate each locator by highlighting or printing the found element in Mobile Spy.

Sample:
//android.widget.TextView[@text=‘Nama’]/following-sibling::android.widget.EditText
//android.widget.TextView[@text=‘Usia’]/following-sibling::android.widget.EditText

I suggested this:

//android.widget.TextView[@text='Nama']/parent::android.widget.EditText

@dineshh suggested this:

//android.widget.TextView[@text=‘Nama’]/following-sibling::android.widget.EditText

The only difference is the xpath axes to use: parent:: or following-sibling::.

Which xpath is correct against the actual DOM?

… I am not sure. I don’t have the system to test in my hand, so that I can not try.

@daeri757

Please try and see.

1 Like

@daeri757

You should step back to the beginning and rethink. You shouldn’t use XPath for Mobile at all.

Why? , read:

  • With the XCUITest driver, XPath might be very slow. This is because every time we run an XPath query, the entire app hierarchy must be recursively walked and serialized into XML, which can take a lot of time, especially if our app has many elements.

The sample code attached in the original post contains 10 calls to findTestObject(testobject). If these testobjects have locators in XPath, your test will repeat the app-to-XML serialization 10 times. It would be terribly slow.

2 Likes

Hi, thank you very much for your suggestions. I really appreciate both of your answers. @kazurayam @dineshh

I’ve tried applying the solution, but unfortunately it still doesn’t work. In fact, when I changed my XPath to those lines of code, it caused an error when running the test case.

The main reason why I’m still using XPath instead of other locators such as Accessibility ID, className, or Android UI Automator, is because the app currently has no content-desc or resource-id for each input field. This makes me quite confused about how to test it properly.

From what I observed, Katalon seems to treat all objects with similar XPath like:

/android.view.View[.//android.widget.TextView[@text='Nama']]//android.widget.EditText

as the same object. As a result, it always selects the first empty “EditText”, regardless of the label (“Nama”, “No HP”, etc.), since there is no unique ID attached to each field.

So my question is: would it be better for me to contact the developer team to request adding unique IDs or accessibility properties for each field, or is there still another possible workaround/solution that I can try on my side?

Thank you in advance for your help! :folded_hands:

2 Likes

As you have observed, the Recorder and Spy tool of Katalon Studio are not complete. These tools quite often generates poor XPath expressions, sometimes wrong ones. So, you need to be able to improve (fix, change, rewrite, …) the generated locators manually. You need to be better skilled for XPath (or CSS Selector) than the tools.

Yes. Desperately you need to cooperate with the app developer team. You should ask them to update the IOS/Android app to be test-friendly.

I don’t think so.

1 Like

okay thank you for the advice

Did you come up with this pathway or did some system? It took me a bit to interpret the pathway as I generally start at some solid point and move to the object I want. As an example, this starts at the “View”, but instead I would start with the object that has the text of “Nama”. From here, as @kazurayam showed you, you move up to the “parent” of the “Nama” TextView and you are home. The pathway you have, starts at the parent or ancestor of the “Nama” TextView and then moves down, one or more nodes (the double slash) to find any of the TextView.

Perhaps, you can put the array on your EditText to indicate which of the ones you want, like:

//android.view.View[.//android.widget.TextView[@text='Nama']]//android.widget.EditText[1] // the first one

or go with the “parent” pathway as @kazurayam showed you:

//android.widget.TextView[@text='Nama']/parent::android.widget.EditText

This pathway looks soooo easy.

I’m not sure how many are on your team, but this pathway seems apparent and should be easy for your team to understand.

Let us know your solution or workaround if implemented

1 Like

If @daeri757 is going to contact his developer team to request adding unique IDs or accessiblility properties for each field, I suppose, their internal discussion and development will take long time: weeks or months. So we shouldn’t rush him.

1 Like

To all new comers to Katalon, let me tell you again:

I hope Katalon to write this in their official document clearly.

New comers to Katalon for IOS/Android Mobile app testing tend to waste their time and efforts while trying to utilize XPath for Mobile. They do so because the doc suggests it. It’s unfortunate.

One day they will find this forum topic. Then, I am afraid, they will get angry and say “you are too late to tell it.”

1 Like

We already mentioned it: Locator strategies for Mobile objects | Katalon Docs :nerd_face:

XPath: Search the app XML source using XPath. Use XPath locators only if there is no other way to locate the given element due to its performance issues.

1 Like

Your voice is too quiet to be heard. :ear_with_hearing_aid:

2 Likes

Katalon Studio GUI displays XPATH preset in the input field for Locator Strategy. The following screenshot clearly shows it.

This GUI design will let people believe that Katalon recommends XPATH as the best strategy for Moble Object.

An excuse in the doc is not enough. It will rather confuse users. :smiling_face_with_sunglasses:

1 Like

Hello everyone,

It has been quite some time since I first asked this question. First of all, I would like to sincerely thank everyone who took the time to respond and share insights regarding my issue.

After reviewing the suggestions and discussions here, I followed up by discussing the problem directly with the developer team. We identified that the root cause was the absence of unique identifiers, such as content-desc or stable IDs, on the input fields, which are essential for reliable automated testing.

Once these attributes were added and properly configured, the issue was resolved, and the test automation is working as expected.

Thank you again to everyone who kindly helped me understand and resolve this problem. Your support was greatly appreciated.

@kazurayam @dineshh @grylion54 @duyluong

3 Likes

I checked Katalon Studio v10.4.2, the latest version.

In the GUI, it still defaults to “XPATH” for Mobile TestObjects’s locator strategy.

But Katalon does not highly recommend “XPATH” in their official document.

@duyluong

Confusing, isn’t it?

2 Likes