xcode UI 测试标签栏控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40230282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UI testing a tab bar controller
提问by Josh
I have built a simple tab bar with 3 tabs.
我构建了一个带有 3 个标签的简单标签栏。
I want to run a UI test to make sure that if the user clicks a tab bar item, the correct view controller shows. How would I go about doing that? Below is the code I would start with, just don't know how to write my assertion.
我想运行 UI 测试以确保如果用户单击选项卡栏项,则显示正确的视图控制器。我该怎么做呢?下面是我要开始的代码,只是不知道如何写我的断言。
func testTabBarMyProfileButton() {
let tabBarsQuery = XCUIApplication().tabBars
tabBarsQuery.buttons["My Profile"].tap()
}
func testTabBarGraphsButton() {
let tabBarsQuery = XCUIApplication().tabBars
tabBarsQuery.buttons["Graphs"].tap()
}
func testTabBarAboutButton() {
let tabBarsQuery = XCUIApplication().tabBars
tabBarsQuery.buttons["About"].tap()
}
采纳答案by Amjad Husseini
If you have different controls in each view controller shown on each tab bar, you can make assertions if they exist or not (what is expected). For example if the first tab bar has UILabel named "First name" you can assert if it exists by writing
如果在每个选项卡栏上显示的每个视图控制器中都有不同的控件,则可以断言它们是否存在(预期)。例如,如果第一个标签栏有名为“名字”的 UILabel,您可以通过编写来断言它是否存在
Let theLabel = app.staticTexts["myValue"]
XCTAssert(theLabel.exists).to(beTrue)
And on the other screens do the same thing for the different controls.
在其他屏幕上为不同的控件做同样的事情。
回答by PSchuette
If anyone finds this looking to UI test the contents of another app, I just found a solution..
如果有人发现这希望 UI 测试另一个应用程序的内容,我刚刚找到了一个解决方案..
The tab bar item is a lazy variable and needs to be touched before you can reference a tab bar button by value. Add this line:
标签栏项是一个惰性变量,需要先触摸,然后才能按值引用标签栏按钮。添加这一行:
tabBarItem.accessibilityIdentifier = "my-snazzy-identifier"
to the viewDidLoad method and you should be able to do this in your UI tests:
到 viewDidLoad 方法,你应该能够在你的 UI 测试中做到这一点:
app.tabBars.buttons["Button Title"].tap()
回答by Marc Fdn
You can access the tabbar button by its position:
您可以通过其位置访问标签栏按钮:
app.tabBars.buttons.element(boundBy: 2).tap()
app.tabBars.buttons.element(boundBy: 2).tap()
回答by Joe Masilotti
You can test the title of the navigation bar.
您可以测试导航栏的标题。
XCTAssert(app.navigationBars["Graphs"].exists)