How to click Android program view through MonkeyRunner?

I want to use MonkeyRunner to check the compatibility of my Android program for a list of devices with different screen resolutions. I need to click a view, but the view is not in the same position for different resolutions. How can I get his position or do something else to click him? Need your help!

+6
source share
3 answers

I know this a bit later, but you can use hierarchyviewer in android sdk to get view id.

Then in the script use the following:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice from com.android.monkeyrunner.easy import EasyMonkeyDevice, By device = MonkeyRunner.waitForConnection() easy_device = EasyMonkeyDevice(device) # Start your android app # touch the view by id easy_device.touch(By.id('view_id'), MonkeyDevice.DOWN_AND_UP) 

Later editing: thanks to dtmilano and AndroidViewClient, I was able to make clicks on views as needed. The link is here: https://github.com/dtmilano/AndroidViewClient

+6
source

Unfortunately, this is not possible with MonkeyRunner. One option is to use device.getProperty("display.width") , device.getProperty("display.height") and device.getProperty("display.density") and try to use them to somehow determine where is the view. Another option is to use a tool like Sikuli to try to click on it.

Edit (a year after this answer was originally posted): now it's possible to do what you originally wanted with https://github.com/dtmilano/AndroidViewClient

+4
source

Similar to what someone said above, I use two functions that convert the coordinates of the transition based on the difference in resolution of the device that I originally wrote for the script for and any device that I currently use.

First, I get the current pixel widths x and y.

 CurrentDeviceX = float(device.getProperty("display.width")) CurrentDeviceY = float(device.getProperty("display.height")) 

Then I define a function to convert the x and y coordinates. You can see that the functions below were recorded for a 1280 x 800 device.

 def transX(x): ''' (number) -> intsvd TransX takes the x value supplied from the original device and converts it to match the resolution of whatever device is plugged in ''' OriginalWidth = 1280; #Get X dimensions of Current Device XScale = (CurrentDeviceX)/(OriginalWidth) x = XScale * x return int(x) def transY(y): ''' (number) -> int TransY takes the y value supplied from the original device and converts it to match the resolution of whatever device is plugged in. ''' OriginalHeight = 800; #Get Y dimensions of Current Device YScale = (CurrentDeviceY)/(OriginalHeight) y = YScale * y return int(y) 

Then I can use these functions when creating click events in my scripts.

Example

 device.touch(transX(737), transY(226), 'DOWN_AND_UP') 

Please note that this approach is far from perfect, and it will only work if your application uses binding to customize the user interface based on screen size. This is my quick and dirty approach to creating scripts that run on multiple devices when UIs are not available.

+2
source

Source: https://habr.com/ru/post/894797/


All Articles