I'm trying to setup UI Test in my project. I'm making a UI test that tries to login through my apps login prompt. In order to ensure that the login prompt is shown when the test launches, I'm trying to run ServerManager.logout()
, which is in the project's codebase. This will cause the Login prompt to be shown on launch.
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()
}
}
How should I set up my project to get access to ServerManager
?
When I checked on Target Membership for UITest target (called DonkeyProductionUITests) on the ServerManager.swift file, Xcode started complaining that many reference in that file was undefined for the UITest target. So I added Target Membership for all files in my project to the UITest Target including all CocoaPods. It solved most issues. Still I have some weird leftovers:
What source files should the UITest target have as "Compile Sources"?
Why are types like UIColor
and UIViewController
suddenly not recognizer by Xcode?
The easiest way to add a unit test target to your project is to select the Include Tests checkbox when you create the project. Selecting the checkbox creates targets for unit tests and UI tests. To add a unit test target to an existing Xcode project, choose File > New > Target.
Go to File > New > Target. Then, search for UI Testing Bundle. Select Unit Testing Bundle and then click Next. As UI tests take time, it is usually best to stop immediately when a failure occurs.
Accessing your project's code via @testable import
is only possible in UnitTests. When you are running UITests this is not working, because during a UITest your test class cannot access your app's code.
From Apple's Docs:
UI testing differs from unit testing in fundamental ways. Unit testing enables you to work within your app's scope and allows you to exercise functions and methods with full access to your app's variables and state. UI testing exercises your app's UI in the same way that users do without access to your app's internal methods, functions, and variables. This enables your tests to see the app the same way a user does, exposing UI problems that users encounter.
If you want to logout after a test you have to do it via your app's User Interface: If there is a logout button somewhere in your app, navigate there at the end of your test and let the test tap()
it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With