UITestUtils
工具支持为 Swift 编写 UI 测试
本地 HTTP 服务器
通过 Embassy 和 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("https://: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 响应
router["/api/v2/users"] = DelayResponse(JSONStringResponse(handler: { _ -> Any in
return """
{
"acess_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
}
"""
}))
UITest Utils
页面对象模型设计模式
func testInvalidPassword() {
_ = loginPage
.enterUsername("nvduc2910")
.enterPassword("")
.tapLoginButton()
.then { _ in
XCTAssert(loginPage.invalidPasswordAlert.exists)
}
}
支持访问 ID
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 扩展
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)
}
示例
要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install
命令。
- 模拟真实服务器和本地 HTTP 服务器
- 登录流程测试
- 书籍列表测试
要求
安装
UITestUtils 通过 CocoaPods 提供。要安装它,只需在 Podfile 中添加以下行:
pod 'UITestUtils'
作者
nvduc2910, [email protected]
许可
UITestUtils 使用 MIT 许可。有关更多信息,请参阅 LICENSE 文件。