iOS开发之交互测试/UI Test

UI Tests是一个自动测试UI与交互的Testing组件,Apple在Xcode7中引入了一种新的方式来在你的应用中进行UI测试.UI testing允许你找到UI元素并与之交互,还能检查属性和状态.而且已经完全集成到了Xcode 7测试报告,可以和单元测试一起执行.还有一个优点就是相比于Automation的自动化测试,UITest使用的OC/Swift语言的自动化测试,而前者使用的是JS代码.

作用:

·可以通过编写代码、或者是记录开发者的操作过程并代码化,来实现自动点击某个按钮、视图,或者自动输入文字等功能.

·在实际的开发过程中,随着项目越做越大,功能越来越多,仅仅靠人工操作的方式来覆盖所有测试用例是非常困难的,尤其是加入新功能以后,旧的功能也要重新测试一遍,这导致了测试需要花非常多的时间来进行回归测试,这里产生了大量重复的工作,而这些重复的工作有些是可以自动完成的,UI Tests就可以帮助解决这个问题了.

添加UI Testing的过程在这里不再赘述,和添加单元测试一样,也同样可以在后来追加创建.

我们可以先点击录制按钮,看看生成的测试代码

iOS开发之交互测试/UI Test_第1张图片

点击按钮之后,在模拟器上的操作会被记录,同时生成测试代码,下面是界面和对应生成的测试代码

iOS开发之交互测试/UI Test_第2张图片

    XCUIApplication *app = [[XCUIApplication alloc] init];
    XCUIElementQuery *steppersQuery = app.steppers;
    XCUIElement *incrementButton = steppersQuery.buttons[@"Increment"];
    [incrementButton tap];

    XCUIElement *decrementButton = steppersQuery.buttons[@"Decrement"];
    [decrementButton tap];
    [app.buttons[@"Second"] tap];
    [app.buttons[@"Button"] tap];
    [app.sliders[@"50%"] tap];
    [app.staticTexts[@"Label"] tap];

    XCUIElement *slider = app.sliders[@"100%"];
    [slider tap];
    [slider tap];
    [incrementButton tap];
    [incrementButton tap];
    [incrementButton tap];
    [app.buttons[@"First"] tap];
    [incrementButton tap];
    [decrementButton tap];

当然也可以先写出测试代码,然后让程序运行

测试代码语法:

XCUIApplication: 继承XCUIElement,这个类掌管应用程序的生命周期,里面包含两个主要方法
launch(): 启动程序
terminate(): 终止程序

XCUIElement: 继承NSObject,实现协议XCUIElementAttributes XCUIElementTypeQueryProvider 可以表示系统的各种UI元素
exist: 可以让你判断当前的UI元素是否存在,如果对一个不存在的元素进行操作,会导致测试组件抛出异常并中断测试
descendantsMatchingType(type:XCUIElementType)->XCUIElementQuery: 取某种类型的元素以及它的子类集合
childrenMatchingType(type:XCUIElementType)->XCUIElementQuery: 取某种类型的元素集合,不包含它的子类

这两个方法的区别在于,你仅使用系统的UIButton时,用childrenMatchingType就可以了,如果你还希望查询自己定义的子Button,就要用descendantsMatchingType

另外UI元素的一些交互
tap(): 点击
doubleTap(): 双击
pressForDuration(duration: NSTimeInterval): 长按一段时间,在你需要进行延时操作时,这个就派上用场了
swipeUp(): 这个响应不了pan手势,暂时没发现能用在什么地方,也可能是beta版的bug,先不解释
typeText(text: String): 用于textField和textView输入文本时使用,使用前要确保文本框获得输入焦点,可以使用tap()函数使其获得焦点

XCUIElementAttributes协议
里面包含了UIAccessibility中的部分属性
@property (readonly) NSString *identifier;
@property (readonly) CGRect frame;
@property (readonly, nullable) id value;
@property (readonly, copy) NSString *title;
@property (readonly, copy) NSString *label;
@property (readonly) XCUIElementType elementType;
@property (readonly, getter = isEnabled) BOOL enabled;
@property (readonly) XCUIUserInterfaceSizeClass horizontalSizeClass;
@property (readonly) XCUIUserInterfaceSizeClass verticalSizeClass;
@property (readonly, nullable) NSString *placeholderValue;
@property (readonly, getter = isSelected) BOOL selected;
@property (readonly) BOOL hasFocus;

