UI 测试 - 如何检查我是否在正确的控制器中?
UI testing - how to check that I am in right controller?
我的控制器中有按钮,单击按钮后我发出请求并检查结果。如果它是错误的,而不是我显示警报,否则我会执行到另一个控制器的 segue。我想对此进行 UI 测试。这就是我的测试方法的结尾:
func testExample() {
...
elementsQuery.buttons["CreateAccount"].tap()
XCTAssertEqual(app.navigationBars.element.identifier, "ProgrammeViewController")
}
我在最后一行中断了。我想我没有设置正确。但我不确定如何设置它。我在那个控制器中有标题,但我为每种语言更改了它。所以我想用accessibilityLabel
。我试过了:
self.navigationController?.navigationBar.accessibilityLabel = "ProgrammeViewController"
self.navigationItem.accessibilityLabel = "ProgrammeViewController"
但这并没有帮助。那么这样做的正确方法是什么?我应该在 viewDidLoad
或 viewWillAppear
中设置 accessibilityLabel
吗?我想如果我想将它用于多个控制器,那么 viewWillAppear
会更好,对吗?感谢帮助
简答:
设置accessibilityIdentifier
而不是accessibilityLabel
self.navigationController?.navigationBar.accessibilityIdentifier = "ProgrammeViewController"
长答案:
来自 UIAccessibilityIdentification protocol:
The UIAccessibilityIdentification protocol is used to associate a unique identifier with elements in your user interface. You can use the identifiers you define in UI Automation scripts ...
这篇文档有点过时了,因为它讨论了 UI Automation(Apple 的 UI Testing framework before Xcode 7)。重点是,对于UI测试,建议使用本协议中定义的属性(accessibilityIdentifier
)
并且,来自 UIAccessibility protocol 上的 accessibilityLabel:
If an accessibility element does not display a descriptive label, set this property to supply a short, localized label that succinctly identifies the element. For example, a “Play music” button might display an icon that shows sighted users what it does. To be accessible, however, the button should have the accessibility label “Play” or “Play music” so that an assistive application can provide this information to users with disabilities.
所以accessibilityLabel
是为了帮助残障人士。
讨论:虽然你可以在UI测试代码中同时使用accessibilityLabel
和accessibilityIdentifier
,因为毕竟UI 测试就像用户浏览我们的应用程序,如果灵魂目的是识别元素,请不要使用 label。
总是使用accessibilityIdentifier
来识别一个元素
accessibilityLabel
是不是来识别元素。这是为了帮助残疾人。测试它是否设置正确是一个有效的测试。例如,可以为 Stop 图标编写一个测试,检查 accessibilityLabel
因为它确实是 "Stop"
(或本地化变体)而不是其他字符串。
理想情况下,accessibilityLabel
应该根据用户的语言环境改变,"Stop"
应该变成"رکیے"
在乌尔都语。但是,accessibilityIdentifier
应该保持不变。用户永远看不到它。它供程序员您识别 UI 测试中的元素。 accessibilityIdentifier = "MusicPlayerController"
将保持 "MusicPlayerController"
而不管用户的区域设置如何。
在哪里设置accessibilityIdentifier
在viewWillAppear
或viewDidLoad
中设置都无所谓,只要在UI显示时设置即可。我倾向于在 Storyboard(或 .Xib)中设置这些。并非所有控件都在 Storyboard 中公开此字段,例如UITableView
,对于这些我设置在viewDidLoad
.
这只是对已给出答案的扩展。就像@ishaq 已经说过的那样,故事板不会为每个组件提供 Accessibility
属性。但是您也可以使用 User Defined Runtime Attributes
在故事板中轻松设置 accessibilityIdentifier
。
我的控制器中有按钮,单击按钮后我发出请求并检查结果。如果它是错误的,而不是我显示警报,否则我会执行到另一个控制器的 segue。我想对此进行 UI 测试。这就是我的测试方法的结尾:
func testExample() {
...
elementsQuery.buttons["CreateAccount"].tap()
XCTAssertEqual(app.navigationBars.element.identifier, "ProgrammeViewController")
}
我在最后一行中断了。我想我没有设置正确。但我不确定如何设置它。我在那个控制器中有标题,但我为每种语言更改了它。所以我想用accessibilityLabel
。我试过了:
self.navigationController?.navigationBar.accessibilityLabel = "ProgrammeViewController"
self.navigationItem.accessibilityLabel = "ProgrammeViewController"
但这并没有帮助。那么这样做的正确方法是什么?我应该在 viewDidLoad
或 viewWillAppear
中设置 accessibilityLabel
吗?我想如果我想将它用于多个控制器,那么 viewWillAppear
会更好,对吗?感谢帮助
简答:
设置accessibilityIdentifier
而不是accessibilityLabel
self.navigationController?.navigationBar.accessibilityIdentifier = "ProgrammeViewController"
长答案:
来自 UIAccessibilityIdentification protocol:
The UIAccessibilityIdentification protocol is used to associate a unique identifier with elements in your user interface. You can use the identifiers you define in UI Automation scripts ...
这篇文档有点过时了,因为它讨论了 UI Automation(Apple 的 UI Testing framework before Xcode 7)。重点是,对于UI测试,建议使用本协议中定义的属性(accessibilityIdentifier
)
并且,来自 UIAccessibility protocol 上的 accessibilityLabel:
If an accessibility element does not display a descriptive label, set this property to supply a short, localized label that succinctly identifies the element. For example, a “Play music” button might display an icon that shows sighted users what it does. To be accessible, however, the button should have the accessibility label “Play” or “Play music” so that an assistive application can provide this information to users with disabilities.
所以accessibilityLabel
是为了帮助残障人士。
讨论:虽然你可以在UI测试代码中同时使用accessibilityLabel
和accessibilityIdentifier
,因为毕竟UI 测试就像用户浏览我们的应用程序,如果灵魂目的是识别元素,请不要使用 label。
总是使用accessibilityIdentifier
来识别一个元素
accessibilityLabel
是不是来识别元素。这是为了帮助残疾人。测试它是否设置正确是一个有效的测试。例如,可以为 Stop 图标编写一个测试,检查 accessibilityLabel
因为它确实是 "Stop"
(或本地化变体)而不是其他字符串。
理想情况下,accessibilityLabel
应该根据用户的语言环境改变,"Stop"
应该变成"رکیے"
在乌尔都语。但是,accessibilityIdentifier
应该保持不变。用户永远看不到它。它供程序员您识别 UI 测试中的元素。 accessibilityIdentifier = "MusicPlayerController"
将保持 "MusicPlayerController"
而不管用户的区域设置如何。
在哪里设置accessibilityIdentifier
在viewWillAppear
或viewDidLoad
中设置都无所谓,只要在UI显示时设置即可。我倾向于在 Storyboard(或 .Xib)中设置这些。并非所有控件都在 Storyboard 中公开此字段,例如UITableView
,对于这些我设置在viewDidLoad
.
这只是对已给出答案的扩展。就像@ishaq 已经说过的那样,故事板不会为每个组件提供 Accessibility
属性。但是您也可以使用 User Defined Runtime Attributes
在故事板中轻松设置 accessibilityIdentifier
。