EarlGrey iOS自动化UI测试(集成篇)

目前针对iOS开发,很少会有UI方面的自动化测试.

目前Xcode提供了UI Automation,UI Testing Bundle,两者的优势都是可以通过录制真实的模拟器或者真机操作自动生成测试代码.而且不需要额外环境,只是需要xcode即可.但是缺点也很明显,就是自动生成的code可执行性,和可维护性很差.页面上的细微改动,都会导致case无法正常work.

EaryGrey是google提供的一套ios ui测试框架,基于Unit Testing Bundle,可以通过matcher和condition,matcher确保能够拿到页面元素,condition通过条件判断确保case执行成功.

EaryGrey集成有两种方式,一种是通过cocoapods,一种是github下载源码自己编译framework添加依赖

源码地址:https://github.com/google/EarlGrey

 

EaryGrey集成:

1.在已有需要测试的项目中新建测试target,选择"iOS Unit Testing Bundle".

2.新建Target导入EarlGrey.framework(本文使用自己编译的framework)

3.新建Target,Build Phases>Add New Copy File Phases(参考图片)

4.新建Scheme,Product>Scheme>New Scheme,target选择新建的test

5.编辑Scheme,Product>Scheme>Manager Schemes,刚才新建的scheme选择Shared

6.编辑原有Target Scheme

   Key: `DYLD_INSERT_LIBRARIES`
   Value:`@executable_path/EarlGrey.framework/EarlGrey`

7.测试target新建testcase

- (void)testExample {
 [[EarlGrey selectElementWithMatcher:grey_keyWindow()]
     assertWithMatcher:grey_sufficientlyVisible()];
}

8.Command+U执行测试

 

如何自动测试?

xcodebuild test -scheme xxx -project xxx.xcodeproj -configuration Debug -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6,OS=9.3' > test_ios.log | tail -f test.log

xcodebuild终端命令执行unit test.这样就可以定时执行,并且统计输出日志.

 

EarlGrey是一套较为完整的UI自动测试解决方案,以前UI测试过于依赖人工.这种自动化测试方式将会会大大提高效率.

posted @ 2016-08-07 10:30  shuffle  阅读(2458)  评论(0编辑  收藏  举报