Skip to content

Commit

Permalink
fix: use Public API on Xcode10.2
Browse files Browse the repository at this point in the history
  • Loading branch information
a455455b committed Sep 25, 2019
1 parent 330c6d8 commit eae9f8c
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 38 deletions.
60 changes: 30 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,36 +102,36 @@ the input event generation. Here is a simple example:

````swift
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()
let application = XCUIApplication()

// 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.
// before Xcode 10.1, you can use
// monkey.addDefaultXCTestPrivateActions()

// after Xcode 10.1 We can only use public API
monkey.addDefaultXCTestPublicActions()

// UIAutomation actions seem to work only on the simulator.
//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()
}
````

Expand Down
76 changes: 76 additions & 0 deletions SwiftMonkey/MonkeyXCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,80 @@ extension Monkey {
}
}
}


public func addDefaultXCTestPublicActions() {
addXCTestPublicTapAction(weight: 25)
addXCTestPublicLongPressAction(weight: 1)
addXCTestPublicDragAction(weight: 1)
}

/**
Add an action that generates a tap, with a possibility for
multiple taps with multiple fingers, using the private
XCTest API.

- parameter weight: The relative probability of this
event being generated. Can be any value larger than
zero. Probabilities will be normalised to the sum
of all relative probabilities.
- parameter multipleTouchProbability: Probability that
the tap event will use multiple fingers. Between 0 and 1.
*/
public func addXCTestPublicTapAction(weight: Double,
multipleTouchProbability: Double = 0.05) {
addAction(weight: weight) { [weak self] in
let locations: [CGPoint]
if self!.r.randomDouble() < multipleTouchProbability {
let numberOfTouches = Int(self!.r.randomUInt32() % 3) + 2
let rect = self!.randomRect()
locations = (1...numberOfTouches).map { _ in
self!.randomPoint(inRect: rect)
}
} else {
locations = [ self!.randomPoint() ]
}
let app = XCUIApplication()
let coordinate = app.coordinate(withNormalizedOffset: CGVector(dx: locations[0].x/(app.frame.maxX/2), dy: locations[0].y/(app.frame.maxY/2)))
coordinate.tap()
}
}

/**
Add an action that generates a long press event
using the private XCTest API.

- Parameter weight: The relative probability of this
event being generated. Can be any value larger than
zero. Probabilities will be normalised to the sum
of all relative probabilities.
*/
public func addXCTestPublicLongPressAction(weight: Double) {
addAction(weight: weight) { [weak self] in
let point = self!.randomPoint()
let app = XCUIApplication()
let coordinate = app.coordinate(withNormalizedOffset: CGVector(dx: point.x/(app.frame.maxX/2), dy: point.y/(app.frame.maxY/2)))
coordinate.press(forDuration: 0.5)
}
}

/**
Add an action that generates a drag event from one random
screen position to another using the private XCTest API.

- Parameter weight: The relative probability of this
event being generated. Can be any value larger than
zero. Probabilities will be normalised to the sum
of all relative probabilities.
*/
public func addXCTestPublicDragAction(weight: Double) {
addAction(weight: weight) { [weak self] in
let start = self!.randomPointAvoidingPanelAreas()
let end = self!.randomPoint()
let app = XCUIApplication()
let startCoordinate = app.coordinate(withNormalizedOffset: CGVector(dx: start.x/(app.frame.maxX/2), dy: start.y/(app.frame.maxY/2)))
let endCoordinate = app.coordinate(withNormalizedOffset: CGVector(dx: end.x/(app.frame.maxX/2), dy: end.y/(app.frame.maxY/2)))
startCoordinate.press(forDuration: 0.2, thenDragTo: endCoordinate)
}
}
}
16 changes: 8 additions & 8 deletions SwiftMonkeyExample/AppTests/MonkeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ class SwiftMonkeyExampleUITests: XCTestCase {
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
Expand All @@ -39,11 +33,17 @@ class SwiftMonkeyExampleUITests: XCTestCase {
// 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.
// before Xcode 10.1, you can use
// monkey.addDefaultXCTestPrivateActions()

// after Xcode 10.1 We can only use public API
monkey.addDefaultXCTestPublicActions()

// 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.
Expand Down

0 comments on commit eae9f8c

Please sign in to comment.