14

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.

G.Sabathier
  • 377
  • 1
  • 4
  • 13

4 Answers4

9

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")

eleventy
  • 166
  • 2
  • 5
0

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

Testing Singh
  • 1,347
  • 1
  • 15
  • 26
0

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

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
-1

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.

damusnet
  • 4,320
  • 3
  • 25
  • 39