Utilities support write UI Testing for Swift
Supporting local server by Embassy and Ambassador
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let arguments = ProcessInfo.processInfo.arguments
for argument in arguments {
switch argument {
case "UI_TESTING":
ServicesAPI.shared.configureBaseURL("http://localhost:8080")
default:
ServicesAPI.shared.configureBaseURL("https://da31de14-b60e-4249-8921-0dcf5d5796ef.mock.pstmn.io")
}
}
return true
}
router["/api/v2/users"] = DelayResponse(JSONResponse(handler: { _ -> Any in
return [
["id": "01", "name": "john"],
["id": "02", "name": "tom"]
]
}))
JSONString response support
router["/api/v2/users"] = DelayResponse(JSONStringResponse(handler: { _ -> Any in
return """
{
"acess_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
}
"""
}))
Page Object Model Design pattern
func testInvalidPassword() {
_ = loginPage
.enterUsername("nvduc2910")
.enterPassword("")
.tapLoginButton()
.then { _ in
XCTAssert(loginPage.invalidPasswordAlert.exists)
}
}
AccessId support
extension LoginController: UITestablePage {
typealias UIElementType = UIElements.LoginUIElements
func makeViewTestable() {
self.makeViewTestable(self.usernameTextField, using: .userNameTextField)
self.makeViewTestable(self.passwordTextField, using: .passwordTextField)
self.makeViewTestable(self.loginButton, using: .loginButton)
}
}
XCTest Extensions
open func expectToSee(_ element: XCUIElement,
within timeout: TimeInterval = 5,
_ file: StaticString = #file,
_ line: UInt = #line) {
if element.exists {
XCTAssertTrue(true)
} else {
XCTAssertTrue(element.waitForExistence(timeout: timeout), file: file, line: line)
}
}
open func expectNotToSee(_ element: XCUIElement,
within timeout: TimeInterval = 5,
_ file: StaticString = #file,
_ line: UInt = #line) {
let date = Date().addingTimeInterval(timeout)
var result = false
repeat {
result = !element.exists
} while !result && Date().timeIntervalSince1970 < date.timeIntervalSince1970
XCTAssertTrue(result, file: file, line: line)
}
open func enterText(_ text: String, into element: XCUIElement, _ file: StaticString = #file, _ line: UInt = #line) {
if !element.hasFocus() {
element.tap()
}
element.typeText(text)
guard let value = element.value as? String else { return XCTFail(file: file, line: line) }
XCTAssertEqual(value, text, file: file, line: line)
}
To run the example project, clone the repo, and run pod install
from the Example directory first.
- Mock real server and local HTTP Server
- Login flow test
- Book list test
UITestUtils is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'UITestUtils'
nvduc2910, [email protected]
UITestUtils is available under the MIT license. See the LICENSE file for more info.