Appium+ WebdriverIO finding element issues - android

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?

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
You can then press this button
then select XPATH and add this //*[#content-desc="input-email"] selector
hit Search which will lead to this
So in your case, if you would use or something like above or just this
$('//*[#content-desc="helpme"]').setValue('test')
then it should work

Related

Appium does not find element until it is tapped on mobile device. How to find element for which it does not have focus?

I am new for automation testing.
I am trying to automate UI and want to tap on humburger menu.
I tried it with
1. MobileElement el1 = driver.findElement(By.xpath("//*[#content-desc='" + "Menu" + "']"));
el1.click();
2. MobileElement el1 = driver.findElement(By.name("Menu)"));
el1.click();
3. MobileElement el1 = (MobileElement) driver.findElementByXPath("//android.widget.ImageButton[#content-desc=\"Menu\"]");
in all the cases i got error for element not found.
But when i tap manually on menu within test befor el1.click() get executed then test get run successfully. It is just like menu element get focused and appium find it.
So how to execute this without tap or getting focus?
Try to find the element by its name. In UI Inspector search before with name or any other locator. If it searches successfully with few attempts. Sometimes element has dynamic identification.
Can you try your code again but this time set the capability automationName to uiautomator2? Your problem sounds like the ones I have sometimes and moving from UIA1 to UIA2 (uiautomator2) solved that. UIA2 is however a bit more slow detecting elements IMO.

TextField ellipsis nativescript

I'm developing a Nativescript app, and in some devices the hint is bigger than the input, IOS add three dots at the end (...) like an ellipsis, but android only cut off the hint.
I tried the following code:
let shareInput: TextField = this.page.getViewById<TextField>('share-input');
shareInput.android.setEllipsize("end");
I'm getting an error like this:
java.lang.Exception: Failed resolving method setEllipsize on class
android.widget.EditText
I red some android issues and tried to use setSingleLine method but it does not work.
Any help is welcomed.
So this method is what you need (and are using :) - https://developer.android.com/reference/android/widget/EditText.html#setEllipsize(android.text.TextUtils.TruncateAt)
your argument is incorrect, it isn't expecting a string but an ENUM.
So try setEllipsize(android.text.TextUtils.TruncateAt.END)
And if you're going to be setting this on multiple fields I would add a var for the TruncateAt enum like const TruncateAt = android.test.TextUtils.TruncaeAT then just use that const/var in the argument like (TruncateAt.END) to save on repeating yourself.

No ID for my Android UI Testing

