XCTest() 获取 XCUIElement 的父级
XCTest() get parent of XCUIElement
这是我在终端 windows
中输入 po print(XCUIApplication().debugDescription)
时尝试打印应用程序元素结构时的示例
tables, (address code like 0x00006b), traits: (some random number)
cells, (address code), traits: (random number)
others, (address code), traits: (random number), label: "Other Value"
buttons, (address code), traits: (random number), identifier: "primaryButton", label: "Press ME"
所以,基本上,如果我想访问 others
值,我只需要通过其标签进行访问即可:
let cellsValue = XCUIApplication().tables.cells.otherElements.staticTexts["Other Value"];
但是,问题是 staticTexts
可以超时更改,所以我的解决方案是我可以访问具有特定标识符的元素,例如:
let btnPrimary = XCUIApplication().tables.buttons["primaryBtn"];
并调用它的父元素(在本例中为单元格元素),例如:
let tableParent = btnPrimary.parent()
这样我就可以使用这个轻松访问该元素:
let otherElement = tableParent.otherElements.firstMatch.label
但是 parent()
函数不存在,那么我怎样才能访问元素的父元素?
我找到了解决这个问题的方法,我把它贴在这里以防有人遇到和我一样的问题。因此,您不必根据具有唯一标识符的 child 找到 parent,您只需要根据 XCTest() 给出的内容找到具有该唯一标识符的元素即可。所以基本上,你必须这样做:
let parent = XCUIApplication().tables.cell.containing(.button, "primaryButton")
并通过以下方式访问他们的 child:
let otherValue = parent.otherElements.firstMatch()
这将 return 第一个元素具有 other
类型,如果你想访问元素索引,调用 element(boundBy: )
方法!
这是我在终端 windows
中输入po print(XCUIApplication().debugDescription)
时尝试打印应用程序元素结构时的示例
tables, (address code like 0x00006b), traits: (some random number)
cells, (address code), traits: (random number)
others, (address code), traits: (random number), label: "Other Value"
buttons, (address code), traits: (random number), identifier: "primaryButton", label: "Press ME"
所以,基本上,如果我想访问 others
值,我只需要通过其标签进行访问即可:
let cellsValue = XCUIApplication().tables.cells.otherElements.staticTexts["Other Value"];
但是,问题是 staticTexts
可以超时更改,所以我的解决方案是我可以访问具有特定标识符的元素,例如:
let btnPrimary = XCUIApplication().tables.buttons["primaryBtn"];
并调用它的父元素(在本例中为单元格元素),例如:
let tableParent = btnPrimary.parent()
这样我就可以使用这个轻松访问该元素:
let otherElement = tableParent.otherElements.firstMatch.label
但是 parent()
函数不存在,那么我怎样才能访问元素的父元素?
我找到了解决这个问题的方法,我把它贴在这里以防有人遇到和我一样的问题。因此,您不必根据具有唯一标识符的 child 找到 parent,您只需要根据 XCTest() 给出的内容找到具有该唯一标识符的元素即可。所以基本上,你必须这样做:
let parent = XCUIApplication().tables.cell.containing(.button, "primaryButton")
并通过以下方式访问他们的 child:
let otherValue = parent.otherElements.firstMatch()
这将 return 第一个元素具有 other
类型,如果你想访问元素索引,调用 element(boundBy: )
方法!