如何在Xcode中的iOS UI测试中选择选择器视图项? [英] How to select a picker view item in an iOS UI test in Xcode?

查看:178
本文介绍了如何在Xcode中的iOS UI测试中选择选择器视图项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a picker view with few items: "Red", "Green", "Yellow", "Black". In my UI test I need to select a specific item "Green" from it. I am using the XCTest UI testing APIs that were intruduced with Xcode 7.

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

What I managed to do so far is to swipe the whole picker view up in the unit test. It is not ideal because it always changes the picker view to the bottom item (when swiping up).

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

更改选择器视图的另一种非常类似的方法是调用 pressForDuration ... thenDragToElement ,这不是我想要的。

Another but very similar way of changing the picker view is to call pressForDuration ... thenDragToElement, which is not what I want.

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

当我使用UI测试记录功能时,它会不记录选择器视图滚动事件。当我点击选择器查看项目时它会记录:

When I use the UI test record function it does not record the picker view scrolling events. It does record when I tap on picker view items:

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

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

but that does not actually work when the test is run (probably because it needs to scroll the picker view first before tapping).

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

Here is the demo app with the test.

https://github.com/exchangegroup/PickerViewTestDemo

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

It is now possible to select a picker view since Xcode 7.0 beta 6 .

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


推荐答案

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

As noted in the question's update, Xcode 7 Beta 6 added support for interacting with pickers. The newly added method -adjustToPickerWheelValue: should be used to select items on a UIPickerView.

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

这是一个 GitHub repo with一个有效的例子。还有一些我写的博客文章中的更多信息

这篇关于如何在Xcode中的iOS UI测试中选择选择器视图项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