自动化测试/CI测试

KIF API中文翻译(一)

2017-08-11  本文已影响383人  龙猫六六

UIAccessibility是在UIKit里的一个非正式协议,提供关于UI元素的辅助功能。这个信息能够通过VoiceOver和其他辅助科技帮助肢体障碍者用户与你的用户进行交互。
UIAccessibility所有属性的列表:

//控件的识别符,默认值为nil
accessibilityLabel

//操作后的提示
//是对控件执行操作后结果的简单描述,而不识别控件和操作。
//例如,包含电子邮件消息的表行的提示可能是“选择消息”,但不是“点击此行以选择消息”。
accessibilityHint:

//控件具体值
//当辅助功能元素具有静态标签和动态值, accessibilityValue为动态值
//如textfield控件的标示属性为accessibilityLabel,对应输入框的文本值属性为accessibilityValue
accessibilityValue

//控件语言属性
accessibilityLanguage

//控件的特性
//控件的种类信息,使用了UIAccessibiltyTraits为值的位掩码,这个属性可以由文档里指定好几个属性结合而成。如自定义按钮显示一张图片,点击播放声音,则控件的特征为“按钮”,“图片”,“播放声音”。
accessibilityTraits

//控件的位置信息
//具体指的是在屏幕上的大小和位置信息
accessibilityFrame

//控件激活点
//默认为accessibilityFrame中心点,但也有列外如长按删除应用,控件激活点为图标左上角的圆圈叉
accessibilityActivationPoint

//控件隐藏
//使用此属性来隐藏控件。在这种情况下,隐藏的控件可能在屏幕上可见,但它们无法进行操作。
accessibilityElementsHidden

//控件的组属性
//默认值为no,若为yes VoiceOver将忽略该该控件,且包括该控件所在视图中的其他控件
accessibilityViewIsModal

控件获取

等待控件显示(一)
相对通用的方法,衍生出三种api等待目标控件出现,返回目标的UIView对象
定位目标对象参数:
label(accessibilityLabel):控件的辅助功能标示
traits(accessibilityTraits):控件的辅助功能特性
value(accessibilityValue):控件的文本值,如textfield文本输入满足一定条件才符合条件
返回参数:
UIView:控件对象指针

- (UIView *)waitForViewWithAccessibilityLabel:(NSString *)label;
- (UIView *)waitForViewWithAccessibilityLabel:(NSString *)label traits:(UIAccessibilityTraits)traits;
- (UIView *)waitForViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits;

等待控件显示(二)
与等待控件显示(一)基本一致,区别在于添加“可点击”的约束

- (UIView *)waitForTappableViewWithAccessibilityLabel:(NSString *)label;
- (UIView *)waitForTappableViewWithAccessibilityLabel:(NSString *)label traits:(UIAccessibilityTraits)traits;
- (UIView *)waitForTappableViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits;

等待控件显示(三)
通过传址,将符合条件的控件对象进行赋值。
参数:
赋值参数
element(UIAccessibilityElement **):传址,将符合条件的对象存储,类型为UIAccessibilityElement
view(out UIView **):传址,将符合条件的对象存储,类型为UIView
条件参数
label(accessibilityLabel):控件的辅助功能标示
fromView(UIView):遍历符合条件的起始视图
traits(UIAccessibilityTraits):控件的辅助功能特性
mustBeTappable(BOOL):是否可以点击

- (void)waitForAccessibilityElement:(UIAccessibilityElement **)element view:(out UIView **)view withLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits tappable:(BOOL)mustBeTappable;
- (void)waitForAccessibilityElement:(UIAccessibilityElement **)element view:(out UIView **)view withLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits fromRootView:(UIView *)fromView tappable:(BOOL)mustBeTappable;

