如何在 Xcode iOS UI 测试中 select 选择器视图项?

How to select a picker view item in an iOS UI test in Xcode?

我有一个包含几个项目的选择器视图:"Red"、"Green"、"Yellow"、"Black"。在我的 UI 测试中,我需要从中 select 一个特定的项目 "Green"。我正在使用 XCTest UI 测试 API,这些 API 由 Xcode 7.

引入

到目前为止我设法做的是在单元测试中向上滑动整个选择器视图。这并不理想,因为它总是将选择器视图更改为底部项目(向上滑动时)。

let app = XCUIApplication()
app.launch()
app.pickers.elementAtIndex(0).swipeUp()    
XCTAssert(app.staticTexts["Selected: Black"].exists)

另一种非常相似的更改选择器视图的方法是调用 pressForDuration ... thenDragToElement,这不是我想要的。

app.pickers.elementAtIndex(0).pressForDuration(0.1, thenDragToElement: someElement)

当我使用 UI 测试 record 函数时,它不会记录选择器视图滚动事件。当我点击选择器视图项目时它会记录:

app.pickerWheels["Green"].tap()

但当测试为 运行 时,这实际上不起作用(可能是因为它需要在点击之前先滚动选择器视图)。

这是经过测试的演示应用程序。

https://github.com/exchangegroup/PickerViewTestDemo

更新

自 Xcode 7.0 beta 6 以来,现在可以 select 选择器视图。

app.pickerWheels["Green"].adjustToPickerWheelValue("Yellow")

如问题更新中所述,Xcode 7 Beta 6 添加了对与选择器交互的支持。新添加的方法 -adjustToPickerWheelValue: 应该用于 UIPickerView 上的 select 项。

let app = XCUIApplication()
app.launch()
app.pickerWheels.element.adjustToPickerWheelValue("Yellow")

这是一个GitHub repo with a working example. And some more information in a blog post I wrote

如果有多个轮子,一个简单的方法select是这样的:

前提:是日期选择器(UIDatePicker),swift语言

    app.pickerWheels.elementBoundByIndex(0).adjustToPickerWheelValue("March")
    app.pickerWheels.elementBoundByIndex(1).adjustToPickerWheelValue("13")
    app.pickerWheels.elementBoundByIndex(2).adjustToPickerWheelValue("1990")

其中:索引“0”是月,“1”是日,“2”是年

Swift4版@Joao_dche的回答

app.pickerWheels.element(boundBy: 0).adjust(toPickerWheelValue: "March")
app.pickerWheels.element(boundBy: 1).adjust(toPickerWheelValue: "13")
app.pickerWheels.element(boundBy: 2).adjust(toPickerWheelValue: "1990")

Objective-C @Joao_dche 的回答版本

将 UIDatePicker 与 XCTest 一起用于 UI 测试:

    XCUIElementQuery *datePickersQuery = app.datePickers;
    [datePickersQuery.pickerWheels.allElementsBoundByIndex[0] adjustToPickerWheelValue:@"May"];
    [datePickersQuery.pickerWheels.allElementsBoundByIndex[1] adjustToPickerWheelValue:@"13"];
    [datePickersQuery.pickerWheels.allElementsBoundByIndex[2] adjustToPickerWheelValue:@"2017"];

    XCUIElement *doneButton = app.buttons[@"Done"];
    [doneButton.firstMatch tap];

在 iOS 11 上运行良好,但在 iOS 10.3.1 [Xcode 9.2 上对我不起作用]

我使用自定义视图(实际上是标签)作为选择器行,不幸的是乔的华丽回答 对我不起作用 iOS 10.3.1 而 在 iOS 11 上完美工作。所以我不得不使用

app.pickerWheels["X, Y of Z"].swipeUp()

哪里

X is my label's text.
Y is row position in picker view. Position number starts with 1, not zero.
Z is number of values in picker view.

所以在我的例子中,这个命令看起来像

app.pickerWheels["1st, 1 of 28"].swipeUp()

这是 select pickervie(UIPickerView)

最受欢迎的 post 之一

如果您想从选择器视图中select state/date。您可以尝试以下 swift 3 代码。

 XCUIApplication().tables.pickerWheels["AK"].adjust(toPickerWheelValue: "MA")

PickerWheels 从AK 中拾取并设置目标状态。

XCUIApplication().tables.pickerWheels["Starting Point""].adjust(toPickerWheelValue:"目标位置 ")

我使用下面的代码来识别选择器轮中现在显示的元素。

在选取器中查找元素

let valueSelected = algorithmsPicker.pickerWheels.element(boundBy: 0).value as! String

到select选择器轮中的一个值:

 algorithmsPicker.pickerWheels[valueSelected].adjust(toPickerWheelValue: "BubbleSort")