Generating Android device events

4,471 views
Skip to first unread message

Steve Hastings

unread,
Apr 18, 2013, 4:42:37 PM4/18/13
to appium-...@googlegroups.com
If you are working directly in Android UI Automator, writing Java code, you have full access to Android device events.  Examples:

getUiDevice().pressHome();  // press "Home" button to switch to desktop

getUiDevice().pressBack();  // press the "back" button

Checking the Wiki, I see that Appium has support for tap and swipe events.  Does Appium also have support for these device events?  I could fake a "Home" or "back" event by figuring out the X,Y coordinates of those GUI controls and issuing a tap event, but I would of course prefer to do this in a way that doesn't depend on the screen resolution of the specific device.

I have an app where the only way to dismiss a dialog is the "back" button.  I think this is a common thing, actually.

Also, some Android devices have direction pads or other hard keys, and probably all Android devices have volume control hard keys.  So I hope there will be a way to issue key events in Appium.

P.S. When I use Appium, I prefer to use the Python bindings, so if there is a way to do this, I would very much appreciate a Python example. I imagine it will probably need to use driver.execute_script(), which is fine with me at least for now.

Thanks.

bootstrap online

unread,
Apr 18, 2013, 4:49:33 PM4/18/13
to appium-...@googlegroups.com
On Android with Ruby, you can go back using: $driver.navigate.back

There's also a mobile: keyevent method to send a key event to Android. Both methods should work using any of the bindings.

Steve Hastings

unread,
Apr 19, 2013, 12:02:14 AM4/19/13
to appium-...@googlegroups.com
I have checked, and my Python bindings don't seem to have driver.navigate.  I modified my test script, and once connected to Appium it prints str(dir(driver)) and there is nothing in there that looks like "navigate".

But your suggestion of using "mobile: keyevent" is spot on.  Code 4 is the Back key:

driver.execute_script("mobile: keyevent", {"keycode":4})

Of course I have wrapped this in a nice function.  Thanks.

Jonathan Lipps

unread,
Apr 19, 2013, 12:14:41 AM4/19/13
to Steve Hastings, appium-...@googlegroups.com
I believe python has driver.back()

--
http://appium.io
---
You received this message because you are subscribed to the Google Groups "Appium-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to appium-discus...@googlegroups.com.
Visit this group at http://groups.google.com/group/appium-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ritika Mishra

unread,
Apr 22, 2013, 8:42:12 AM4/22/13
to appium-...@googlegroups.com
I am able to preform "driver.navigate().back()", but not able to perform action on other device buttons, like the HOME button and the MENU button 

bootstrap online

unread,
Apr 22, 2013, 9:23:04 AM4/22/13
to appium-...@googlegroups.com
You can look up the keyevent codes. For example, home is 3.

http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_HOME

Ritika Mishra

unread,
Apr 23, 2013, 1:24:47 AM4/23/13
to appium-...@googlegroups.com
Hi, i tried using the KeyEvent code, but every time i execute it, i get :

info: [ADB] Sending keyevent NaN
info: Responding to client with success: {"status":0,"value":null,"sessionId":"53369111-a7ef-4527-8b47-b45a2839f6d5"}

no action takes place.

Thanks.

bootstrap online

unread,
Apr 23, 2013, 9:13:19 AM4/23/13
to Ritika Mishra, appium-...@googlegroups.com
That means you didn't send a number.

Jonathan Lipps

unread,
Apr 23, 2013, 9:39:17 AM4/23/13
to bootstrap online, Ritika Mishra, appium-...@googlegroups.com
Remember that the parameters to executeScript need to be wrapped in an array in many languages (incl Java). So, in js:

driver.executeScript("mobile: keyevent", [{keycode: 3}]);

Notice that {keycode: 3} is inside an array.

Evgenii

