我是 UI 测试写作的新手。
我想知道是否有可能知道哪些元素是/存在的。我想知道有多少以及如何称呼它们。
我尝试过这样的事情来遍历所有元素,但它不起作用。
for element in app.accessibilityElements! {
print(element)
}
我是 UI 测试写作的新手。
我想知道是否有可能知道哪些元素是/存在的。我想知道有多少以及如何称呼它们。
我尝试过这样的事情来遍历所有元素,但它不起作用。
for element in app.accessibilityElements! {
print(element)
}
您正在寻找debugDescription
XCUIElement 的方法,以获取当前可见窗口的整个层次结构:
app.debugDescription
引用标题评论:
提供有关元素的调试信息。字符串中的数据将根据捕获的时间而有所不同,但它可能包括以下任何一项以及其他数据:
• Values for the elements attributes. • The entire tree of descendants rooted at the element. • The element's query.
此数据应仅用于调试 - 取决于作为测试一部分的任何数据不受支持。
您总是可以在测试用例中有一个断点,然后对控制台进行一些打印调用。
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,即使该元素不在屏幕上并且尚未向用户显示(即,如果某些东西隐藏在屏幕外然后过渡到框架中)