iOS自动化UI测试——KIF使用分享

Test Like a User!

  • 演示自动化UI测试样例

演示app,全流程一条线测试下来,注册--登录--实名认证--信用卡认证--添加收款器--刷卡收款

  • 介绍KIF

KIF的全称是Keep it functional。它是一个建立在XCTest的UI测试框架,通过accessibility来定位具体的控件,来操作UI。

  • 导入KIF

target 'XXXUnitTests' do

#    3.5.2

     pod 'KIF', :configurations => ['Debug']

end

  • 写测试用例和讲解常用方法

给UI组件的accessibilityLabel属性赋值,例如:self.nameTextF.accessibilityLabel = @"nameTextF";

新建单元测试类,写测试方法(以test开头)//- (void)testZhuCe

[tester tapViewWithAccessibilityLabel:@"liJiZhuCeBtn"];

[tester enterText:@"19902030220" intoViewWithAccessibilityLabel:@"phoneNumField"];

[tester clearTextFromAndThenEnterText:@"邓超界" intoViewWithAccessibilityLabel:@"jieSuanZhangHuText"];

[tester waitForTimeInterval:15];// 手动输入验证码

[tester waitForViewWithAccessibilityLabel:@"dengLuVC"];

if ([tester tryFindingTappableViewWithAccessibilityLabel:@"jumpButton" error:nil]) {

    [tester tapViewWithAccessibilityLabel:@"jumpButton"];

}

[tester enterText:@"0119" intoViewWithAccessibilityLabel:@"carddateField" traits:UIAccessibilityTraitNone expectedResult:@"01/19"];

 

  • Tips

按钮的title、类的title,可以直接做为访问标签;

如果UI组件被键盘挡住了,需要先退掉键盘;如果UI组件不在屏幕范围内,不可以访问,但是滚动视图,可以访问,且会出现在可视范围。

无法访问系统自己的弹窗。例如app想定位用户,不能自动点击允许;但是app自己的弹窗,可以操作的;

类内部的多个测试方法的测试顺序,是无序的;类与类的测试顺序,是无序的;可以将某个测试类或者测试方法给disable掉;

  • 遇到的坑

pod 'KIF', :configurations => ['Debug']
pod 'KIF/IdentifierTests'#此处错误;

造成Target Support Files和Headers文件夹内文件丢失;

IdentifierTests不导入也可以正常使用;

  • 参考网址

http://www.cocoachina.com/ios/20170401/18995.html 

http://www.oschina.net/translate/ios-ui-testing-with-kif

原文地址:https://www.cnblogs.com/dengchaojie/p/6814395.html