我刚开始使用 Subliminal 编写集成测试.我想测试用户点击按钮后是否显示某个 View Controller 。最好的测试方法是什么?
- 我可以使用 Subliminal 获取最顶层的 View Controller 并找到它的标题吗?
- 我可以得到最顶层的 View Controller 并比较它的类吗?
- Navigation Controller 的标题标签可以吗?
目前我只能通过其可访问性标签获取导航 Controller 的标题标签,如下所示:
SLElement *titleLabel = [SLElement elementWithAccessibilityLabel:@"欢迎光临"];
SLAssertTrue([[UIAElement(titleLabel) label] isEqualToString:@"Welcome"], @"它应该显示欢迎屏幕");
但是如果 @"Welcome"== @"Welcome"
之后我会测试。 欢迎标签也可能出现在其他 View Controller 的 View 层次结构中。
如果在用户操作后显示的 View Controller 确实是我期望的 View Controller ,您将采用什么方法来测试和断言?
谢谢!
最佳答案
要在您的可访问性层次结构中识别无法通过其可访问性标签、值和特征的组合来描述(或无法唯一描述)的对象,您最好的选择是使用 [SLElement elementMatching:withDescription:]
。方法是这样使用的:
SLElement *titleLabel = [SLElement elementMatching:^BOOL(NSObject *obj) {
// Code to test objects (obj parameter) in the accessibility hierarchy,
// and return YES or NO depending on whether or not obj is the particular
// object you're looking for.
} withDescription:@"A description of the view for Subliminal to use in logs/error reports"];
匹配 block 中的内容取决于您的应用程序的结构以及您要识别的内容,但请记住,您可以在其中放置基本上任意的代码来评估输入可访问性元素 obj
.您可以检查 obj.accessibilityLabel
并且如果该标签等于 @"Welcome"
然后(验证 obj
是 UIView
) 并在 View 层次结构中向上走,寻找特定的 View 或类(可能是 UINavigationBar
)来证明 obj
存在于您的导航 Controller 中。或者您可以只测试 obj
的类并查找仅出现在您尝试验证的特定导航 Controller 中的内容。
另一种可以用来解决此类问题的工具是潜意识 app-hook .您可以在您的应用程序中注册对象(类 或 特定实例)以响应来自 Subliminal 测试的消息。因此,例如,如果您的应用程序委托(delegate)知道是否显示了此 View Controller ,则您可以注册应用程序委托(delegate)以响应指示是否显示 View Controller 的消息,并且在您的测试代码中您可以使用SLAskAppYesNo
以查明是否显示了 View Controller 。
关于ios - 如何在 Subliminal 中断言 UINavigationController 的标题文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20098964/