Class AndroidDriver (implements AppiumDriver interface) has method pressKeyCode(int key) for your handset / emulator to navigate to Home screen or navigating back.
the argument key can be any one of the option from below link
https://developer.android.com/reference/android/view/KeyEvent.html
To navigate to home screen:
driver.pressKeyCode(AndroidKeyCode.HOME);
Navigate to back screen:
driver.pressKeyCode(AndroidKeyCode.BACK);
Let's see how we can implement in code those above 2 statements
public class NavigateScreens{ static DesiredCapabilities cap; static AndroidDriver driver; @BeforeTest static void setup() throws MalformedURLException{ cap = new DesiredCapabilities(); cap.setCapability(CapabilityType.PLATFORM, "Android"); cap.setCapability(CapabilityType.VERSION, "5.1.0"); cap.setCapability("deviceName", "mygeny510"); cap.setCapability("appPackage", "io.selendroid.testapp"); cap.setCapability("appActivity", ".HomeScreenActivity"); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap); } @Test static void HomeBack(){ driver.findElementByName("Touch Actions").click(); driver.findElementByName("Canvas").click(); driver.pressKeyCode(AndroidKeyCode.BACK); driver.pressKeyCode(AndroidKeyCode.HOME); } }
hi i’m using driver.pressKeyCode(AndroidKeyCode.KEYCODE_CAMERA); to click picture in camera but it’s not working
can you try this one –
Runtime.getRuntime().exec(“cmd /C adb shell input keyevent ” + 27);