【问题标题】:How to select a picker view item in an iOS UI test in Xcode?如何在 Xcode 的 iOS UI 测试中选择选择器视图项?
【发布时间】:2015-09-24 06:46:34
【问题描述】:

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

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

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 开始的选择器视图。

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

【问题讨论】:

  • 真的很想知道答案。我面临着类似的问题。当然我们不能依赖生成的选择器代码。
  • 在 OSX 中尚不支持。呃。

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


【解决方案1】:

如问题更新中所述,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

【讨论】:

  • 有一个关于这个openradar.me/22918650 的公开 rdar 这是如何工作的?
  • .adjust(toPickerWheelValue: "foo") 不适合我
【解决方案2】:

如果有多个轮子,选择项目的简单方法如下:

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

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

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

【讨论】:

  • 使用绝对索引时要小心本地化。月轮和日轮可能会有所不同,具体取决于设备所在的本地(例如,我们和德国本地人有不同的布局)
  • 这不起作用。我总是收到:“UI 测试失败 - 内部错误:无法在可能的值 thing、thing2、thing3 中找到当前值 'Something, 1 of 3' 用于拾取轮“Something, 1 of 3” PickerWheel”
  • :索引“0”是月份,“1”是天,“2”是年...适用于所有国家/地区配置?
  • 这在英语以外的地方不起作用。在丹麦语中,march 的值是“marts”,在法语中是“mars”。示例代码在两者中都将失败。我没有测试过这个顺序,但是提到的两种语言都是按日-月-年排序的,而不是月-日-年,所以这也可能是一个问题
  • app.pickerWheels.element(boundBy: 0).adjust(toPickerWheelValue: NSLocalizedString("January", comment: ""))
【解决方案3】:

@Joao_dche的回答的 Swift 4 版本

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

【讨论】:

  • 这在 Swift 4 中是一样的
【解决方案4】:

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

【讨论】:

    【解决方案5】:

    Joe's answer 在 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()
    

    【讨论】:

      【解决方案6】:

      这是 select pickervie(UIPickerView) 最受欢迎的帖子之一

      如果您想从选择器视图中选择州/日期。您可以尝试以下 swift 3 代码。

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

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

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

      【讨论】:

        【解决方案7】:

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

        在选取器中查找元素

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

        在选择轮中选择一个值:

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

        【讨论】:

          猜你喜欢
          • 2017-10-14
          • 2015-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多