ios - 使用 XCTestCase 点击 MKAnnotationView

标签 ios swift xctest xcode-ui-testing uitest

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

有什么想法吗?

最佳答案

要与 map 注释交互,请使用 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()

关于ios - 使用 XCTestCase 点击 MKAnnotationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37652399/

相关文章:

xcode - 如何从 faSTLane 只运行一个 ui-test?

objective-c - 如果将自动释放对象标记为自动释放会发生什么

iOS:使用 UIImageView 进行动画

iOS:带有 RTF 的警报 View (粗体、URL 等)

ios - 单击时更改按钮背景颜色- WatchKit

ios - 为什么我不能将在 viewDidLoad 方法中创建的 View 约束到 ViewControllers View ?

ios - 将应用程序提交到应用程序商店错误

ios - Firebase - for 循环 Swift 中嵌套的 observeSingleEvent 方法

ios - 如何在 Obj C 中编写没有完成 block 的异步方法的单元测试

ios - 无法在 Xcode 11 中运行单元测试 : The run destination * is not valid for tests you have chosen to perform