UITests操作指南

2017-12-06  本文已影响0人  灰s

UI Tests有什么用?

它可以通过编写代码、或者是记录开发者的操作过程并代码化,来实现自动点击某个按钮、视图,或者自动输入文字等功能。

添加UI Tests

image.png image.png

添加成功

默认的UITests方法都是以test开头的。这里的testExample是系统自动生成的方法。

image.png

添加测试代码

有两种方式

新手不推荐自己写,很麻烦,坑也很多。我也是用的自动添加,然后再自己调整。

image.png
这里有个操作技巧。就是开始录制了以后(也就是app启动了以后),可以再次点击这个按钮来暂停,某些不需要记录的操作完成以后,可以再次点击启动。这样在同一个逻辑需要多次调整的时候,就可以避免一些重复代码了。

再次运行写好的Test

image.png

自动添加的代码,你再次运行这个测试用例的时候,不一定能正常运行,会有一些bug,需要再次调整。

注意事项:

 Return YES if the receiver should be exposed as an accessibility element. 
 default == NO
 default on UIKit controls == YES 
 Setting the property to YES will cause the receiver to be visible to assistive applications. 
tablesQuery.cells.containing(.staticText, identifier:"澳洲直邮Devondale德运奶粉高钙全脂奶粉成人奶粉1kg ")

下面是两个我写好的测试代码

1. 登录的模拟测试,这个要复杂一些,涉及了界面跳转和一些重复点击。
2. 购物车的模拟测试,这个主要是给大家看看tableView这类集合视图的操作。

这是我项目的tabbar,两段测试代码最开头都有app.tabBars.buttons["我的"].tap()这种代码,就是tabbar的切换。代码的前面会附上购物车和登录界面的设计图。

image.png image.png
image.png
    /**
     这里分三步:
     1. 国家地区为中国时,判断手机号是否正确
     2. 国家地区不是中国时,判断手机号是否正确
     3. 正确的账号密码,登录功能
     */
    func testLogin() {
        let app = XCUIApplication()
        let window = app.windows.element(boundBy: 0)
        app.tabBars.buttons["我的"].tap()
        window.press(forDuration: 1)
        //---------------------1
        //找到手机号的输入框
        let textField = app.textFields["请输入手机号"]
        XCTAssertEqual(textField.exists, true)
        //点击
        textField.tap()
        //修改输入框的输入
        textField.typeText("187027813111")
        //获取清空手机号的按钮
        let clearTextButton = app.textFields["请输入手机号"].buttons["Clear text"]
        //收键盘
        app.buttons["确定"].tap()
        //找到登录按钮
        let button = app.buttons.element(matching: .button, identifier: T_LoginBtn)
        //点击登录
        button.tap()
        //暂停2秒
        window.press(forDuration: 2)
        //---------------------2
        //跳到选择国家界面
        let selectCountry = app.buttons["login arrow"]
        selectCountry.tap()
        let tablesQuery = app.tables
        //选中一个国家并返回
        tablesQuery.staticTexts["爱沙尼亚"].tap()
        //点击登录
        button.tap()
        //暂停2秒
        window.press(forDuration: 2)
        textField.tap()
        //清空账号信息
        clearTextButton.tap()
        textField.typeText("18702781315")
        //收键盘
        app.buttons["确定"].tap()
        //再次选中国家
        selectCountry.tap()
        tablesQuery.staticTexts["中国"].tap()
        //点击登录
        button.tap()
        //暂停2秒
        window.press(forDuration: 2)
        //---------------------3
        let pwdTextField = app.secureTextFields.element(matching: .secureTextField, identifier: T_PWDLoginKey)
        pwdTextField.tap()
        pwdTextField.typeText("123456")
        //输入完成点击确认按钮
        let doneButton = app.buttons["Done"]
        doneButton.tap()
        
        button.tap()
    }
image.png
    /**
     购物车的测试模型
     */
    func testShopCart() {
        let app = XCUIApplication()
        app.tabBars.buttons["tab shopcart"].tap()
        //获取tableView的所有cell
        let tablesCells = app.tables.cells
        //遍历cell
        (0..<tablesCells.count).forEach { (index) in
            let cell = tablesCells.element(boundBy: index)
            cell.buttons["shopCart add"].tap()
            cell.buttons["shopCart add"].tap()
            cell.buttons["shopCart add"].tap()
            cell.buttons["shopCart reduce"].tap()
            cell.buttons["shopCart reduce"].tap()
            cell.buttons["shopcart unselect"].tap()
            cell.buttons["shopcart unselect"].tap()
        }
    }

参考链接:
http://www.cocoachina.com/ios/20150925/13566.html

上一篇 下一篇

猜你喜欢

热点阅读