【问题标题】:UI Testing in Xcode 7: Selecting a row number in a UIPickerView / PickerWheelXcode 7 中的 UI 测试:在 UIPickerView / PickerWheel 中选择行号
【发布时间】:2015-10-23 08:53:55
【问题描述】:

Xcode 7 UI 测试中有没有办法在 UIPickerView 中选择第三行?

我已经尝试了各种类似的方法来识别每个选择器中的行,但下面的所有请求都返回 0:

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

有什么想法吗?

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

【问题讨论】:

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


    【解决方案1】:

    您可以使用-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

    【讨论】:

    • 抱歉乔。我的问题并不完全清楚。我已经相应地更新了它。
    • 请注意adjustToPickerWheelValue 的当前实现中存在一个错误。有一个关于它的雷达:openradar.me/22918650
    【解决方案2】:

    解决方法:

    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 就可以了。

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

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

    【讨论】:

      【解决方案3】:

      日期选择器 (UIDatePicker) 示例

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

      这里是 Picker 视图 (UIPickerView) 的示例

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-04
        • 2016-03-20
        • 1970-01-01
        • 2015-09-24
        • 1970-01-01
        • 2015-12-10
        • 1970-01-01
        相关资源
        最近更新 更多