Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating a Home Button Press - iPhone Testing

So I'm working on testing an iPhone app. I'm trying to setup test cases that we can use later using UIAutomation (I'm not really tied down to this - I can switch to some other framework). I basically need to create a test that goes something like:

+ Launches application
+ Clicks a few specific buttons
+ Hits the home button exiting the application
+ Relaunches the application
+ Checks the status of the application

Does anyone have any idea how to do this or what framework to use to do this?

Thanks in advance.

like image 863
Ayush Sood Avatar asked Dec 22 '22 10:12

Ayush Sood


2 Answers

You can't simulate the home button being pressed, but you can force the app to exit as if it were. Calling [[UIApplication sharedApplication] terminate]; will terminate or (in the case of iOS 4.x) background the app. You will not, however, be able to relaunch the application using UIAutomation.

You could try UIAutomation in combination with a screen recording script that allows you to replay mouse movement and clicks. This would allow you to interact with the simulator directly for things like home button clicks and app icon clicks.

Alternately you can get "good enough" testing using the UIATarget class. Per the docs,

The UIATarget class represents high-level user interface elements of the system under test (SUT)—that is, your application, the iOS, and the connected device on which they’re running. Your test scripts, written in JavaScript and running in conjunction with the UI Automation instrument, use this class and related UIA classes to exercise the SUT and log results.

Using UIATarget.localTarget().deactivateAppForDuration(seconds); you can background your app for n seconds.

Use this method to test shifting your application to and from the background execution context. Note that applications built using iOS SDK 4.0 or later and running in iOS 4.0 and later aren’t necessarily terminated when the user presses the Home button. See iOS Application Programming Guide for details of multitasking and background execution context.

like image 94
memmons Avatar answered Jan 10 '23 03:01

memmons


It is possible to press the home button in Xcode 7 with UI Testing.

XCUIDevice.sharedDevice().pressButton(XCUIDeviceButton.Home)

Source: https://stackoverflow.com/a/33087217/142358

like image 36
Steve Moser Avatar answered Jan 10 '23 03:01

Steve Moser