'Appium+ WebdriverIO finding element issues

I created a small test android app which has a text box and a button and am able to build this and launch on a real device (android) using WebDriverIO.

However, when I try to select elements I can't locate them.

Example: the text field has the following properties: id: editTextTextPersonName

input type: textPersonName

contentDescription: @string/fldName1 (which is the string "helpme" in the xml)

Using UIAutomatorViewer I can see the field has the following:

resouce-id** = com.example.myfirstapp:id/editTextTextPersonName

package = android.widget.EditText

class = android.widget.EditText

content-desc: helpme

However, I cannot locate this using WebDriverIO.

I have tried $('~helpme').setValue('test') but that doesn't work.

Does anyone have any suggestions?



Solution 1:[1]

To be honest, you're doing everything correct. AFAIK there is an issue with the $('~helpme') selector within WebdriverIO/Appium not being able to find the correct element / failing on Android.

What I did in the past, especially if I needed to use cross-platform locators, was that I created a method like this

const locatorStrategy = (selector: string): string => {
  return driver.isIOS ? `id=${selector}` : `//*[@content-desc="${selector}"]`;
};

which could be used like this

$(locatorStrategy('input-email'));

You can also validate your selector with Appium Inspector. This is how Appium Inspector looks like

enter image description here

You can then press this button

enter image description here

then select XPATH and add this //*[@content-desc="input-email"] selector

enter image description here

hit Search which will lead to this

enter image description here

So in your case, if you would use or something like above or just this

$('//*[@content-desc="helpme"]').setValue('test')

then it should work

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wswebcreation