Skip to content

Instantly share code, notes, and snippets.

@mathonsunday
Created April 22, 2021 20:51
Show Gist options
  • Save mathonsunday/5949c1610902db208ff9192aaf48f781 to your computer and use it in GitHub Desktop.
Save mathonsunday/5949c1610902db208ff9192aaf48f781 to your computer and use it in GitHub Desktop.
public class MapBot: Robot, RobotItem {
public enum Item: UIElementable {
case map
case cluster(withText: String)
case pricePin(withText: String)
case clearAll
case schoolPin
public func UIElement(on app: XCUIApplication) -> XCUIElement {
switch self {
case .map: return app.otherElements[mapViewIdentifier].firstMatch
case .cluster(let text):
return app.otherElements[mapView]
.descendants(matching: .any)[clusterIdentifier + "_\(text)"]
.firstMatch
case .pricePin(let text):
return app.otherElements[mapViewIdentifier]
.descendants(matching: .any)[pinIdentifier + "_\(text)"]
.firstMatch
case .clearAll: return app.buttons[searchThisAreaButtonIdentifier]
case .schoolPin:
return app.otherElements[schoolPinIdentifier].firstMatch
}
}
}
override public init() {
super.init()
}
@discardableResult
public func panDown() -> MapBot {
app.otherElements[mapViewIdentifier].swipeDown()
return self
}
...
@discardableResult
public func zoomOut(scale: CGFloat = 0.5) -> MapBot {
app.otherElements[mapViewIdentifier].pinch(withScale: scale, velocity: -10)
return self
}
...
@discardableResult
public func tapAnyCluster() -> MapBot {
app.otherElements[mapViewIdentifier]
.descendants(matching: .any)
.allElementsBoundByIndex
.first(where: { $0.label.contains(clusterIdentifier) })?
.tap()
return self
}
...
@discardableResult
public func tapClusterWithHighestCountAndReturnCount() -> Int {
let cluster = app.otherElements[mapViewIdentifier]
.descendants(matching: .any)
.allElementsBoundByIndex
.filter { $0.label.contains(clusterIdentifier) }
.sorted(by: {
let count1 = Int($0.label.filter("0123456789".contains)) ?? 0
let count2 = Int($1.label.filter("0123456789".contains)) ?? 0
return count1 > count2
}).first
let count = Int(cluster?.label.filter("0123456789".contains)) ?? 0
cluster?.tap()
return count
}
...
public var hasPricePins: Bool {
let pricePin = app.otherElements[mapViewIdentifier]
.descendants(matching: .any)
.allElementsBoundByIndex
.first(where: { $0.label.contains(pinIdentifier) })?
.waitForExistence(timeout: 5)
return pricePin != nil
}
...
public var hasBoundaries: Bool {
return boundariesCount > 0
}
public var boundariesCount: Int {
return app.otherElements[mapViewIdentifier].descendants(matching: .any)
.allElementsBoundByIndex
.filter { $0.identifier.contains("GMSPolygon") }
.count
}
...
@discardableResult
public func assert(hasPricePins: Bool) -> MapBot {
assert(.exists, on: .map)
XCTAssertEqual(hasPricePins, self.hasPricePins)
return self
}
...
@discardableResult
public func tapMapAtLocalCoordinates(x: Double, y: Double) -> MapBot {
let normalized = Item.map.UIElement(on: app)
.coordinate(withNormalizedOffset: .zero)
let coordinate = normalized.withOffset(CGVector(dx: x, dy: y))
coordinate.tap()
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment