Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Commit

Permalink
fix: error calculating coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
孙健强 committed Oct 3, 2019
1 parent eae9f8c commit 72b8640
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 63 deletions.
106 changes: 44 additions & 62 deletions SwiftMonkey/MonkeyXCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import XCTest
*/
@available(iOS 9.0, *)
extension Monkey {

/// Generates a random `CGVector` inside the frame of the app.
public func randomOffset() -> CGVector {
let point = randomPoint()
return CGVector(dx: point.x, dy: point.y)
}
/**
Add an action that checks, at a fixed interval,
if an alert is being displayed, and if so, selects
Expand All @@ -41,78 +45,56 @@ extension Monkey {
}
}


public func addDefaultXCTestPublicActions() {
addXCTestPublicTapAction(weight: 25)
addXCTestPublicLongPressAction(weight: 1)
addXCTestPublicDragAction(weight: 1)
public func addDefaultXCTestPublicActions(app: XCUIApplication) {
addXCTestPublicTapAction(app: app, weight: 25)
addXCTestPublicLongPressAction(app: app, weight: 1)
addXCTestPublicDragAction(app: app, 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)
}

/// Add an action that generates a tap, with a possibility for double taps, using the public XCTest API.
/// - Parameter app: The application proxy.
/// - 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 doubleTapProbability: Probability that a double tap event is used. Between 0 and 1.
public func addXCTestPublicTapAction(app: XCUIApplication,
weight: Double,
doubleTapProbability: Double = 0.05) {
addAction(weight: weight) { [unowned self] in
let doubleTap = self.r.randomDouble() < doubleTapProbability
let coordinate = app.coordinate(withNormalizedOffset: .zero).withOffset(self.randomOffset())

if doubleTap {
coordinate.doubleTap()
} else {
locations = [ self!.randomPoint() ]
coordinate.tap()
}
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)))

/// Add an action that generates a long press event using the public XCTest API.
/// - Parameter app: The application proxy.
/// - 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(app: XCUIApplication,
weight: Double) {
addAction(weight: weight) { [unowned self] in
let coordinate = app.coordinate(withNormalizedOffset: .zero).withOffset(self.randomOffset())
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)))

/// Add an action that generates a drag event from one random screen position to another using the public XCTest API.
/// - Parameter app: The application proxy.
/// - 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(app: XCUIApplication,
weight: Double) {
addAction(weight: weight) { [unowned self] in
let startCoordinate = app.coordinate(withNormalizedOffset: .zero).withOffset(self.randomOffset())
let endCoordinate = app.coordinate(withNormalizedOffset: .zero).withOffset(self.randomOffset())
startCoordinate.press(forDuration: 0.2, thenDragTo: endCoordinate)
}
}
Expand Down
2 changes: 1 addition & 1 deletion SwiftMonkeyExample/AppTests/MonkeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SwiftMonkeyExampleUITests: XCTestCase {
// monkey.addDefaultXCTestPrivateActions()

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

// UIAutomation actions seem to work only on the simulator.
//monkey.addDefaultUIAutomationActions()
Expand Down

0 comments on commit 72b8640

Please sign in to comment.