Xcode 7中的UI测试:在UIPickerView / PickerWheel中选择行号

时间:2015-10-23 08:53:55

标签: xcode swift uipickerview ui-testing xcode-ui-testing

在Xcode 7 UI Testing中有没有办法选择UIPickerView中的第3行?

我尝试过各种类似的东西来识别每个选择器中的行,但下面的所有请求都返回0:

XCUIApplication().pickers.element.cells.count
XCUIApplication().pickers.element.staticTexts.count

有什么想法吗?

更新:我知道adjustToPickerWheelValue方法,你可以在其中选择你已经知道的特定值,但是当我不知道选择器中存在的值时,我试图选择第3个值(index = 4)。

3 个答案:

答案 0 :(得分:2)

您可以使用-adjustToPickerWheelValue:选择UIPickerView上的项目。

当屏幕上有一个UIPickerView时,您可以直接选择元素,就像这样。

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

如果选择器有多个轮子,您需要先通过它的辅助功能标识符选择轮子,然后进行调整。

app.pickerWheels["Feet"].adjustToPickerWheelValue("5")

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

答案 1 :(得分:0)

日期选择器(UIDatePicker)

的示例

enter image description here

 XCUIApplication().datePickers.pickerWheels.elementBoundByIndex(0).adjustToPickerWheelValue("March")
 XCUIApplication().datePickers.pickerWheels.elementBoundByIndex(1).adjustToPickerWheelValue("24")
 XCUIApplication().datePickers.pickerWheels.elementBoundByIndex(2).adjustToPickerWheelValue("2000")

以下是Picker视图(UIPickerView)的示例

enter image description here

 XCUIApplication().pickerWheels.element.adjustToPickerWheelValue("Male")

答案 2 :(得分:0)

解决方法:

let pickerWheel = XCUIApplication().pickers.pickerWheels.element
let appWindowHeight = XCUIApplication().windows.elementBoundByIndex(0).frame.height
let pickerWheelCellHeight = GFloat(44)
    func advancePickerWheelOneValue() -> Self {
         let topScrollPoint = Double(pickerWheel.frame.midY/appWindowHeight)
         let bottomScrollPoint = Double((pickerWheel.frame.midY + pickerWheelCellHeight)/appWindowHeight)
         let topScreenPoint = XCUIApplication().windows.elementBoundByIndex(0).coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: topScrollPoint))
         let bottomScreenPoint = XCUIApplication().windows.elementBoundByIndex(0).coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: bottomScrollPoint))
         bottomScreenPoint.pressForDuration(0, thenDragToCoordinate: topScreenPoint)
         return self
    }

您可能需要根据选择器的位置调整dx值。

.coordinateWithNormalizedOffset(CGVector(dx: 0.5,

但是如果选择器占据屏幕的宽度,则0.5可以工作。

使用此方法,您可以一次移动选择器的值。多次调用它,你就可以得到你想要的值。

它不理想但如果你想检查某些索引它会有所帮助。希望你必须在具有数百个值的选择器上使用它;)