swift 自动化测试

刚刚给公司项目加上自动化测试,分享一下

1、在原来项目的基础上,创建自动化测试 TARGETS 




2、修改名称为:项目名-dev(分两步)

第一步:直接双击target,直接修改



第二步:下拉模拟器列表,选中manager schemes,在弹出的窗体直接双击修改



3、修改环境变量,添加 DEV 标签

选中DEV target, Build Settings --> Other Swift Flags --> 添加 -D DEV 两行




4、配置对应的info.plist文件,选中targets,右边选择info文件





5、到这里为止,测试target已经创建完毕,剩下的要写代码了

找到工程文件夹 (工程名UITests)

创建一个swift 文件 (工程名_MonkeyUITest )


上代码:(代码里面的“Sign in” 是登录按钮的title,“UserName” 是登录用户名的输入框的水印文字,“Password” 是密码输入框的水印文字,对应改成你们自己工程的就行)


//

//  sst_MonkeyUITest.swift

//  sst-ios

//

//  Copyright 2016 SST. All rights reserved.

//


import XCTest


class sst_MonkeyUITest:XCTestCase {

    

    var testCnt =0

    

    overridefunc setUp() {

        super.setUp()

        

        // Put setup code here. This method is called before the invocation of each test method in the class.

        

        // In UI tests it is usually best to stop immediately when a failure occurs.

        continueAfterFailure =true

        // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.

        XCUIApplication().launch()


        // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.

    }

    

    overridefunc tearDown() {

        // Put teardown code here. This method is called after the invocation of each test method in the class.

        super.tearDown()

    }

    

    overridefunc recordFailure(withDescription description:String, inFile filePath: String, atLine lineNumber:UInt, expected: Bool) {

        print("\(description)\(self.testCnt)")

    }

    

    func testExample() {

        // Use recording to get started writing UI tests.

        // Use XCTAssert and related functions to verify your tests produce the correct results.

    }

    

    func fRandom(_ upperBound:UInt32) -> UInt32 {

        returnarc4random_uniform(upperBound)

    }

    

    func testMonkey() {

        let app =XCUIApplication()

        

        for_ in0 ..< 1000000 {

            

            // Login or Paypal

            if app.secureTextFields.count ==1 {

                var isLoginV = false

                for ind in0 ..< app.buttons.count {

                    if app.buttons.element(boundBy: ind).label== "Sign in" { // Login

                       isLoginV = true

                       break

                   }

                }

                if isLoginV {

                    let emailTextField = app.textFields["UserName"]

                   emailTextField.tap()

                    if let email = emailTextField.valueas? String {

                       if email != "UPIS1" {

                           let deleteKey = app.keys["delete"]

                           for _in 0 ..< email.characters.count {

                               deleteKey.tap()

                           }

                           emailTextField.typeText("UPIS1")

                       }

                   }

                   app.buttons["Next"].tap()

                    let passwordSecureTextField = app.secureTextFields["Password"]

                   passwordSecureTextField.tap()

                   passwordSecureTextField.typeText("888888")

                   app.buttons["Done"].tap()

                } else {

                    // Paypal

                }

            }

            

            var isValidAction =false

            for_ in0 ..< 999 {

                switch fRandom(9) {

                case 1,2,3:

                    if app.cells.count >0 {

                       let ind = UInt(fRandom(UInt32(app.buttons.count)))

                       let cell = app.cells.element(boundBy: ind)

                       if cell.exists&& ind < app.cells.count {

                           cell.coordinate(withNormalizedOffset:CGVector(dx: 0.5, dy:0.5)).tap()

                           isValidAction = true

                       }

                   }

                case 4:

                    if app.scrollViews.count >0 {

                       let scrollView = app.scrollViews.element(boundBy:UInt(fRandom(UInt32(app.scrollViews.count))))

                       if scrollView.exists&& scrollView.isHittable {

                           switch fRandom(4) {

                           case 0:

                               scrollView.swipeDown()

                           case 1:

                               scrollView.swipeUp()

                           case 2:

                               scrollView.swipeLeft()

                           default:

                               scrollView.swipeRight()

                           }

                           isValidAction = true

                       }

                   }

                default:

                    if app.buttons.count >0 {

                       let ind = UInt(fRandom(UInt32(app.buttons.count)))

                       let button = app.buttons.element(boundBy: ind)

                       if button.isHittable&& ind < app.buttons.count {

                           button.coordinate(withNormalizedOffset:CGVector(dx: 0.5, dy:0.5)).tap()

                           isValidAction = true

                       }

                   }

                }

                if isValidAction {

                    break

                }

            }

            

            testCnt +=1

        }

        

        print("Complete Monkey Test.\(testCnt)")

    }

    

    func randomCGFloat(_ upperBound:CGFloat) -> CGFloat {

        returnCGFloat(arc4random_uniform(UInt32(upperBound)))

    }

    

    func testMonkeyWithCoordinate() {

        let app =XCUIApplication()

        

        for_ in0 ..< 1000000 {

            app.coordinate(withNormalizedOffset:CGVector(dx: Double(fRandom(10))/10, dy: Double(fRandom(10))/10)).tap()

            

            testCnt +=1

        }

    }

    

}



6、运行

点击testMonkey左边的绿色箭头运行测试用例,然后你的项目就会自己跑起来了,并且自己点击,自己操作,自动化测试完毕,不懂的可以问我 qq:751776425


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章