RxSwift&Moya&ObjectMapper&Unit UI test
使用RxSwift&Moya&ObjectMapper 实现 登陆 验证 tableview 搜索 collectview 搜索 XCTest的 UITest尝试
登陆邮箱验证
class func isValidEmail(_ email: String) -> Bool {
let regular = try? NSRegularExpression(pattern: "^\\S+@\\S+\\.\\S+$", options: [])
if let re = regular {
let range = NSRange(location: 0, length: email.count)
let result = re.matches(in: email, options: [], range: range)
return result.count > 0
}
return false
}
对登陆进行邮箱 密码长度验证
let userObservable = usernameTextField.textField?.rx.text.map({ InputValidator.isValidEmail($0!) })
userObservable?.map({$0 ? "Email" : "输入有效邮箱地址"}).subscribe(onNext: { text in
self.usernameTextField.placeholder = text
}).disposed(by: bag)
let passObservable = passwordTextField.textField?.rx.text.map({ InputValidator.isValidPassword($0!) })
passObservable?.map({$0 ? "Password" : "密码不能少于8位"}).subscribe(onNext: { text in
self.passwordTextField.placeholder = text
}).disposed(by: bag)
Observable.combineLatest(userObservable!, passObservable!){(varildUser,varildPass) -> Bool in
return varildPass && varildUser
}.subscribe (onNext:{ (isEnable) in
self.loginBtn.button?.isEnabled = isEnable
}).disposed(by: bag)
网络请求之后进行数据筛选
let networkTool = MoyaProvider<NetworkTool>()
let apiHelper = NetworkHelper(moyaProvider: networkTool)
apiHelper.fetchBasicInfo().subscribe(onNext: { [unowned self] (data) in
if text.count > 0 {
let datas = data.list?.filter { (dataS) -> Bool in
//model是否包含搜索字符串
return (dataS.weatherDate!.contains(text))
}
//修改value值,重新发出事件
self.wyVariable.accept(datas!)
} else {
self.wyVariable.accept(data.list!)
}
}, onError: { (error) in
print("error",error)
}).disposed(by: bag)
//使用ObjectMapper 进行模型转换
struct ResponseBasicInfo: Mappable {
var dataStatus:DataStatus?
var list:[ListData]?
init(map: Map) {
dataStatus <- map["DataStatus"]
list <- map["Data"]
}
mutating func mapping(map: Map) {
dataStatus <- map["DataStatus"]
list <- map["Data"]//后台返回Data 和 本地有冲突的情况下 可以映射为其他 例如 list字段
}
}
XCTest UItest 前期写代码测试还是比较麻烦,后期是一步到位的测试 自动输入登陆邮箱密码 登陆 tableview 滑动 collectview 搜索框输入 测试搜索结果
let app = XCUIApplication()
app.launch()//启动应用 必须有的初始代码
let username = app.otherElements["Email"]//获取登陆的邮箱输入框
username.tap()
username.typeText("123456@qq.com")
let password = app.otherElements["Password"]//获取登陆的密码输入框
password.tap()
password.typeText("123123123")
app.buttons["SIGN IN"].tap()
sleep(5)
//获取的tableview控件
let table = app.children(matching: .window).element(boundBy: 0).children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.children(matching: .table).element
let tables = app.tables
table.swipeDown()//tableview滑动
table.swipeUp()