UI XCTest,视图中存在的元素

Kon*_*ušs 2 xcode xctest swift xcode-ui-testing xctestcase

我是UI测试写作的新手.

我想知道是否可以知道视图中存在哪些元素.我想知道它们的数量和方式.

我尝试过这样的东西来循环遍历所有元素,但它不起作用.

for element in app.accessibilityElements! {
        print(element)
    }
Run Code Online (Sandbox Code Playgroud)

Tom*_*min 6

您正在寻找debugDescriptionXCUIElement 的方法,在您的情况下获取当前可见窗口的整个层次结构:

app.debugDescription
Run Code Online (Sandbox Code Playgroud)

引用标题评论:

提供有关该元素的调试信息.字符串中的数据将根据捕获的时间而变化,但可能包括以下任何内容以及其他数据:

   • Values for the elements attributes.
   • The entire tree of descendants rooted at the element.
   • The element's query.
Run Code Online (Sandbox Code Playgroud)

此数据应仅用于调试 - 取决于作为测试的一部分的任何数据不受支持.


Mik*_*ich 5

您可以在测试用例中始终有一个断点,然后对控制台进行一些打印调用.

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) }
Run Code Online (Sandbox Code Playgroud)

这将告诉您该元素是否在屏幕上可见,因为该element.exist调用将返回true,即使该元素在屏幕外并且尚未向用户显示(即,如果某些内容隐藏在屏幕上然后转换到框架中)