【问题标题】:Tap MKAnnotationView with XCTestCase使用 XCTestCase 点击 MKAnnotationView
【发布时间】:2016-10-05 18:20:43
【问题描述】:

我想用我的XCTestCase 和 UITest 来点击MKAnnotationView,所以我的 mapView 是XCUIElement。我不知道如何从 mapView 获取注释,我不想在屏幕上放置固定位置,因为它在各种设备上进行了测试。所以我被困在 let mapView:XCUIElement = app.maps.elementBoundByIndex(0)

有什么想法吗?

【问题讨论】:

  • 您是否尝试添加辅助功能标识符?

标签: ios swift xctest xcode-ui-testing uitest


【解决方案1】:

要与地图注释交互,请使用title 属性引用的otherElements

例如,在您的生产代码中,在您的MKAnnotation 中设置一个标题。

class Annotation: NSObject, MKAnnotation {
    let coordinate: CLLocationCoordinate2D
    let title: String?

    init(title: String?, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.coordinate = coordinate
    }
}

let coordinate = CLLocationCoordinate2DMake(40.703490, -73.987770)
let annotation = Annotation(title: "BeerMenus HQ", coordinate: coordinate)
let mapView = MKMapView()
mapView.addAnnotation(annotation)

然后在测试中,您可以通过title 属性引用注释。

let app = XCUIApplication()
let annotation = app.maps.element.otherElements["BeerMenus HQ"]
annotation.tap()

【讨论】:

  • 我认为这也可以使用 AccessibilityIdentifier 来完成,这将是 Apple 希望我们使用的更“正确”的方式。
  • 可能被一个可访问性标识符关闭。但是,如果您已经设置了title,为什么不直接使用它呢? MKAnnotation 协议需要title
  • 标题是面向用户的,对吧?将面向用户的东西与测试基础设施分开意味着你可以改变一个而不改变另一个。我希望这是否重要完全取决于团队!
  • 就我而言,我无法使用app.maps.element.otherElements["title"] 访问引脚注释,因为我的注释没有标题。但是我可以通过app.otherElements["accessibilityIdentifier"] 访问它。请注意,这仅在maps被省略时才有效。
猜你喜欢
  • 1970-01-01
  • 2013-06-16
  • 1970-01-01
  • 2011-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
相关资源
最近更新 更多