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


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