【问题标题】:UI XCTest, elements which exist in viewUI XCTest,视图中存在的元素
【发布时间】:2016-08-28 10:37:47
【问题描述】:
我是 UI 测试写作的新手。
我想知道是否有可能知道哪些元素是/存在于视图中。我想知道有多少以及如何称呼它们。
我尝试过这样的方法来遍历所有元素,但它不起作用。
for element in app.accessibilityElements! {
print(element)
}
【问题讨论】:
标签:
xcode
swift
xctest
xcode-ui-testing
xctestcase
【解决方案1】:
您总是可以在测试用例中设置一个断点,然后对控制台进行一些打印调用。
po app.accessibilityElements
po app.accessibilityElements.elementBoundByIndex(0)
po app.buttons["Icon Menu Light"]
etc.
查看输出视图层次结构中返回的内容以供参考,或者只需简单调用po app 即可打印层次结构。
一旦你知道一个特定的视图存在..app.buttons["Icon Menu Light"].exists..你可以尝试使用类似这样的东西来确认按钮/元素在当前视图中是可见的,方法是将它传递给一个帮助函数,比如:
func isElementInView(element: XCUIElement) -> Bool {
let window = XCUIApplication().windows.elementBoundByIndex(0)
return CGRectContainsRect(window.frame, element.frame) }
这将告诉您该元素是否在屏幕上可见,因为element.exist 调用将返回 true,即使该元素不在屏幕上并且尚未向用户显示(即,如果某些东西在屏幕外隐藏然后转换进入框架)
【解决方案2】:
您正在寻找 XCUIElement 的 debugDescription 方法,以获取当前可见窗口的整个层次结构:
app.debugDescription
引用标题 cmets:
提供有关元素的调试信息。字符串中的数据将根据
它被捕获的时间,但它可能包括以下任何一项以及其他数据:
• Values for the elements attributes.
• The entire tree of descendants rooted at the element.
• The element's query.
此数据应仅用于调试 - 取决于作为测试一部分的任何数据不受支持。