Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift XCTest()获取XUIElement的父级_Swift_Automation_Automated Tests_Xctest - Fatal编程技术网

Swift XCTest()获取XUIElement的父级

Swift XCTest()获取XUIElement的父级,swift,automation,automated-tests,xctest,Swift,Automation,Automated Tests,Xctest,这是我在终端窗口中输入po print(xguiapplication().debugDescription)时尝试打印应用程序的元素结构时的示例 tables, (address code like 0x00006b), traits: (some random number) cells, (address code), traits: (random number) others, (address code), traits: (random number), la

这是我在终端窗口中输入
po print(xguiapplication().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"
所以,基本上,如果我想访问
其他
值,我只需要通过它的标签访问:

let cellsValue=xguiapplication().tables.cells.otherElements.staticText[“Other Value”]

但是,问题是,
statictext
可以随着时间的推移而更改,因此我的解决方案是可以访问具有特定标识符的元素,如:

let btnPrimary = XCUIApplication().tables.buttons["primaryBtn"];
并调用它的父元素(本例中为cells元素),如下所示:

因此,我可以使用以下方法轻松访问该元素:

let otherElement = tableParent.otherElements.firstMatch.label

但是
parent()
函数不存在,那么我如何才能访问元素的父元素呢?

我找到了这个问题的解决方案,我在这里发布了它,以防有人和我一样有同样的问题。因此,您不必根据具有唯一标识符的子元素来查找父元素,只需根据XCTest()提供的内容查找具有唯一标识符的元素即可。所以基本上,你必须这样做:

let parent = XCUIApplication().tables.cell.containing(.button, "primaryButton")
以及通过以下方式访问其子女:

let otherValue = parent.otherElements.firstMatch()

它将返回第一个元素的类型为
other
,如果要访问元素索引,请调用
element(boundBy:)
方法

如果孩子没有唯一的id tho,你就不走运了
let otherValue = parent.otherElements.firstMatch()