我可以看到 EarlGrey 用来定位元素的确切 UI 层次结构吗?

Can I see the exact UI hierarchy that EarlGrey uses to locate elements?

我正在编写一个失败并出现以下错误的测试:

Error Domain=com.google.earlgrey.ElementInteractionErrorDomain Code=0 "No element found." UserInfo={NSLocalizedDescription=No element found.}

我可以在生成的屏幕截图中看到我尝试匹配的元素存在。我逐步查看代码,看到 earlgrey 使用元素提供程序。当我的测试失败时,我看到了一个 ui 层次结构转储。我想在特定断点处打印完全相同的 ui 层次结构。我该怎么做?

您可以使用 GREYElementHierarchy class 在 EarlGrey 测试中的任何时候打印元素层次结构。

UIWindow *hierarchy = [[[UIApplication sharedApplication] delegate] window]);

NSLog(@"Hierarchy: %@", hierarchy);

EarlGrey FAQ 一样,您可以在测试中的任何位置添加断点,并且在命中时,运行 在 Xcode 的调试 window:

expression -- print(GREYElementHierarchy.hierarchyStringForAllUIWindows())

这将输出完整的层次结构,因此解析起来会有些混乱。

祝你好运