【问题标题】:MKAnnotation via XCTest UI Testing通过 XCTest UI 测试的 MKAnnotation
【发布时间】:2017-04-29 06:09:56
【问题描述】:

如何通过 XCTest 的 UI 测试访问 MKMapView 上的引脚?

我想数一数,验证特定的数字是否存在(基于无障碍 ID 或标题)等。

MKAnnotation 似乎没有 XCUIElementType。

我很难在 MKMapView + XCTest 上找到任何文档。

【问题讨论】:

    标签: swift3 xcode8 mkannotation xctest xcode-ui-testing


    【解决方案1】:

    不幸的是,注释视图不在maps 下。您会在那里找到地图上的兴趣点。要查询注释视图,您应该使用XCUIApplication().windows.element.otherElements["Custom Identifier"]

    accessibilityIdentifier = "Custom Identifier" 添加到您的注释视图,而不是注释。 MKAnnotation 没有实现 accessibilityIdentifier

    【讨论】:

    • 你可以使用app.otherElements["Custom Identifier"]
    【解决方案2】:

    使用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()
    

    【讨论】:

    • 我正在使用 IBOutlet 到 MKMapView。当我尝试访问时,XCTest 告诉我“找不到 Map 匹配项”:app.maps.element.otherElements["Pin 1"]
    【解决方案3】:

    在我的地图中,我只有 1 个大头针,我可以使用 地图大头针 标识符访问它,如下所示:

        let annotation = app.otherElements.matching(identifier: "Map pin").firstMatch
        XCTAssertTrue(annotation.exists)
    
        annotation.tap()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      相关资源
      最近更新 更多