XCUITest — Test Automation Organization

import XCTestclass LoginUITest: TestBase {
private var hasRunAtLeastOnce = false
public
lazy var login = Login()
override func setUpWithError() throws {
try super.setUpWithError()
if hasRunAtLeastOnce == false {
hasRunAtLeastOnce = true
XCUIApplication().launch()
self.login
.navigateToLogin()
}
}
override func tearDown() {
super.tearDown()
}
func testLoginButtonDisabledWhenUserNameEmpty() {
addTeardownBlock {
self.login
.clearPasswordField()
}
login
.enterPasscode(“123”)
.verifyLoginButtonIsDisabled()
}
func testLoginButtonDisabledWhenPasswordEmpty() {
addTeardownBlock {
self.login
.clearUserNameField()
}
login
.enterUsername(“sally”)
.verifyLoginButtonIsDisabled()
}
func testLoginButtonDisabledWhenBothFieldsEmpty()
addTeardownBlock {
self.login
.clearUserNameField()
.clearPasswordField()
}
login
.verifyLoginButtonIsDisabled()
}
func testLoginButtonEnabledWhenBothFieldsFilled() {
addTeardownBlock {
self.login
.clearUserNameField()
.clearPasswordField()
}
login
.enterLoginInfo(user: TestPatients.users[5])
.dismissKeyboard()
.verifyLoginButtonIsEnabled()
}
}
import Foundation
import XCTest
class Login {
private lazy var alertHelper = AlertHelper()
private lazy var dashboard = Dashboard()
private lazy var more = More()
private lazy var onboarding = Onboarding()
private lazy var tabBar = TabBar()
private lazy var termsAndConditions = TermsAndConditions()
@discardableResult
func
clearUserNameField() -> Self {
clearTextField(textField: LoginScreen.usernameTextField.element)
return self
}
@discardableResult
func
dismissKeyboard() -> Self
LoginScreen.loginLabel.element.tap()
return self
}
@discardableResult
func
enterLoginInfo(user: Patient) -> Self {
waitForLoginButtonToExist()
LoginScreen.usernameTextField.element.tap()
LoginScreen.usernameTextField.element.typeText(user.username)
waitForPasswordFieldToExist()
LoginScreen.passwordTextField.element.tap()
LoginScreen.passwordTextField.element.typeText(user.password)
return self
}
@discardableResult
func
verifyLoginButtonIsDisabled() -> Self
XCTAssertFalse(LoginScreen.loginButton.element.isEnabled)
return self
}
@discardableResult
func
waitForLoginButtonToExist() -> Self
WaitHelper.waitFor(existence: true,
element: LoginScreen.loginButton.element,
failureLog: "Login Screen login button does not exist.")
return self
}

... additional functions ...
import Foundation
import XCTest
enum LoginScreen: String {
case cancelButton = "Cancel"
case hideButton = "Hide"
case loginButton = "log_in_button"
case loginLabel = "Username"
case passcodeErrorAlert = "UIAlert"
case passwordTextField = "password"
case showButton = "Show"
case usernameTextField = "username"
var element: XCUIElement {
switch self {
case .loginButton, .showButton, .hideButton, .cancelButton:
return XCUIApplication().buttons[self.rawValue]
case .usernameTextField:
return XCUIApplication().textFields[self.rawValue]
case .passwordTextField:
return XCUIApplication().secureTextFields[self.rawValue]
case .loginLabel:
return XCUIApplication().staticTexts[self.rawValue]
case .passcodeErrorAlert:
return XCUIApplication().navigationBars[self.rawValue]
}
}
}
// MARK: - Element Functionsfunc medCell(medCell: Int) -> XCUIElement {
return MedicationsScreen.medsListTable.element.cells.element(boundBy: medCell)
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store