不确定是否还有人需要这个。但以防万一这可以帮助那里的人......
这是Nexial Automation中代码的改编:
val index = RandomUtils.nextInt(0, 7)
// index = 3
val picker = driver.findElement(By.xpath("//XCUIElementTypePickerWheel"))
val currentPickedValue = picker.getAttribute("value")
val pickerId = picker.id
// if picker already has selection, we'll swipe to top of the dropdown first
if (StringUtils.isNotBlank(currentPickedValue))
driver.executeScript(
scriptScript,
mapOf<String, Any>("element" to pickerId, "direction" to "down", "velocity" to 250))
// magic number 25 (per dropdown option)
val scrollAmount = 25.0 / picker.size.height
for (i in 0 until index)
driver.executeScript(
scriptSelectPickerValue,
mapOf<String, Any>("element" to pickerId, "order" to "next", "offset" to scrollAmount))
代码是用 Kotlin 编写的。如果你了解 Java/JavaScript/C#,那么上面的内容应该是可以理解的。
这个神奇的数字是通过一些在线研究和反复试验得出的。到目前为止似乎工作正常。
我将 index 随机化为 0 到 7 之间的整数。随意将 index 更改为其他值。
此代码仅在 XCUITest 驱动程序上测试过。