下面以Swift项目为例,为了节省时间,这里直接storybored拖界面,可以自行用代码:
最终完成功能是:两个界面,分别记录按钮的点击事件,并进行测试

记录详细流程如下:
1.创建项目,语言Swift ,选中include UI Tests,点击next创建:

2.选中ViewController快速创建导航,并添加两个按钮,修改导航title未mainView:


3.创建subView控制器,把pushToNextPage按钮设置点击事件push新的页面subView,并在subView界面也添加两个按钮:

4.添加按钮事件
(1)在mainView控制器,doSomething按钮事件:
//点击按钮事件
@IBAction func clickBtn(sender: UIButton) {
//mainView控制器中进行一些逻辑操作或者其他anything ...
//打印日志
print("mainView控制器中进行一些逻辑操作或者其他anything")
}
(2)在创建SubViewController.swift 的里:
import Foundation
import UIKit
class SubViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
//点击clickMe1按钮事件1
@IBAction func clickBtn1(sender: UIButton) {
//打印日志
print("SubView控制器中进行一些逻辑操作或者其他anything")
}
//点击clickMe2按钮事件2
@IBAction func clickBtn2(sender: UIButton) {
//打印日志
print("SubView控制器中进行一些逻辑操作或者其他anything")
}
}
5.运行项目,确定没什么问题.
6.添加UITests代码方法

-
将光标定位在testMainViewClickBtn方法中,点击底部红点[有时候红点不能点击,将xcode关掉再打开一般就好了]开始录制UTTest代码:
开始录制UTTest代码
8.经过步骤7项目会启动,此时点击mainView里的所有事件,测试代码会自动录制,录制完后再点击红点结束录制:
//MainView测试方法
func testMainViewClickBtn() {
let app = XCUIApplication()
app.buttons["pushToNextPage"].tap()
app.navigationBars["subView"].buttons["mainView"].tap()
app.buttons["doSomething"].tap()
app.otherElements.containing(.navigationBar, identifier:"mainView").children(matching: .other).element.children(matching: .other).element.children(matching: .other).element.tap()
}
9.subView进行同样操作,至此简单UITest自动录制结束.开始运行测试,提示success标示已成功:

网友评论