我想在我的項目中設置UI測試。我正在進行UI測試,嘗試通過我的應用程序登錄提示登錄。爲了確保在測試啓動時顯示登錄提示,我試圖運行ServerManager.logout()
,它位於項目的代碼庫中。這將導致在啓動時顯示登錄提示。從XCTestCase訪問項目代碼 - UI測試
import XCTest
class SmokeTest: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
XCUIApplication().launch()
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
ServerManager.logout()
}
func testLogin() {
let app = XCUIApplication()
let emailTextField = app.textFields["email textfield"]
emailTextField.tap()
emailTextField.typeText("[email protected]")
let passwordTextField = app.secureTextFields["password textfield"]
passwordTextField.tap()
passwordTextField.typeText("12345678")
let loginButton = app.buttons["log in button"]
loginButton.tap()
}
}
我應該如何設置我的項目去ServerManager
訪問?
當我在目標成員檢查在ServerManager.swift文件UITest目標(稱爲DonkeyProductionUITests)時,Xcode開始抱怨,在該文件中許多提法是不確定的UITest目標。因此,我將項目中所有文件的Target Membership添加到UITest Target,包括所有CocoaPods。它解決了大部分問題。不過我有一些奇怪的剩菜:
應UITest目標必須爲「編譯源代碼」是什麼源文件?
爲什麼類型,如UIColor
和UIViewController
突然不Xcode的識別器?
太棒了,謝謝!但有點惱人..我只是想確保應用程序在測試啓動時處於正確的狀態,但這似乎不是'func setup()'的用例...您是否介意添加鏈接來自蘋果的參考? – Wiingaard
是的,這有點不方便,但是沒有辦法。測試後,您必須使用應用的用戶界面重置您的應用。如果你需要在每次測試後做到這一點,你可以將該代碼移動到'tearDown()'。這裏是鏈接到文檔:https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/09-ui_testing.html – joern
你不覺得這是一個解決方案啓動應用程序的參數,像這樣的解決方案:http://stackoverflow.com/a/33466038/2299801謝謝:) – Wiingaard