如何在Xcode的iOS UItesting中select一个select器视图项?

我有一个select器视图,几个项目:“红色”,“绿色”,“黄色”,“黑色”。 在我的UItesting中,我需要从中select一个特定的“绿色”项目。 我正在使用Xcode 7入侵的XCTest UItestingAPI。

在这里输入图像说明

到目前为止我所做的是在unit testing中将整个拾取器视图向上滑动。 这是不理想的,因为它始终将select器视图更改为底部项目(向上滑动时)。

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

另一个但非常相似的方式来改变select器视图是调用pressForDuration ... thenDragToElement ,这不是我想要的。

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

当我使用UItestingloggingfunction时,它不loggingselect器视图滚动事件。 当我点击select器视图项目时,它会logging下来:

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

但在testing运行时实际上并不工作(可能是因为它需要在点击之前先滚动select器视图)。

这是testing的演示应用程序。

https://github.com/exchangegroup/PickerViewTestDemo

更新

现在可以select自Xcode 7.0 beta 6以来的select器视图。

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

正如问题更新中所指出的那样,Xcode 7 Beta 6增加了与拾取器交互的支持。 应该使用新添加的方法-adjustToPickerWheelValue:来selectUIPickerView上的项目。

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

这里有一个GitHub回购与一个工作的例子 。 还有一些我写在博客文章中的信息 。

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

先决条件:这是一个dateselect器(UIDatePicker),快捷的语言

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

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

Swift 3版本的@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的答案

使用UCTatePicker和XCTest进行UItesting:

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