【发布时间】:2017-04-29 06:09:56
【问题描述】:
如何通过 XCTest 的 UI 测试访问 MKMapView 上的引脚?
我想数一数,验证特定的数字是否存在(基于无障碍 ID 或标题)等。
MKAnnotation 似乎没有 XCUIElementType。
我很难在 MKMapView + XCTest 上找到任何文档。
【问题讨论】:
标签: swift3 xcode8 mkannotation xctest xcode-ui-testing
如何通过 XCTest 的 UI 测试访问 MKMapView 上的引脚?
我想数一数,验证特定的数字是否存在(基于无障碍 ID 或标题)等。
MKAnnotation 似乎没有 XCUIElementType。
我很难在 MKMapView + XCTest 上找到任何文档。
【问题讨论】:
标签: swift3 xcode8 mkannotation xctest xcode-ui-testing
不幸的是,注释视图不在maps 下。您会在那里找到地图上的兴趣点。要查询注释视图,您应该使用XCUIApplication().windows.element.otherElements["Custom Identifier"]。
将accessibilityIdentifier = "Custom Identifier" 添加到您的注释视图,而不是注释。 MKAnnotation 没有实现 accessibilityIdentifier。
【讨论】:
app.otherElements["Custom Identifier"]
使用UIAccessibilityIdentification。
class Annotation: NSObject, MKAnnotation, UIAccessibilityIdentification {
let coordinate: CLLocationCoordinate2D
let title: String?
var accessibilityIdentifier: String?
init(title: String?, coordinate: CLLocationCoordinate2D) {
self.title = title
self.coordinate = coordinate
}
}
let coordinate = CLLocationCoordinate2DMake(40.2853, -73.3382)
let annotation = Annotation(title: "A Place Title", coordinate: coordinate)
annotation.accessibilityIdentifier = "Some Identifier"
let mapView = MKMapView()
mapView.addAnnotation(annotation)
在测试中你可以通过otherElements引用注解。
let app = XCUIApplication()
let annotation = app.maps.element.otherElements["Custom Identifier"]
annotation.tap()
【讨论】:
在我的地图中,我只有 1 个大头针,我可以使用 地图大头针 标识符访问它,如下所示:
let annotation = app.otherElements.matching(identifier: "Map pin").firstMatch
XCTAssertTrue(annotation.exists)
annotation.tap()
【讨论】: