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

    override func 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.

    }

    

    override func tearDown() {

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

        super.tearDown()

    }

    
    override func 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 {

        return arc4random_uniform(upperBound)

    }

    

    func testMonkey() {
        let app = XCUIApplication()
        for _ in 0 ..< 1000000 {

            // Login orPaypal
            if app.secureTextFields.count == 1 {
                var isLoginV = false
                for ind in 0 ..< 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.value as? 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 _ in 0 ..< 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 {

        return CGFloat(arc4random_uniform(UInt32(upperBound)))

    }

    

    func testMonkeyWithCoordinate() {

        let app = XCUIApplication()
      
        for _ in 0 ..< 1000000 {
            app.coordinate(withNormalizedOffset: CGVector(dx: Double(fRandom(10))/10, dy: Double(fRandom(10))/10)).tap()

            testCnt += 1

        }
    }
    
}

 

6、运行

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

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值