XCUIElementTypeQueryProvider协议
里面包含了系统中大部分UI控件的类型,可通过读属性的方式取得某种类型的UI集合
@property (readonly, copy) XCUIElementQuery *groups;
@property (readonly, copy) XCUIElementQuery *windows;
@property (readonly, copy) XCUIElementQuery *sheets;
@property (readonly, copy) XCUIElementQuery *drawers;
@property (readonly, copy) XCUIElementQuery *alerts;
@property (readonly, copy) XCUIElementQuery *dialogs;
@property (readonly, copy) XCUIElementQuery *buttons;
@property (readonly, copy) XCUIElementQuery *radioButtons;
@property (readonly, copy) XCUIElementQuery *radioGroups;
@property (readonly, copy) XCUIElementQuery *checkBoxes;
@property (readonly, copy) XCUIElementQuery *disclosureTriangles;
@property (readonly, copy) XCUIElementQuery *popUpButtons;
@property (readonly, copy) XCUIElementQuery *comboBoxes;
@property (readonly, copy) XCUIElementQuery *menuButtons;
@property (readonly, copy) XCUIElementQuery *toolbarButtons;
@property (readonly, copy) XCUIElementQuery *popovers;
@property (readonly, copy) XCUIElementQuery *keyboards;
@property (readonly, copy) XCUIElementQuery *keys;
@property (readonly, copy) XCUIElementQuery *navigationBars;
@property (readonly, copy) XCUIElementQuery *tabBars;
@property (readonly, copy) XCUIElementQuery *tabGroups;
@property (readonly, copy) XCUIElementQuery *toolbars;
@property (readonly, copy) XCUIElementQuery *statusBars;
@property (readonly, copy) XCUIElementQuery *tables;
@property (readonly, copy) XCUIElementQuery *tableRows;
@property (readonly, copy) XCUIElementQuery *tableColumns;
@property (readonly, copy) XCUIElementQuery *outlines;
@property (readonly, copy) XCUIElementQuery *outlineRows;
@property (readonly, copy) XCUIElementQuery *browsers;
@property (readonly, copy) XCUIElementQuery *collectionViews;
@property (readonly, copy) XCUIElementQuery *sliders;
@property (readonly, copy) XCUIElementQuery *pageIndicators;
@property (readonly, copy) XCUIElementQuery *progressIndicators;
@property (readonly, copy) XCUIElementQuery *activityIndicators;
@property (readonly, copy) XCUIElementQuery *segmentedControls;
@property (readonly, copy) XCUIElementQuery *pickers;
@property (readonly, copy) XCUIElementQuery *pickerWheels;
@property (readonly, copy) XCUIElementQuery *switches;
@property (readonly, copy) XCUIElementQuery *toggles;
@property (readonly, copy) XCUIElementQuery *links;
@property (readonly, copy) XCUIElementQuery *images;
@property (readonly, copy) XCUIElementQuery *icons;
@property (readonly, copy) XCUIElementQuery *searchFields;
@property (readonly, copy) XCUIElementQuery *scrollViews;
@property (readonly, copy) XCUIElementQuery *scrollBars;
@property (readonly, copy) XCUIElementQuery *staticTexts;
@property (readonly, copy) XCUIElementQuery *textFields;
@property (readonly, copy) XCUIElementQuery *secureTextFields;
@property (readonly, copy) XCUIElementQuery *datePickers;
@property (readonly, copy) XCUIElementQuery *textViews;
@property (readonly, copy) XCUIElementQuery *menus;
@property (readonly, copy) XCUIElementQuery *menuItems;
@property (readonly, copy) XCUIElementQuery *menuBars;
@property (readonly, copy) XCUIElementQuery *menuBarItems;
@property (readonly, copy) XCUIElementQuery *maps;
@property (readonly, copy) XCUIElementQuery *webViews;
@property (readonly, copy) XCUIElementQuery *steppers;
@property (readonly, copy) XCUIElementQuery *incrementArrows;
@property (readonly, copy) XCUIElementQuery *decrementArrows;
@property (readonly, copy) XCUIElementQuery *tabs;
@property (readonly, copy) XCUIElementQuery *timelines;
@property (readonly, copy) XCUIElementQuery *ratingIndicators;
@property (readonly, copy) XCUIElementQuery *valueIndicators;
@property (readonly, copy) XCUIElementQuery *splitGroups;
@property (readonly, copy) XCUIElementQuery *splitters;
@property (readonly, copy) XCUIElementQuery *relevanceIndicators;
@property (readonly, copy) XCUIElementQuery *colorWells;
@property (readonly, copy) XCUIElementQuery *helpTags;
@property (readonly, copy) XCUIElementQuery *mattes;
@property (readonly, copy) XCUIElementQuery *dockItems;
@property (readonly, copy) XCUIElementQuery *rulers;
@property (readonly, copy) XCUIElementQuery *rulerMarkers;
@property (readonly, copy) XCUIElementQuery *grids;
@property (readonly, copy) XCUIElementQuery *levelIndicators;
@property (readonly, copy) XCUIElementQuery *cells;
@property (readonly, copy) XCUIElementQuery *layoutAreas;
@property (readonly, copy) XCUIElementQuery *layoutItems;
@property (readonly, copy) XCUIElementQuery *handles;
@property (readonly, copy) XCUIElementQuery *otherElements;

你可能感兴趣的:(iOS)