No ID for my Android UI Testing - android

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.

Related

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?
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

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.

Android studio how to determine which layout is displayed

Lets assume that I have a pretty big Android app and run test build on device (emulator or real) and go to some specific screen.
Is there any way to determine which screen is currently displayed on device? I.e. I'm on MainActivity screen with activity_main.xml layout and I want to find there this info is displayed.
I couldn't find such thing in Logcat.
Try to keep a unique tag name with the class Name as given below
private static final String TAG = "UniqueName" + EachCalss.class.getSimpleName();
while debugging time search with uniqueName
I found an answer in a pretty unexpected place.
You can track your displayed screens via Chrome.
Go to
chrome://inspect/#devices -> inspect your device -> elements.
There you can find your layout elements, activities, fragments etc.

Remove app from running apps by swiping out using UI automator

I have to write a test case using UIautomator to swipe it out from the recent apps.I am trying below code but not sure what should be "id of the app".I tried running uiautomatorviewer and all apps are showing same resourceId.
uiDevice.pressRecentApps();
UiObject app = uiDevice.findObject(new UiSelector().resourceId("The id of the app"));
app.swipeLeft(100);
You should be able to find the app using text.
UiObject app = uiDevice.findObject(new UiSelector().text("appText"));
app.swipeLeft();
If you do a search by text, then you end up getting the element that the text is actually written on.
I'm not sure if you can swipe on that element, so if you need a parent element this question covered how to do that.

Android hierarchy for XPath to interact with specific item

I'm new to automatization, Android, Selenium, Appium and xpath, too. I know it's suck a great beggining.
I write tests for Android devices, but the application I have to test have a lot of costum views. I found out the best way to interact with these custom items is to put an "android:contentDescription" field in the Views. My only question is how to get access to the element with have a specified contentDescription? This is az android specific question, I'm not even sure that the content-desc is the field I'm looking for.
I have the hierarchy provided by Android UI Animator Viewer:
http://i.imgur.com/NUGc56o.png
The ways i've tried:
xpath: //*[contains(#android:contentDescription,'example text')]
I was able to get access by finding them as an ImageView, but as I mentioned I need to work with custom Views
My code looks like somtihng like this:
driver.findElementByXPath("//*[constains(#content-desc,'Login')]").click();
Thanks for the help!
You could also try using Accessibility labels or the UIAutomator locator strategy.
Here's Appium's documentation on those.
Your xpath is incorrect. It should be: "//android.widget.ImageView[#content-desc='Login']"
Here's some pseudocode of what you should do:
login_image = driver.findElementByXPath("//android.widget.ImageView[#content-desc='Login']"); // Gets you the WebElement
print login_image.getClass(); // Just for debugging, make sure it's not nil/null
login_image.click(); // Click on it!

Categories

Resources