Simulating Pressing The Home Button in Xcode 7 UI Automation

Simulating pressing the Home button in Xcode 7 UI Automation

You need to get the device instance first. So to simulate pressing the home button:

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

should work (it does for me on a physical device)

Thanks!

Mazen

Swift 5 Version:

XCUIDevice.shared.press(XCUIDevice.Button.home)

Also, verified it works in the simulator, at least in Xcode 11.2.1 on a simulated "iPad Pro (9.7-inch)" running iPadOS 13.2.2.

Simulating a Home Button Press - iPhone Testing

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.

iOS simulator: double click home button does not work sometimes

I have this problem too when I want to close my app. So, I use command line instead:

xcrun simctl list | grep Booted

then copy simulator ID, and run this command to close app

xcrun simctl terminate <simulator ID> <your app bundle ID>



... or if you have only one simulator running, just type booted:

xcrun simctl terminate booted <your app bundle ID>

Xcode 7 UI Tests, Recording button is greyed out

FWIW: I had this problem and it turns out I was trying to run the simulator in the wrong OS.

I was trying to use iOS 8, and UITesting only works in iOS 9+.

Switch the simulator version, and the record button appears.

How to get Instruments attached to simulator for UI Automation Testing

The simplest way to attach your target to the simulator and run your UIAutomation scripts is to profile the app. Xcode - Product - Build For - Profiling and then select the Automation template.

Another way to attach the target, if you've already built the app on your simulator. Is to select Choose Target and then go to the following location /Users/[yourUserName]/Library/Application Support/iPhone Simulator/[iOSVersion]/Applications/[AppFolder]/[NameOfYourTarget]

For more info, you can take a look at this blog which is pretty detailed http://blog.manbolo.com/2012/04/08/ios-automated-tests-with-uiautomation

Hope it helps.

With Appium how to click the home button or launch the home screen in ios simulator?

Thanks guys for responding ,seems like there is no way the home screen can be launched , even java robot keys "command+shift+home" doesn't work .

So I had to write one apple script, when called will launch the home screen in the simulator .

it's here

    try
tell application "System Events"

if exists process "Simulator" then
tell process "Dock"
delay 2
set frontmost to true
activate
tell list 1
try
perform action "AXShowMenu" of UI element "Simulator"
delay 2
#click accssibilitytitle "Open" of menu item of menu1 -- up arrow
key code 126 -- up arrow

key code 126 -- up arrow

key code 126 -- up arrow
key code 126 -- up arrow

key code 126 -- up arrow
-- key code 125 -- down arrow
delay 2
key code 36 -- return key
on error errMsg
if errMsg contains "Simulator" then
log "Simulator is not present in the dock... To run the automation, add Simulator in the dock and try again!!!"

return
else
log errMsg

return
end if
end try
end tell
end tell
end if

end tell
on error errMsg
log errMsg

end try

tell application "System Events" to tell process "Simulator"
tell menu bar item 5 of menu bar 1
delay 3
click
delay 5
click menu item "Home" of menu 1
delay 3
end tell
end tell

It is able to successfully bring the simulator in focus by right click on the simulator icon present in dock and then selecting "Home" from the "Hardware" menu (which is menu 5) .



Related Topics



Leave a reply



Submit