I would like to write some UI Test on my Android application in order to take automated screenshots.
My application is written with React-Native.
In order to write my tests in need to know the resource-id of my component but as you can see in this screenshot i can't see any resource-id with ui Automator Viewer in this React-Native example app.
Look this picture.
I would like to know if there is a way to give some IDs to my components so I can write some test or if there is another way to select my components.
Note : I'm trying to write Espresso Test.
The testID only works on iOS, and is not mapped to the resource-id in Android.
You can specify an accessibilityLabel to your component. In uiautomatorviewer it shows up as content-desc
To be cross-platform, you should specify both:
<Text style={styles.welcome} accessibilityLabel="MyId" testID="MyId">
Welcome!
</Text>
You can then search for it using something like webdriver with the tilde selector browser.waitForExist("~MyId")
First of all, ID of each UI Component of Android objects has to be defined in the resource(res) folder as layout XMLs(you need to make sure you have defined it in the right tags) and to obtain the IDs in the Android please follow the steps:
1. Launch Android Studio, Click on "Android Device Monitor" icon next to "SDK Manager"
2. Connect your device and launch the application
3. Select your device name from the list of devices on the left panel
4. Click on Dump "View Hierarchy for UI Automate"
5. Now you can hoover over any view on thde device dump and can see Resource-d in the node details in the right column.
Note: You can also take screenshots
1)First thing first if its your project you can go to the layout and add id to these component and then take screenshot using uiautomator viewer you will find your resource id
2) If option 1 is not possible(that's highly unlikely to happen) you can still test it as from your screenshot i can see only one edittext box so using uiauatomator you can test it on the basis of the class . Here below is the code:
private static UiDevice mDevice;
func(){
mDevice = UiDevice.getInstance(getInstrumentation());
UiObject xyz=new UiObject(new UiSelector().**getClass**(android.widget.EditText));
xyz.click();
xyz.setText("abcdef");
}
// I am not sure its getclass() or getclassName() check both option
//xyz.click() is necessary as in uiautoamtor you won't be able to right text
in a textbox until and unless it is selected
Yes there is! You can pass a testID prop to any component that implements the <View /> props, as documented here.
testID string
Used to locate this view in end-to-end tests.
See also this very informative blogpost about UITesting in React Native. It is about iOS testing, but the javascript part is going to be the same for Android.

MonkeyTalk Android Detect String Containing \n for Button Tap

I am using MonkeyTalk to automate some user test cases for my Android app. Everything is working fine except for when I try and detect a button containing this string:
"Connect\n(Code Required)"
I get this error:
FAILURE: Unable to find Button(Connect\n(Code required))
If I change the button to "Connect" and perform a tap on that value MonkeyTalk has no trouble, but something about the line break must be throwing it off.
After some searching I found this thread that confirmed my suspicious about the line break. There was one suggested fix here, to set the default encoding to UTF-8 (Select the Project > File > Properties > Resources)
However this did not work for me.
I have also tried to find the button using a wildcard like so:
"*(Code Required)"
But this does not seem to be supported either.
Maybe there is an alternative line break character I could use?
Thanks in advance for the help!
Maybe there's a carriage return in there? I know in most text editors a new line actually consists of (carriage return)+(newline).
Also take a look at this:
TextView carriage return not working
Also, depending on how flexible your requirements are, you could use the #N MonkeyId replacement to get the Nth button.
IN javascript you can use below command
app.button("buttonname").tap(x, y);
Use android:contentDesxription="your_component_id" in your view xml file definition or view.setContentDescription("your_component_id"); directly on view in code to make it easy to access in MonkeyTalk.

Appium: Android Resource ID: translates to what in their selenium integration?

So for example I have:
wd.findElement.By(name("searchBttn"));
the resource-id in the ui xml screen cap is
com.aut.android:id/searchBttn
and Appium can't find the item. I have tried with ID and Name. Is there something else here i could try is there a direct line for line correlation list somewhere that i am missing?
You can find an Android item in Appium by Resource ID, but you have to specify the entire package name along with the resource id:
wd.findElement.By(id("com.aut.android:id/searchBttn"));
You can fetch element by few more way other than name or id is not available
Can find element find by using resource-id like below example:
driver.findElement(By.xpath("//*[#resource-id='login']")).click();
driver.findElement(By.xpath("//*[#id='cash']")).click();
Here: com.android.calculator2:id/digit5 is complete resource id and can use like this:
driver.findElement(By.id("com.android.calculator2:id/digit5")).click();
Can find by class name:
driver.findElement(By.className("android.widget.Button")).click();
At least name works for me. I'm trying to find out how it works for ID, but you can look at the following.
Example: If the button text is set to "testbtn", then you can access it from Appium with the following command:
driver.findElement(By.name("testbtn")).click();
This works for me, try it.
EDIT:
As one of the Appium devs stated here, it currently seems to be not possible to access elements by ID on Android.
EDIT2:
This feature is now implemented! As I found out, you can now access elements from Android by ID using selendroid (it seems to be a sub part of Appium).
In your test class you need to specify capabilities.setCapability("device", "selendroid"); when setting up instead of capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
Now you can just access a button with
driver.findElement(By.id("testbtn")).click();
You can find the official documentation from Selendroid here
I had the same problem in python where I couldn't find the elements that had the id attribute even though I could see them in the appium inspector.
Some of the apps have multiple contexts that the driver can handle like NATIVE_APP & WEB_VIEW so I had to switch the context of the driver to WEB_VIEW so it could find the elements with the driver.find_elements_by_id("")(python syntax)
Documentation on how to switch contexts with code examples -
http://appium.io/docs/en/writing-running-appium/web/hybrid/index.html

Categories

Resources