如何在 Xcode 的 iOS UI 测试中选择选择器视图项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31257409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select a picker view item in an iOS UI test in Xcode?
提问by Evgenii
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.
我有一个包含几个项目的选择器视图:“红色”、“绿色”、“黄色”、“黑色”。在我的 UI 测试中,我需要从中选择一个特定的项目“绿色”。我正在使用 Xcode 7 引入的 XCTest UI 测试 API。
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)
Another but very similar way of changing the picker view is to call pressForDuration ... thenDragToElement
, which is not what I want.
更改选择器视图的另一种但非常相似的方法是调用pressForDuration ... thenDragToElement
,这不是我想要的。
app.pickers.elementAtIndex(0).pressForDuration(0.1, thenDragToElement: someElement)
When I use the UI test recordfunction it does not record the picker view scrolling events. It does record when I tap on picker view items:
当我使用 UI 测试记录功能时,它不会记录选择器视图滚动事件。当我点击选择器视图项目时,它会记录:
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
https://github.com/exchangegroup/PickerViewTestDemo
Update
更新
It is now possible to select a picker view since Xcode 7.0 beta 6 .
从 Xcode 7.0 beta 6 开始,现在可以选择选择器视图。
app.pickerWheels["Green"].adjustToPickerWheelValue("Yellow")
回答by Joe Masilotti
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
.
如问题更新中所述,Xcode 7 Beta 6 添加了对与选择器交互的支持。新添加的方法-adjustToPickerWheelValue:
应该用于选择UIPickerView
.
let app = XCUIApplication()
app.launch()
app.pickerWheels.element.adjustToPickerWheelValue("Yellow")
Here's a GitHub repo with a working example. And some more information in a blog post I wrote.
这是一个带有工作示例的GitHub 存储库。以及我写的博客文章中的更多信息。
回答by Joao_dche
If there are multiple wheels, an easy way to select items it is like this:
如果有多个轮子,一个简单的选择项目的方法是这样的:
precondition: it's a date picker (UIDatePicker), swift language
先决条件:它是一个日期选择器(UIDatePicker),快速语言
app.pickerWheels.elementBoundByIndex(0).adjustToPickerWheelValue("March")
app.pickerWheels.elementBoundByIndex(1).adjustToPickerWheelValue("13")
app.pickerWheels.elementBoundByIndex(2).adjustToPickerWheelValue("1990")
where: index "0" is month, "1" is day, "2" is year
其中:索引“0”是月,“1”是日,“2”是年
回答by radioaktiv
Swift 4 version of @Joao_dche's answer
@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")
回答by Debo
Objective-CVersion of @Joao_dche's Answer
@Joao_dche答案的Objective-C版本
Using UIDatePicker with XCTest for UI Testing:
使用 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];
回答by Pavel Vavilov
Joe's answerworking great on iOS 11, but doesn't work for me on iOS 10.3.1 [Xcode 9.2]
Joe 的回答在 iOS 11 上效果很好,但在 iOS 10.3.1 [Xcode 9.2] 上对我不起作用
I use custom views (labels actually) as picker rows and unfortunately gorgeous Joe's answer doesn't workfor me on iOS 10.3.1 while working perfectlyon iOS 11. So I had to use
我使用自定义视图(实际上是标签)作为选择器行,不幸的是,华丽的乔的答案在 iOS 10.3.1 上对我不起作用,而在 iOS 11 上完美运行。所以我不得不使用
app.pickerWheels["X, Y of Z"].swipeUp()
where
在哪里
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.
So in my case this command looks like
所以在我的情况下,这个命令看起来像
app.pickerWheels["1st, 1 of 28"].swipeUp()
回答by N..
This is one of most popular post for select pickervie(UIPickerView)
这是 select pickervie(UIPickerView) 最受欢迎的帖子之一
If you want to select state/date from picker view. You can try following swift 3 code.
如果您想从选择器视图中选择状态/日期。您可以尝试遵循 swift 3 代码。
XCUIApplication().tables.pickerWheels["AK"].adjust(toPickerWheelValue: "MA")
PickerWheels pick from AK and set the target state.
PickerWheels 从 AK 中拾取并设置目标状态。
XCUIApplication().tables.pickerWheels["Starting Point""].adjust(toPickerWheelValue: "Target Place")
XCUIApplication().tables.pickerWheels[ "起点""].adjust(toPickerWheelValue: " Target Place")
回答by Sujananth
I used below code to identify the element displayed now in the picker wheel.
我使用下面的代码来识别现在显示在选择器轮中的元素。
To Find an element in picker
在选择器中查找元素
let valueSelected = algorithmsPicker.pickerWheels.element(boundBy: 0).value as! String
To select a value in the picker wheel:
要在选择器轮中选择一个值:
algorithmsPicker.pickerWheels[valueSelected].adjust(toPickerWheelValue: "BubbleSort")