unread,
Jul 30, 2013, 7:14:59 PM7/30/13
to appium-...@googlegroups.com, bootstrap online, Ritika Mishra
Hi Jonathan,
Could you please help. I run this command in python script:
self.driver.execute_script('mobile: keyevent', [{'keycode': 82}])
Result is:
[ADB] Sending keyevent NaN

Is sintaxis wrong? 


I found work around:
os.system("adb shell input keyevent 82")
which is works great for me, it taps menu button


вторник, 23 апреля 2013 г., 6:39:17 UTC-7 пользователь Jonathan Lipps написал:

Dan Cuellar

unread,
Jul 30, 2013, 10:05:38 PM7/30/13
to appium-...@googlegroups.com, bootstrap online, Ritika Mishra
Check out this page on how to call mobile methods

Evgenii

unread,
Jul 31, 2013, 2:04:34 PM7/31/13
to appium-...@googlegroups.com, bootstrap online, Ritika Mishra
I tried both ways:
self.driver.execute_script('mobile: keyevent', [{'keycode': 82}])
self.driver.execute_script('mobile: keyevent', [{'keycode': '82'}])

result the same

вторник, 30 июля 2013 г., 19:05:38 UTC-7 пользователь Dan Cuellar написал:

bootstrap online

unread,
Jul 31, 2013, 2:34:50 PM7/31/13
to Evgenii, appium-...@googlegroups.com, Ritika Mishra
Are you running Appium from source? A bug was fixed related to this on
Selendroid.

Jonathan Lipps

unread,
Aug 5, 2013, 2:45:04 PM8/5/13
to bootstrap online, Evgenii, appium-...@googlegroups.com, Ritika Mishra
In python, you don't need the extra []

self.driver.execute_script('mobile: keyevent', {'keycode': 82})


> Visit this group at http://groups.google.com/group/appium-discuss.

Aakanksha Malhotra

unread,
Jan 15, 2014, 1:08:42 PM1/15/14
to appium-...@googlegroups.com
hi 
Can somebody please guide about this in java ?? i.e how to automate back event using appium in java

Jonathan Lipps

unread,
Jan 15, 2014, 1:23:20 PM1/15/14
to Aakanksha Malhotra, appium-...@googlegroups.com
use the regular webdriver back() method. Should be in the java webdriver docs.

On Jan 15, 2014, at 10:08 AM, Aakanksha Malhotra <amalho...@gmail.com> wrote:

hi 
Can somebody please guide about this in java ?? i.e how to automate back event using appium in java

CY_suncheng

unread,
Jun 6, 2014, 3:45:52 AM6/6/14
to appium-...@googlegroups.com, steveha...@gmail.com
hello , i use driver.back() for appium 0.18 and it works well, but when i upgrade appium to 1.1, it does not work and no any error message

在 2013年4月19日星期五UTC+8下午12时14分41秒,Jonathan Lipps写道:

Isaac Murchie

unread,
Jun 6, 2014, 10:10:12 AM6/6/14
to CY_suncheng, appium-...@googlegroups.com, steveha...@gmail.com
`driver.back()` seems to be working fine. It ends up calling `pressBack` on the device.

The logs:

debug: Appium request initiated at /wd/hub/session/c198c229-f4e0-4ac7-af7b-cb1f753c295f/back
debug: Request received with params: {"sessionId":"c198c229-f4e0-4ac7-af7b-cb1f753c295f"}
debug: Pushing command to appium work queue: ["pressBack"]
debug: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"pressBack","params":{}}
debug: [BOOTSTRAP] [info] Got command of type ACTION
debug: [BOOTSTRAP] [debug] Got command action: pressBack
debug: [BOOTSTRAP] [info] Returning result: {"value":true,"status":0}
debug: Responding to client with success: {"status":0,"value":true,"sessionId":"c198c229-f4e0-4ac7-af7b-cb1f753c295f"}
POST /wd/hub/session/c198c229-f4e0-4ac7-af7b-cb1f753c295f/back 200 1293.584 ms - 76


Reply all
Reply to author
Forward
0 new messages