xcode 如何在 XCUIElementQuery 中获取 XCUIElement 的索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32164956/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:42:47  来源:igfitidea点击:

How to get index of XCUIElement in XCUIElementQuery?

iosxcodeswiftxcode-ui-testingui-testing

提问by Bart?omiej Semańczyk

This is my simple UITest(customizing order of tabs in tabbarcontroller):

这是我的简单UITest(在 tabbarcontroller 中自定义选项卡的顺序)

func testIsOrderOfTabsSaved() {

    let app = XCUIApplication()
    let tabBarsQuery = app.tabBars
    tabBarsQuery.buttons["More"].tap()
    app.navigationBars["More"].buttons["Edit"].tap()
    tabBarsQuery.buttons["Takeaway"].swipeLeft()
    tabBarsQuery.buttons["In Restaurant"].swipeLeft()

    //here, how to get position of moved button with text "In Restaurant"?

NOTE:

注意

It is possible to get XCUIElementfrom XCUIElementQueryby index. Can I do this fro the other way?

它有可能获得XCUIElementXCUIElementQuery通过索引。我可以用另一种方式做到这一点吗?

回答by Alex

It seems that the queries automatically return in order based on position on screen.

似乎查询会根据屏幕上的位置自动按顺序返回。

for i in 0...tabsQuery.buttons.count {
    let ele = tabsQuery.buttons.elementBoundByIndex(i)
}

Where the index i represents the position of the button in the tab bar, 0 being the leftmost tab, i == tabsQuery.buttons.count being the rightmost.

其中索引 i 表示按钮在选项卡栏中的位置,0 是最左边的选项卡, i == tabsQuery.buttons.count 是最右边的。

回答by Sulthan

You have various ways to create a position test. The simplest way is to get buttons at indices 0 and 1, then get two buttons by name and compare the arrays are equal: (written without testing)

您可以通过多种方式创建职位测试。最简单的方法是在索引 0 和 1 处获取按钮,然后按名称获取两个按钮并比较数组是否相等:(未测试而编写)

 let buttonOrder = [tabBarsQuery.buttons.elementAtIndex(0), tabBarsQuery.buttons.elementAtIndex(1)]

 let takeawayButton = buttons["Takeaway"];
 let restaurantButton = buttons["In Restaurant"];

 XCTAssert(buttonOrder == [takeawayButton, restaurantButton])

Another option is to directly get the frameof each button and assert that one X coordinate is lower than the other.

另一种选择是直接获取frame每个按钮的 并断言一个 X 坐标低于另一个。

To answer your specific question about getting the index of an XCUIElementin a XCUIElementQuery, that's absolutely possible. Just go through all the elements in the query and return the index of the first one equal to the element.

要回答有关XCUIElement在 a 中获取 an 索引的具体问题XCUIElementQuery,这是绝对有可能的。只需遍历查询中的所有元素并返回与该元素相等的第一个元素的索引。

回答by Lance Kind

An XCUIElement alone isn't able to tell you its position in within the XCUIElementQuery. You can search the XCUIElementQuery to discover its offset if you know something about the XCUIElement. In the below let's imagine that "More" is the identifier (change 'identifier' to 'label' if that is what you're working with).

单独的 XCUIElement 无法告诉您它在 XCUIElementQuery 中的位置。如果您对 XCUIElement 有所了解,则可以搜索 XCUIElementQuery 以发现其偏移量。在下面,让我们假设“更多”是标识符(如果您正在使用,请将“标识符”更改为“标签”)。

If you want to find the offset of the "More" button (as posted in the original question) then:

如果您想找到“更多”按钮的偏移量(如原始问题中所述),则:

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
}

After the above loop exits, you'll either have the offset or it'll be nil if "More" isn't found.

在上述循环退出后,您将获得偏移量,或者如果未找到“更多”,它将为零。