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

时间:2015-07-06 23:32:06

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

我有一个带有少量项目的选择器视图:“红色”,“绿色”,“黄色”,“黑色”。在我的UI测试中,我需要从中选择一个特定的项目“绿色”。我正在使用Xcode 7引入的XCTest UI测试API。

enter image description here

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

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测试记录功能时,它不会记录选择器视图滚动事件。当我点击选择器视图项时它会记录:

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

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

这是带有测试的演示应用程序。

https://github.com/exchangegroup/PickerViewTestDemo

更新

现在可以从Xcode 7.0 beta 6中选择一个选择器视图。

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

7 个答案:

答案 0 :(得分:46)

正如问题的更新中所述,Xcode 7 Beta 6增加了对与拣货员交互的支持。应使用新添加的方法-adjustToPickerWheelValue:来选择UIPickerView上的项目。

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

这里是GitHub repo with a working example。还有一些more information in a blog post I wrote

答案 1 :(得分:15)

如果有多个轮子,选择项目的简单方法就是这样:

前提条件:它是日期选择器(UIDatePicker),快速语言

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

其中:index" 0"是月," 1"是白天," 2"是一年

答案 2 :(得分:6)

Swift 4版 @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")

答案 3 :(得分:2)

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];

答案 4 :(得分:1)

Joe's answer在iOS 11上运行良好,但在iOS 10.3.1 [Xcode 9.2]

上对我无效

我使用自定义视图(实际上是标签)作为选择器行,不幸的是,在iOS 10.3.1上,我对Joe的回答 不起作用 在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()

答案 5 :(得分:1)

这是select pickervie(UIPickerView)

最受欢迎的帖子之一

如果要从选择器视图中选择状态/日期。您可以尝试使用swift 3代码。

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

PickerWheels从AK中选择并设置目标状态。

XCUIApplication()。tables.pickerWheels ["起点" "]。adjust(toPickerWheelValue:" 目标地点 &#34)

enter image description here

答案 6 :(得分:0)

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

在选择器中查找元素

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

选择拣货轮中的值:

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