刚刚给公司项目加上自动化测试,分享一下
成都创新互联自2013年创立以来,是专业互联网技术服务公司,拥有项目成都做网站、网站制作、成都外贸网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元明水做网站,已为上家服务,为明水各地企业和个人服务,联系电话:028-86922220
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 indin0 ..< 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()
iflet email = emailTextField.valueas?String {
if email !="UPIS1" {
let deleteKey = app.keys["delete"]
for_in0 ..< 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 {
switchfRandom(9) {
case1,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
}
}
case4:
if app.scrollViews.count >0 {
let scrollView = app.scrollViews.element(boundBy:UInt(fRandom(UInt32(app.scrollViews.count))))
if scrollView.exists&& scrollView.isHittable {
switchfRandom(4) {
case0:
scrollView.swipeDown()
case1:
scrollView.swipeUp()
case2:
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)))
http://cdkjz.cn/article/igcsos.html