3

I am working on automation of mobile native app using Appium in Java language. I want to click on device Menu button.

Method 1:

 JavascriptExecutor js = (JavascriptExecutor) driver;
 js.executeScript("mobile: keyevent", 82);

tried but not working.

Method 2:-

HashMap swipeObject = new HashMap();
swipeObject.put("keycode", 82);
((JavascriptExecutor ) driver).executeScript("mobile: keyevent", swipeObject);

and

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

this is not working,can any one help me on this.

  • You should use applium sendkey method , Like : AppiumDriver.sendKeyEvent(AndroidKeyCode.MENU) OR driver.getKeyboard().sendKeys(AndroidKeys.MENU); – Helping Hands Feb 3 '15 at 6:39
  • i am using webdriver..is it ok for it – RaMeSh Feb 3 '15 at 6:41
  • Yes of course. It is ok with appium + webdriver. – Helping Hands Feb 3 '15 at 6:42
  • code is same which I have shared as comment. Once you confirm that it is working then sure i will post as answer. – Helping Hands Feb 3 '15 at 6:44
  • code is required for click device menu bar at top right. – RaMeSh Feb 3 '15 at 6:47
1

Appium Java-client provides mechanism to Send a key event to the device. Please check the here for more details. So for latest appium java client, below code should work.

  driver.pressKeyCode(AndroidKeyCode.MENU);
  • Can you please explain what trick does this do? – IAmMilinPatel Jul 13 '16 at 3:29
  • I don't think it as trick, Appium Java-client provides mechanism to Send a key event to the device. Please check this...appium.github.io/java-client/io/appium/java_client/android/… – Amol Jul 13 '16 at 3:35
  • Adding this explanation and the reference link to your answer would me more helpful for Narendra as well as other people having similar query! Some reason as to why this would work and why it is a better option over any other (if available) really helps!!! – IAmMilinPatel Jul 13 '16 at 3:48
0

The simplest working solution is to use the KEYCODE KeyEvent:

adb shell input keyevent <KEYCODE_MENU>

Help - Android keycode

  • can you expand on why this is a good solution? – ECiurleo Jul 11 '16 at 8:34
  • @Eciurleo - 1 This is shell command. 2 It will directly intended to communicate with device 3 Utility included in android sdk itself Due to above all, success percentage may increase than other methods. This was my criteria for simplest solution :) – NarendraC Jul 11 '16 at 9:24
  • sounds good, can you add that into your answer using the edit? Most users find it helpful to include the logic and background as to why a solution is the preferred one rather than just giving a solution :) – ECiurleo Jul 11 '16 at 14:32
  • sure @ECiurleo , but waiting to know if it works for RaMeSh – NarendraC Jul 19 '16 at 13:30
  • people accept answers based on the answer itself, not the comments. The comments should be a mechanism to improve the answer only. – ECiurleo Jul 19 '16 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.