最近在做真机UITest,趟了几个坑。这儿简单总结一下。
首先,你要参考这篇文章,让你的代码可以UITest。
http://www.jianshu.com/p/f4ba532caed0
注意当你在已有项目里添加uitest target的时候,Podfile要记得添加target,并更新。
今天的主角是!!!
t = 18.54s Wait for app to idle
进入我项目里的某个页面的时候,原先指定的点击事件不走了,并且停在打印完Wait for app to idle后就不走了。
一网页里看到。
Perhaps you have some animation or some other background (or foreground) activity that updates your UI on the main thread frequently. This causes the app to never be "quiesce" - at least on this tab. In our application we had UIView animation with option Repeat. CPU usage was fine and it wasn't a battery drain, but it made the test fail every time. Disabling the animation fixed the issue. I couldn't find a way to force the test not to wait to be idle, so we ended up disabling the animation using #ifdef for the UI test target using runtime arguments as described here:
对照我的项目.知道原因了
UITest 只会在 app 为 idle 的时候去走点击事件,但如果当前页面有其他动画(占据主线程),会导致 app 无法quiesce。点击事件就不会走,程序就一直在那里 Wait for app to idle。
然后找了各种方法,都不能强制的去终止Wait for app to idle,比如Wait for app to idle持续5秒后强制走点击事件。无奈只能让动画停止,不去占据主线程了。如果一个 app 里只有一个动画,那还算好办,如果是多个那岂不是非常麻烦。
其实在app开启的时候使所有的Animation失效就行了。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[UIView setAnimationsEnabled:NO]; //HERE
return YES;
}
问题算是解决了,但每次真实运行 app 都去注释掉这行,不是很麻烦?
其实可以在程序启动前添加几个参数。这样,只有遇到testUI的时候,动画才会暂停。正常 app 运行的时候,动画并不会暂停。
- (void)testUI{
XCUIApplication *app = [[XCUIApplication alloc]init];
app.launchArguments = @[@"ResetDefaults", @"NoAnimations", @"UserHasRegistered"];
[app launch];//程序启动
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSArray *arrayAPP = [[NSProcessInfo processInfo] arguments];
//默认arguments就一个。当大于1,就说明该程序是通过testUI启动的。
if (arrayAPP.count > 1) {
[UIView setAnimationsEnabled:NO];
}
return YES;
}
传送门:
http://stackoverflow.com/questions/33624650/xctestcase-wait-for-app-to-idle
http://stackoverflow.com/questions/32667201/accessing-the-host-app-code-from-the-xcode-7-ui-test-target/33466038#33466038
http://stackoverflow.com/questions/33624650/xctestcase-wait-for-app-to-idle