I am testing my application's interface, using Swift 3 and I use xCode 8 .
However, I need to make a user registration before starting my test cases and when I finish all the tests I must delete the created user, in order not to affect the database.
I see that it is possible to make an equivalent of beforeClass
and afterClass
of Android in iOS . Apparently, just overwrite the class method setUp()
and tearDown()
.
How to run one-time setup code before executing any XCTest
However, I could not understand how to put the code that I need to run in order to create my user. Is there any way to access the code of my application? o Do I need to register my user through the interface?
I enclose my current code:
class LoginUITests: XCTestCase {
let app = XCUIApplication()
override class func setUp() {
super.setUp()
print("setUp - Se ejecuta una vez, antes de TODOS los casos de prueba")
// Called once before all tests are run
// Intento ejecutar el registro de un usuario al menos por medio de
// la interfaz
// registerUser()
}
override class func tearDown() {
super.setUp()
print("tearDown - Se ejecuta una vez, despues de TODOS los casos de prueba")
// Called once after all tests are run
// Intento ejecutar el unregister de un usuario al menos por medio de
// la interfaz
// unRegisterUser()
}
override func setUp() {
super.setUp()
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
app.launch()
XCUIDevice.shared().orientation = .portrait
app.tabBars.buttons["Generals"].tap()
XCTAssertTrue(app.tables.cells.staticTexts["Identificate para sincronizar tus conversaciones"].exists)
app.tables.staticTexts["Identificate para sincronizar tus conversaciones"].tap()
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
let tabBarsQuery = app.tabBars
if (tabBarsQuery.buttons.count >= 4){
tabBarsQuery.buttons["Yo"].tap()
app.scrollViews.otherElements.buttons["logOut"].tap()
} else {
print("No hay sesión en curso")
}
}
func registerUser(){
let dict : [String:Any] = getDictionary()!
let defaultEmail : String = dict["defaultEmail"] as! String
let defaultName : String = dict["defaultName"] as! String
let defaultPassword : String = dict["defaultPassword"] as! String
let app = XCUIApplication()
app.launch()
XCUIDevice.shared().orientation = .portrait
app.tabBars.buttons["Generals"].tap()
XCTAssertTrue(app.tables.cells.staticTexts["Identificate para sincronizar tus conversaciones"].exists)
app.tables.staticTexts["Identificate para sincronizar tus conversaciones"].tap()
let tabBarsQuery = app.tabBars
let emailTextField = app.textFields["email"]
emailTextField.tap()
emailTextField.typeText(defaultEmail)
app.typeText("\n")
let registerButton = app.buttons["register"]
registerButton.tap()
let userNameTextField = app.textFields["username"]
let passwordTextField = app.secureTextFields["password"]
let repeatPassTextField = app.secureTextFields["repeatPassword"]
//Ingresamos campos para el registro
userNameTextField.tap()
userNameTextField.typeText(defaultName)
app.typeText("\n")
passwordTextField.tap()
passwordTextField.typeText(defaultPassword)
app.typeText("\n")
repeatPassTextField.tap()
repeatPassTextField.typeText(defaultPassword)
app.typeText("\n")
registerButton.tap()
tabBarsQuery.buttons["Generals"].tap()
XCTAssertFalse(app.tables.cells.staticTexts["Identificate para sincronizar tus conversaciones"].exists)
}
func unRegisterUser(){
//Autenticar al usuario
//Posteriormente, darlo de baja
}
func testLoginEmail(){
//....
}
func testLoginInvalidEmail(){
//...
}
When I wanted to invoke registerUser()
, I necessarily had to instantiate my class LoginUITests
, then put: LoginUITests().registerUser()
, but this also did not work so I could make a record before all my tests.
Is the implementation of setUp()
and tearDown()
correct, or am I failing something?