Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get IDs, names or x paths for ui elements used in a mobile app (Android/iOS) for mobile automation testing?

I want to know how can I get IDs or names of UI elements used in a mobile application that are used in mobile automation testing like appium, monkey-talk, xamarian etc.

like image 743
Mohsin Awan Avatar asked Oct 31 '16 12:10

Mohsin Awan


People also ask

How can get XPath in mobile app?

Now we got one more tool to find XPath for both android and iOS application. Next, you have to connect your android/ios device(for android adb setup has to be done) and run the below command to start Macaca inspector. Once inspector get started you can view the all the mobile element properties with XPath.

How do I find the XPath of an element in Appium?

Use the Spy icon button in order to get the Native/Web properties of all the objects on the screen. The easiest way to create a simple XPath query is by marking the wanted properties of an element (one or multiple properties can be used), right-clicking on them and then clicking on Copy XPath.

How do I find my element ID in Appium?

In the Appium version 1.0 onwards, findElementByTagName is deprecated. Show activity on this post. You can find the ID of an element using android uiautomatorviewer. just go to your SDK tools folder D:\Android\android-sdk\tools, here you can find uiautomatorviewer.


2 Answers

What I explain is for my system (Windows 7), but my explanations should be easily translateable to other systems too.

Prerequisites:

  • When you want to do Android automation testing you should always install Android SDK, so I assume that you have already done so. Lets call the path to the sdk folder <ANDROID_HOME> (including the sdk folder itself)
  • Run <ANDROID_HOME>/SDK Manager.exe. Select the latest Android SDK Tools and Android SDK Platform-tools and install them
  • Connect a device you want to test on:
    • Either run an emulator - it is automatically connected in your system
    • Or connect a physical device and install the proper USB drivers. You cna verify the device is connected with running the command <ANDROID_HOME/platform-tools/adb.exe devices - if there is at least one device in the list you are good to go.

Once you have all that:

Run <ANDROID_HOME>/tools/uiautomatorviewer.bat. this will open a screen that allows you to take screenshots of the conencted device. See below image:

enter image description here

The screenshot is taken via clicking the button under the purple rectangle, whcih I added artificially. The red one is added by the tool, because I hover the element of interest. You can observe the elements properties on the right. These are usually the properties you would use for testing native application.

If you want to test elements that are loaded in WebView then it would be better if you use Chrome remote debugging for obtaining the correct selectors.


Footnote:

As I see you are making the very first steps in mobile automation testing, which is an area I am pretty interested into, may I suggest you take a look at the ATMOSPHERE Android test automation framework. Disclaimer: I am one of its creators. Still - it is freely available and open sourced. We also beleive it provides capabilities not supported in other frameworks and is easy to start with so I hope it will be useful for you!

like image 72
Boris Strandjev Avatar answered Oct 17 '22 05:10

Boris Strandjev


C#

In my case this piece of code resolved my issue:

    private IApp _app = ScenarioContext.Current.Get<IApp>("Application");

    private readonly ILoginScreen _loginScreen;
    private readonly IMainMenuScreen _mainMenuScreen;

    public Login(ILoginScreen loginScreen, IMainMenuScreen mainMenuScreen)
    {
        _app.Repl();
        _loginScreen = loginScreen;
        _mainMenuScreen = mainMenuScreen;
    }

I'm using Gherkin language using Xamarin in which Repl() command opens a new cmd in which you can type the tree command and get the tree structure of ui elements on the specific screen you are. This is shown in the image below:

Xamarin UI Test

like image 26
Mohsin Awan Avatar answered Oct 17 '22 04:10

Mohsin Awan