等待控件显示(四)
利用逻辑约束(NSPredicate)等待符合条件的控件,与等待控件显示(三)类型,差别是使用逻辑约束寻找目标控件
参数:
赋值参数
element(UIAccessibilityElement **):传址,将符合条件的对象存储,类型为UIAccessibilityElement
view(out UIView **):传址,将符合条件的对象存储,类型为UIView
条件参数
predicate(NSPredicate *):逻辑约束
identifier(NSString *):逻辑约束NSString表达,函数内部会将其转为为NSPredicate对象
mustBeTappable(BOOL):是否可点击作为约束条件

- (void)waitForAccessibilityElement:(UIAccessibilityElement **)element view:(out UIView **)view withElementMatchingPredicate:(NSPredicate *)predicate tappable:(BOOL)mustBeTappable;
- (void)waitForAccessibilityElement:(UIAccessibilityElement **)element view:(out UIView **)view withIdentifier:(NSString *)identifier tappable:(BOOL)mustBeTappable;
- (void)waitForAccessibilityElement:(UIAccessibilityElement **)element view:(out UIView **)view withIdentifier:(NSString *)identifier fromRootView:(UIView *)fromView tappable:(BOOL)mustBeTappable;

等待控件消失

与等待控件显示API的传参一致,差别是等待控件消失

- (void)waitForAbsenceOfViewWithAccessibilityLabel:(NSString *)label;
- (void)waitForAbsenceOfViewWithAccessibilityLabel:(NSString *)label traits:(UIAccessibilityTraits)traits;
- (void)waitForAbsenceOfViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits;
- (void)waitForAbsenceOfViewWithElementMatchingPredicate:(NSPredicate *)predicate;

手势操作

点击

//方式一:
//通过accessibilityLabel寻找并点击目标控件
//steup1.等待符合条件的控件(accessibilityLabel标示,accessibilityTraits特性,accessibilityValue文本值)
//steup2.点击符合条件的控件
- (void)tapViewWithAccessibilityLabel:(NSString *)label;
- (void)tapViewWithAccessibilityLabel:(NSString *)label traits:(UIAccessibilityTraits)traits;
- (void)tapViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits;

//方式二:
//点击指定视图view中的UIAccessibilityElement对象
element(UIAccessibilityElement *):UIAccessibilityElement对象
view(UIView *):视图
- (void)tapAccessibilityElement:(UIAccessibilityElement *)element inView:(UIView *)view;

//方式三:点击屏幕某个坐标
- (void)tapScreenAtPoint:(CGPoint)screenPoint;

长按

//方式一:
//通过accessibilityLabel寻找并点击目标控件
//steup1.等待符合条件的控件(accessibilityLabel标示,accessibilityTraits特性,accessibilityValue文本值)
//steup2.长按符合条件的控件,长按时间为duration
- (void)longPressViewWithAccessibilityLabel:(NSString *)label duration:(NSTimeInterval)duration;
- (void)longPressViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value duration:(NSTimeInterval)duration;
- (void)longPressViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits duration:(NSTimeInterval)duration;

//方式二:
//长按指定视图view中的UIAccessibilityElement对象,长按时间为duration
- (void)longPressAccessibilityElement:(UIAccessibilityElement *)element inView:(UIView *)view duration:(NSTimeInterval)duration;

滑动

//方式一:
//根据accessibiltyLabel寻找符合条件的控件,并滑动
//horizontalFraction:水平滑动距离
//verticalFraction:垂直滑动距离
- (void)scrollViewWithAccessibilityLabel:(NSString *)label byFractionOfSizeHorizontal:(CGFloat)horizontalFraction vertical:(CGFloat)verticalFraction

//方式二:
//根据逻辑约束(identifier)寻找符合条件的控件,并滑动
//horizontalFraction:水平滑动距离
//verticalFraction:垂直滑动距离
- (void)scrollViewWithAccessibilityIdentifier:(NSString *)identifier byFractionOfSizeHorizontal:(CGFloat)horizontalFraction vertical:(CGFloat)verticalFraction

//方式三:
//滑动指定视图view中的UIAccessibilityElement对象
//horizontalFraction:水平滑动距离
//verticalFraction:垂直滑动距离
- (void)scrollAccessibilityElement:(UIAccessibilityElement *)element inView:(UIView *)viewToScroll byFractionOfSizeHorizontal:(CGFloat)horizontalFraction vertical:(CGFloat)verticalFraction;

