Classes

The following classes are available globally.

  • A general-purpose class for implementing randomised UI tests. This class lets you schedule blocks to be run at random or fixed intervals, and provides helper functions to generate random coordinates.

    It has several extensions that implement actual event generation, using different methods. For normal usage, you will want to look at for instance the XCTest private API based extension.

    If all you want to do is geneate some events and you do not care about the finer details, you can just use a test case like the following:

    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()
    }
    
    See more

    Declaration

    Swift

    public class Monkey