Ios 如何在XUIElementQuery中获取XUIElement的索引?
这是我的简单Ios 如何在XUIElementQuery中获取XUIElement的索引?,ios,xcode,swift,xcode-ui-testing,ui-testing,Ios,Xcode,Swift,Xcode Ui Testing,Ui Testing,这是我的简单UITest(自定义tabbarcontroller中选项卡的顺序): 注意: 可以通过索引从xguielementquery获取xguielement。我可以用另一种方法来执行此操作吗?似乎查询会根据屏幕上的位置自动返回 for i in 0...tabsQuery.buttons.count { let ele = tabsQuery.buttons.elementBoundByIndex(i) } 其中索引i表示按钮在选项卡栏中的位置,0是最左边的选项卡,i==tab
UITest
(自定义tabbarcontroller中选项卡的顺序):
注意:
可以通过索引从
xguielementquery
获取xguielement
。我可以用另一种方法来执行此操作吗?似乎查询会根据屏幕上的位置自动返回
for i in 0...tabsQuery.buttons.count {
let ele = tabsQuery.buttons.elementBoundByIndex(i)
}
其中索引i表示按钮在选项卡栏中的位置,0是最左边的选项卡,i==tabsqery.buttons.count是最右边的选项卡。创建位置测试有多种方法。最简单的方法是在索引0和1处获取按钮,然后按名称获取两个按钮,并比较数组是否相等:(未经测试编写) 另一个选项是直接获取每个按钮的
帧
,并断言一个X坐标低于另一个X坐标
要回答有关在
xguielementquery
中获取xguielement
索引的特定问题,这是绝对可能的。只需遍历查询中的所有元素,并返回与该元素相等的第一个元素的索引。仅一个XUIElement无法告诉您其在XUIElementQuery中的位置。如果您对XUIElement有所了解,可以搜索XUIElementQuery以发现其偏移量。在下面,让我们假设“More”是标识符(如果您正在使用的是“identifier”,则将“identifier”更改为“label”)
如果要查找“更多”按钮的偏移量(如原始问题中所示),则:
在上述循环退出后,您将获得偏移量,如果找不到“More”,则偏移量将为零。@netshark1000谢谢,已更新为使用
elementAtIndex
。
for i in 0...tabsQuery.buttons.count {
let ele = tabsQuery.buttons.elementBoundByIndex(i)
}
let buttonOrder = [tabBarsQuery.buttons.elementAtIndex(0), tabBarsQuery.buttons.elementAtIndex(1)]
let takeawayButton = buttons["Takeaway"];
let restaurantButton = buttons["In Restaurant"];
XCTAssert(buttonOrder == [takeawayButton, restaurantButton])
var offsetWithinQuery : Int? = nil // optional since it's possible the button isn't found.
for i in 0...tapBarsQuery.buttons.count{
if tapBarsQuery.elementAtIndex(i).indentifier == "More" {
offSetWithinQuery = i
}