轻扫

//方式一
//steup1.寻找符合条件的目标控件(accessibilityLabel标示,accessibilityTraits特性,accessibilityValue文本值)
//steup2.根据设定的方向值,轻扫目标控件
- (void)swipeViewWithAccessibilityLabel:(NSString *)label inDirection:(KIFSwipeDirection)direction;
- (void)swipeViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value inDirection:(KIFSwipeDirection)direction;
- (void)swipeViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits inDirection:(KIFSwipeDirection)direction;

//方式二
//轻扫指定视图view中的UIAccessibilityElement对象,方向有direction指定
- (void)swipeAccessibilityElement:(UIAccessibilityElement *)element inView:(UIView *)viewToSwipe inDirection:(KIFSwipeDirection)direction;

下拉刷新

//方式一
//steup1.寻找符合条件的目标控件(accessibilityLabel标示,accessibilityValue文本值)
//steup2.下拉目标控件,下拉持续时间由pullDownDuration设定
- (void)pullToRefreshViewWithAccessibilityLabel:(NSString *)label pullDownDuration:(KIFPullToRefreshTiming) pullDownDuration;
- (void)pullToRefreshViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value;

//方式二
//下拉刷新指定视图view中的UIAccessibilityElement对象,下拉持续时间由pullDownDuration设定
- (void)pullToRefreshAccessibilityElement:(UIAccessibilityElement *)element inView:(UIView *)viewToSwipe pullDownDuration:(KIFPullToRefreshTiming) pullDownDuration;

特殊控件操作

文本输入操作

//方式一
//对第一响应的控件输入文本,对于UIWebView/无辅助功能标示的控件较为有用
//对默认第一响应进行输入
//text=@“\b”表明删除一个字符,text=@“\b\b”表示删除两个字符,以此类推
- (void)enterTextIntoCurrentFirstResponder:(NSString *)text;
//判断fallbackView是否是可输入控件(UITextField,UITextView,UISearchBar),若是则输入文本


//方式二
//对指定视图中的UIAccessibilityElement输入文本,element和view用于定位点击操作,使得view作为第一响应可以输入文本
//text:输入的文本 
//expectedResult:函数内部会拿该值去检测完成输入后的文本是否完全匹配expectedResult值
//类似断言,即调用- (void)expectView:(UIView *)view toContainText:(NSString *)expectedResult方法
- (void)enterText:(NSString *)text intoElement:(UIAccessibilityElement *)element inView:(UIView *)view expectedResult:(NSString *)expectedResult;
- (void)expectView:(UIView *)view toContainText:(NSString *)expectedResult;

//方式三
//与方式二类事,不同在于寻找控件的条件不同(accessibilityLabel,accessibilityTraits)
- (void)enterText:(NSString *)text intoViewWithAccessibilityLabel:(NSString *)label;
- (void)enterText:(NSString *)text intoViewWithAccessibilityLabel:(NSString *)label traits:(UIAccessibilityTraits)traits expectedResult:(NSString *)expectedResult;

文案删除操作

//方式一
//清除第一响应的文本
- (void)clearTextFromFirstResponder;

//方式二
//清除符合条件的控件文案
- (void)clearTextFromViewWithAccessibilityLabel:(NSString *)label;
- (void)clearTextFromViewWithAccessibilityLabel:(NSString *)label traits:(UIAccessibilityTraits)traits;

//方式三
//与方式二一致,寻找条件不一致
- (void)clearTextFromElement:(UIAccessibilityElement *)element inView:(UIView *)view;


//文案删除后再输入指定文案
- (void)clearTextFromAndThenEnterTextIntoCurrentFirstResponder:(NSString *)text;
- (void)clearTextFromAndThenEnterText:(NSString *)text intoViewWithAccessibilityLabel:(NSString *)label;
- (void)clearTextFromAndThenEnterText:(NSString *)text intoViewWithAccessibilityLabel:(NSString *)label traits:(UIAccessibilityTraits)traits expectedResult:(NSString *)expectedResult;

