SwiftMonkey project is a framework for generating randomised user input in iOS apps. This kind of monkey testing is useful for stress-testing apps and finding rare crashes.
It also contains a related framework called SwiftMonkeyPaws, which provides visualisation of the generated events. This greatly increases the usefulness of your randomised testing, as you can see what touches caused any crash you may encounter.
Why Use SwiftMonkey?
Randomised testing will help you with all of these!
SwiftMonkey is inspired by and has similar goals to UI AutoMonkey, but is integrated into the Xcode UI testing framework, providing better opportunities to debug.
Also Read – URLextractor : Information Gathering & Website Reconnaissance
Quick Start
To see for yourself how this framework works, just grab the code and open SwiftMonkeyExample/SwiftMonkeyExample.xcodeproj
. Then press Cmd-U
to run the UI test.
As a high-level overview, add SwiftMonkey.framework
to your UI test target. Then add a test that creates a Monkey
object and uses it to generate events.
Optionally, you also add the SwiftMonkeyPaws.framework
to your main app, and create a MonkeyPaws
object to enable visualisation. You probably only want to do this for debug builds, or when a specific command line flag is used.
SwiftMonkey uses Swift 4.0. It has no dependencies other than iOS itself (8.0 and up should work). SwiftMonkeyPaws doesn’t have any dependencies, either; you can even use on its own, without SwiftMonkey.
You can install the frameworks using CocoaPods. Assuming that you’ve named your main app and test targets “App” and “Tests”, you can use something like this in your Podfile
:
target ‘App’ do
pod “SwiftMonkeyPaws”, ‘~> 2.1.0’
end
target ‘Tests’ do
pod ‘SwiftMonkey’, ‘~> 2.1.0’
end
Manual Installation
Copy the SwiftMonkey
and SwiftMonkeyPaws
folders into your project. Next, drag the xcodeproj
files into your project.
Then, for SwiftMonkey, add SwiftMonkey.framework
as a dependency for your test target, and add a Copy Files build phase to copy it into Frameworks
.
For SwiftMonkeyPaws, adding SwiftMonkeyPaws.framework
to the Embedded Binaries section of your app target is enough.
(You can also just directly link the Swift files, if you do not want to use frameworks.)
As of this writing, the Swift Package Manager doesn’t support iOS projects. SPM package files have experimentally been created, but obviously don’t really work yet.
To do monkey testing, import SwiftMonkey
, then create a new test case that uses the Monkey
object to configure and run the input event generation. Here is a simple example:
func testMonkey() {
let application = XCUIApplication()
// Workaround for bug in Xcode 7.3. Snapshots are not properly updated
// when you initially call app.frame, resulting in a zero-sized rect.
// Doing a random query seems to update everything properly.
// TODO: Remove this when the Xcode bug is fixed! _ = application.descendants(matching: .any).element(boundBy: 0).frame
// Initialise the monkey tester with the current device
// frame. Giving an explicit seed will make it generate
// the same sequence of events on each run, and leaving it
// out will generate a new sequence on each run. let monkey = Monkey(frame: application.frame)
//let monkey = Monkey(seed: 123, frame: application.frame)
// Add actions for the monkey to perform. We just use a
// default set of actions for this, which is usually enough.
// Use either one of these, but maybe not both.
// XCTest private actions seem to work better at the moment.
// UIAutomation actions seem to work only on the simulator. monkey.addDefaultXCTestPrivateActions() //monkey.addDefaultUIAutomationActions()
// Occasionally, use the regular XCTest functionality
// to check if an alert is shown, and click a random
// button on it. monkey.addXCTestTapAlertAction(interval: 100, application: application)
// Run the monkey test indefinitely. monkey.monkeyAround()
}
The Monkey
object allows you not only to add the built-in event generators, but also any block of your own to be executed either randomly or at set intervals. In these blocks you can do whatever you want, including (but not only) generate more input events.
Documentation for this is limited at the moment, so please refer to Monkey.swift
and its extensions for examples of how to use the more advanced functionality if you need it.
The simplest way to enable the visualisation in your app is to first import SwiftMonkeyPaws
, then do the following somewhere early on in your program execution:
var paws: MonkeyPaws?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if CommandLine.arguments.contains(“–MonkeyPaws”) {
paws = MonkeyPaws(view: window!)
}
return true
}
(This example uses application(_, didFinishLaunchingWithOptions)
, but any time after you have a UIWindow will do. It also only instatiates the visualisation if a certain command line flag is passed, so that it can be enabled only for test runs.)
Using command line flags, If you want to enable MonkeyPaws on your test case file you can add the following on yout testMonkey function:
let application = XCUIApplication()
application.launchArguments = [“–MonkeyPaws”]
This call will swizzle some methods in UIApplication to capture UIEvents. If you would rather not do this, or if you already have a source of UIEvents, you can pass the following option to init
to disable swizzling:
paws = MonkeyPaws(view: window!, tapUIApplication: false)
Then you can pass in events with the following call:
paws?.append(event: event) // event is UIEvent
TODO
Kali Linux 2024.4, the final release of 2024, brings a wide range of updates and…
This Go program applies a lifetime patch to PowerShell to disable ETW (Event Tracing for…
GPOHunter is a comprehensive tool designed to analyze and identify security misconfigurations in Active Directory…
Across small-to-medium enterprises (SMEs) and managed service providers (MSPs), the top priority for cybersecurity leaders…
The free and open-source security platform SecHub, provides a central API to test software with…
Don't worry if there are any bugs in the tool, we will try to fix…