Sometimes there's an automation feature of apparently little utility, but it's fun all the same. For some reason, Apple decided to allow automation of the physical buttons on an iPhone. Well, some of them: the home button, and the volume up and down buttons. I'm still trying to think of actually useful cases for automating these buttons, but in the meantime, let's look at how to use them.
They're accessible as one of Appium's
mobile: methods, like many other non-standard extensions to the WebDriver protocol. Here's an example of how to press the home button:
driver.executeScript("mobile: pressButton", ImmutableMap.of("name", "home"));
In other words, there's a single parameter
name, and it takes one of three values:
The only catch is that the
volumedown buttons can only be automated on a real device, not a simulator (for some bizarre reason, since simulators do have virtual volume buttons that can be clicked).
Pressing the home button is a good way to get out of your app and back to the home screen. Pressing it twice will navigate back to the first home screen. And pressing it twice in quick succession should open up the app switcher. Unfortunately, even with all my attempts at different timing, I couldn't actually get the app switcher to launch. Nor could I get it to launch manually by clicking the simulator button myself, so it's possible that the simulator's home button recognition is itself not reliable.
Anyway, if any of you readers have an idea of an interesting use case for automating these physical buttons, please do let me know! And you can also check out a full code sample on GitHub if you want to see the command in context.