iOS OC SwiftMonkey 自动化测试 通过Pods集成

2,132 阅读2分钟

前言 SwiftMonkey 是一个不错的自动测试工具。但是网上的OC集成SwiftMonkey都太麻烦。他们都是用拖拽文件的方式集成的。这次我用的是CocoaPods的方式集成,整个过程很简单。



0 Xcode,MAC,CocoaPods环境。如果有人真没有CocoaPods环境,去配置一下吧。

先说一下target。我们创建一个项目,这个项目是project,创建项目的时候默认会创建一个同名的target。一个项目下可以有多个target。可以运用这个特性来配置一个项目的不同使用环境。

1 首先要有一个应用。。。(当然了)。

我创建了一个叫 SwiftMonkeyTest 的应用。

2 要保证应用中有一个swift的UITests target。

2.1 如果没有就创建一个。

2.2 如果是OC的删了创建一个。



3 配置Podfile,然后 Pod install。



4 选择SwiftMonkey两个包的swift版本



5 在你新建的target的xxTestUITests替换如下代码。

import XCTest
import SwiftMonkey
class SwiftMonkeyExampleUITests: XCTestCase {
   
     override func setUp() {
        super.setUp()
        XCUIApplication().launch()
     }
        
     override func tearDown() {
         super.tearDown()
     }
    
     func testMonkey() {
         let application = XCUIApplication()
            
            // Workaround for bug in Xcode 7.3. Snapshots are not properly updated
            // when you initially call app.frame, resulting in a zero-sized rect.
            // Doing a random query seems to update everything properly.
            // TODO: Remove this when the Xcode bug is fixed!
            _ = application.descendants(matching: .any).element(boundBy: 0).frame
            
            // Initialise the monkey tester with the current device
            // frame. Giving an explicit seed will make it generate
            // the same sequence of events on each run, and leaving it
            // out will generate a new sequence on each run.
            let monkey = Monkey(frame: application.frame)
            //let monkey = Monkey(seed: 123, frame: application.frame)
            
            // Add actions for the monkey to perform. We just use a
            // default set of actions for this, which is usually enough.
            // Use either one of these but maybe not both.
            // XCTest private actions seem to work better at the moment.
            // UIAutomation actions seem to work only on the simulator.
         monkey.addDefaultXCTestPrivateActions()
            //monkey.addDefaultUIAutomationActions()
            
            // Occasionally, use the regular XCTest functionality
            // to check if an alert is shown, and click a random
            // button on it.
         monkey.addXCTestTapAlertAction(interval: 100, application: application)
            
            // Run the monkey test indefinitely.
             monkey.monkeyAround()
     }
}



6 在Appdelegate中添加如下代码

#import <SwiftMonkeyPaws/SwiftMonkeyPaws-Swift.h>
@property (nonatomic, strong)MonkeyPaws * paws;
#ifdef DEBUG
    self.paws = [[MonkeyPaws alloc]initWithView:self.window tapUIApplication:YES];
#endif



7 运行下看看,如果没问题的话,就可以进行Test操作了。



8 问题解决

8.1 如果报ld: library not found for -l错误了。

那么说明你这个项目至少有一定的完成度了,哈哈哈。解决办法如下:找到Other Linker Flags 添加$(inherited)。如果还不行。把其他的都删了。

8.2 如果找不到头文件了

用#import <xxxxx/xxxxx.h>的形式应用pod文件



ps 如果有问题,欢迎讨论。