UITableView

//点击
//TableView中的指定cell
//参数:
//indexPath(NSIndexPath *):cell的索引值,-1为最后一个cell
//搜索参数:AccessibilityLabel(辅助控件标示)/AccessibilityIdentifier(逻辑约束)/UITableView(控件指针)
- (void)tapRowInTableViewWithAccessibilityLabel:(NSString *)tableViewLabel atIndexPath:(NSIndexPath *)indexPath
- (void)tapRowAtIndexPath:(NSIndexPath *)indexPath inTableViewWithAccessibilityIdentifier:(NSString *)identifier
- (void)tapRowAtIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView;

//等待
//TableView中的指定cell出现
//参数
//indexPath(NSIndexPath *):cell的索引值,-1为最后一个cell
//atPosition(UITableViewScrollPosition):指定cell移动到的位置,如TableView显示的底部,中部,顶部
//搜索条件:AccessibilityIdentifier(逻辑约束)/UITableView(控件指针)
- (UITableViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inTableViewWithAccessibilityIdentifier:(NSString *)identifier;
- (UITableViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inTableViewWithAccessibilityIdentifier:(NSString *)identifier atPosition:(UITableViewScrollPosition)position;
- (UITableViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView;
- (UITableViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView atPosition:(UITableViewScrollPosition)position;

//编辑模式下,移动table cell
//TableView的cell
//参数:
//sourceIndexPath:滑动的起始索引
//destinationIndexPath:滑动的终止索引
//搜索条件:AccessibilityIdentifier(逻辑约束)/UITableView(控件指针)
- (void)moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath inTableViewWithAccessibilityIdentifier:(NSString *)identifier;
- (void)moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath inTableView:(UITableView *)tableView;

//其他
//向某个方向拖拽指定cell
- (void)swipeRowAtIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView inDirection:(KIFSwipeDirection)direction;
//等待指定cell出现删除状态
//cell(UITableViewCell*):cell对象指针
- (void)waitForDeleteStateForCell:(UITableViewCell*)cell;
- (void)waitForDeleteStateForCellAtIndexPath:(NSIndexPath*)indexPath inTableView:(UITableView*)tableView;

CollectionView

//与TableView的使用基本一致
- (void)tapItemAtIndexPath:(NSIndexPath *)indexPath inCollectionViewWithAccessibilityIdentifier:(NSString *)identifier;
- (void)tapItemAtIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView;
- (UICollectionViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView;
- (UICollectionViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inCollectionViewWithAccessibilityIdentifier:(NSString *)identifier;

UIPickerView

//选择对应UIPickerView的单元
//参数:
//title(NSString *):单元的标题
//component:UIPickerView对应的列
//searchOrder(KIFPickerSearchOrder):滑动UIPickerView单元的顺序(4种,具体参考源码)
- (void)selectPickerViewRowWithTitle:(NSString *)title;
- (void)selectPickerViewRowWithTitle:(NSString *)title inComponent:(NSInteger)component;
- (void)selectPickerViewRowWithTitle:(NSString *)title inComponent:(NSInteger)component withSearchOrder:(KIFPickerSearchOrder)searchOrder;

UIDatePicker

//与UIPickerView基本操作一致,具体可以参考源码
- (void)selectDatePickerValue:(NSArray *)datePickerColumnValues;
- (void)selectDatePickerValue:(NSArray *)datePickerColumnValues withSearchOrder:(KIFPickerSearchOrder)searchOrder;

Switch

//开关控件
//参数:
//switchIsOn:控制开还是关
//搜索参数:AccessibilityLabel辅助功能标示/UIAccessibilityElement+ switchView
- (void)setOn:(BOOL)switchIsOn forSwitchWithAccessibilityLabel:(NSString *)label;
- (void)setSwitch:(UISwitch *)switchView element:(UIAccessibilityElement *)element On:(BOOL)switchIsOn;

Slider

//滑块控件
//value:滑动程度
//搜索参数:AccessibilityLabel辅助功能标示/UISlider 对象指针控件
- (void)setValue:(float)value forSliderWithAccessibilityLabel:(NSString *)label;
- (void)setValue:(float)value forSlider:(UISlider *)slider;

Stepper

- (void)tapStepperWithAccessibilityLabel:(NSString *)accessibilityLabel increment:(KIFStepperDirection)stepperDirection;
- (void)tapStepperWithAccessibilityElement:(UIAccessibilityElement *)element increment:(KIFStepperDirection)stepperDirection inView:(UIView *)view;

系统性操作

动画相关

//等待动画消失
//参数:
//timeout(NSTimeInterval):等待超时时间
//stabilizationTime(NSTimeInterval):睡眠时间,一般睡眠时间内不会探测动画是否结束
- (void)waitForAnimationsToFinish;
- (void)waitForAnimationsToFinishWithTimeout:(NSTimeInterval)timeout;
- (void)waitForAnimationsToFinishWithTimeout:(NSTimeInterval)timeout stabilizationTime:(NSTimeInterval)stabilizationTime;

键盘

//等待键盘出现
- (void)waitForSoftwareKeyboard;
//等待键盘出现,且可输入
- (void)waitForKeyInputReady;
//等待键盘消失
- (void)waitForAbsenceOfSoftwareKeyboard;

相册

//选择指定相册的指定位置图片
//参数:
//albumName:相册名
//row:行
//column:列
- (void)choosePhotoInAlbum:(NSString *)albumName atRow:(NSInteger)row column:(NSInteger)column;

第一响应

等待目标控件活动第一响应
- (void)waitForFirstResponderWithAccessibilityLabel:(NSString *)label;
- (void)waitForFirstResponderWithAccessibilityLabel:(NSString *)label traits:(UIAccessibilityTraits)traits;

通知/URL的监听

//监听某个对象接受的指定通知
//参数:
//name:通知名称
//object:监听的对象
//block:监听后需要执行的代码块
- (NSNotification *)waitForNotificationName:(NSString*)name object:(id)object;
- (NSNotification *)waitForNotificationName:(NSString *)name object:(id)object whileExecutingBlock:(void(^)())block;

//监听指定的URL访问
//基本原理是通过hock openURL来实现
//参数
//URLString:监听的目标url
//block:监听到后需要执行的代码块
//returnValue:标记位,hock函数是否打开
- (void)waitForApplicationToOpenURL:(NSString *)URLString whileExecutingBlock:(void(^)())block returning:(BOOL)returnValue;
- (void)waitForApplicationToOpenAnyURLWhileExecutingBlock:(void(^)())block returning:(BOOL)returnValue;
//与上述函数一致,差异在于传递的URLScheme(通过配置可以在浏览器打开应用)
- (void)waitForApplicationToOpenURLWithScheme:(NSString *)URLScheme whileExecutingBlock:(void (^)())block returning:(BOOL)returnValue;

其他

//在屏幕上关闭一个popover的弹窗
- (void)dismissPopover;

//点击status bar
- (void)tapStatusBar;

//模拟home键点击
- (void)deactivateAppForDuration:(NSTimeInterval)duration
// 关闭系统弹窗,关闭成功返回ture,反之返回false
- (BOOL)acknowledgeSystemAlert;

//设置指定视图的文案
- (void)setText:(NSString *)text intoViewWithAccessibilityLabel:(NSString *)label;

//获取指定视图的文案
- (NSString *)textFromView:(UIView *)view;

//截屏
- (void)captureScreenshotWithDescription:(NSString *)description;

//模拟内存报警
- (void)simulateMemoryWarning;

//模拟横竖屏切换
- (void)simulateDeviceRotationToOrientation:(UIDeviceOrientation)orientation;
上一篇下一篇

猜你喜欢

热点阅读