From 72b8640859f81e1f1130958114f5c0e4bd134939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E5=81=A5=E5=BC=BA?= Date: Thu, 3 Oct 2019 17:46:40 +0800 Subject: [PATCH] fix: error calculating coordinates --- SwiftMonkey/MonkeyXCTest.swift | 106 ++++++++---------- SwiftMonkeyExample/AppTests/MonkeyTests.swift | 2 +- 2 files changed, 45 insertions(+), 63 deletions(-) diff --git a/SwiftMonkey/MonkeyXCTest.swift b/SwiftMonkey/MonkeyXCTest.swift index cfeb4d2..12f728f 100644 --- a/SwiftMonkey/MonkeyXCTest.swift +++ b/SwiftMonkey/MonkeyXCTest.swift @@ -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 @@ -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) } } diff --git a/SwiftMonkeyExample/AppTests/MonkeyTests.swift b/SwiftMonkeyExample/AppTests/MonkeyTests.swift index a3cb2f3..5fc4d73 100644 --- a/SwiftMonkeyExample/AppTests/MonkeyTests.swift +++ b/SwiftMonkeyExample/AppTests/MonkeyTests.swift @@ -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()