1: 在podfile中添加單元測試依賴庫
1:在pod file添加依賴庫 不添加單元測試會報file not found錯誤
target 'ServiceEngineerUITests' do
pod 'FMDB', '~> 2.5'
pod 'MJExtension', '~> 3.0.9'
end
重新pod install
2: 選中ServiceEngineerUITests.m
光標點擊代碼中的某一處亚亲,此時紅點可點擊,而光標停留的地方將是代碼錄制的起點幽崩。點擊紅點葛账,然后操作界面.
![](https://github.com/CrasyEvan/ImageSource/blob/master/UITest_0.png?raw=true)
代碼會在此處自動生成
![](https://github.com/CrasyEvan/ImageSource/blob/master/UITest_2.png?raw=true)
3最后運行測試找颓,可以選擇test運行,但最好的方式是直接點擊testExample方法左邊的勾勾運行,簡單易用奈泪。
![](https://github.com/CrasyEvan/ImageSource/blob/master/UITest_3.png?raw=true)
4:需要注意的地方
代碼測試自動生成的代碼
eg:[tabBarsQuery.buttons[@"\U6211"] tap]會報錯!解決辦法:將所有大寫的U全部換成小寫u即可
eg:[tabBarsQuery.buttons[@"\u6211"] tap];
5:通過測試語句找到對應(yīng)的UI元素并通過XCTAssert進行測試
XCUIApplication 啟動 終止測試
XCUIElement 元素
XCUIElementQuery 查詢語句
首先 UI tests 是基于事件響應(yīng)來測試的灸芳,主要分為下面幾個步驟
1:查詢涝桅,找到頁面的元素。
2:明確元素的預(yù)期事件行為烙样,以此作為參考
3:點擊或單擊元素獲取響應(yīng)
4:通過每一步的響應(yīng)判斷預(yù)期的結(jié)果是否通過或者失敗冯遂。
一般步驟
1:通過XCUIElementQuery來找到XCUIElement
2:給元素 XCUIElement傳遞事件
3:使用斷言來將XCUElement的當(dāng)前狀態(tài)和預(yù)期的狀態(tài)進行比較
XCUIElement
XCUIElement 提供了輕觸,點擊误阻,移動债蜜,捏合晴埂,旋轉(zhuǎn)等一系列交互。
判斷當(dāng)前元素是否存在
(readonly) BOOL exists;
可否點擊
@property (readonly, getter = isHittable) BOOL hittable;
返回指定元素的所有子視圖(包含子視圖的子視圖)類元素查詢語句
- (XCUIElementQuery *)descendantsMatchingType:(XCUIElementType)type;
返回指定元素的所有子視圖元素查詢語句
- (XCUIElementQuery *)childrenMatchingType:(XCUIElementType)type;
提供元素的調(diào)試信息
@property (readonly, copy) NSString *debugDescription;
通過XCUIElement category來實現(xiàn)元素的一些列動作相應(yīng)
@interface XCUIElement (XCUIElementEventSynthesis)
//輸出text到element
- (void)typeText:(NSString *)text;
- (void)tap; //點擊事件
- (void)doubleTap; //雙擊事件
- (void)pressForDuration:(NSTimeInterval)duration; //持續(xù)按壓
- (void)swipeUp; //向上滑動
- (void)swipeDown; //下滑
- (void)swipeLeft; //左右 ...
XCUIElementQuery頭文件(遵循XCUIElementTypeQueryProvider協(xié)議)
獲得傳入的索引值所在的元素,返回XCUIElement對象寻定。只能從當(dāng)前對象的查找儒洛。更深層次的元素不在查找范圍內(nèi)
elementAtIndex
根據(jù)NSPredicate定義的匹配條件查找元素。返回XCUIElement對象狼速。只能從當(dāng)前對象中查找琅锻。更深層次的元素不在查找范圍內(nèi)
elementMatchingPredicate
根據(jù)元素類型(XCUIElementType)和id號來匹配查找元素。返回XCUIElement對象向胡。只能從當(dāng)前對象中查找恼蓬。更深層次的元素不在查找范圍內(nèi)
elementMatchingType
傳入XCUIElementType作為匹配條件,得到匹配的XCUIElementQuery對象僵芹,查找對象為當(dāng)前控件的子子孫孫控件处硬。返回XCUIElementQuery對象
descendantsMatchingType:(XCUIElementType)type;
傳入XCUIElementType作為匹配條件,得到匹配的XCUIElementQuery對象拇派,查找對象為當(dāng)前控件的兒子控件荷辕。返回XCUIElementQuery對象
childrenMatchingType:(XCUIElementType)type;
傳入NSPredicate作為過濾器,得到XCUIElementQuery對象件豌。返回XCUIElementQuery對象
matchingPredicate
傳入XCUIElementType和id號作為匹配條件疮方,得到XCUIElementQuery。返回XCUIElementQuery對象
matchingType
傳入id號作為匹配條件茧彤,得到XCUIElementQuery骡显。返回XCUIElementQuery對象
matchingIdentifier傳入NSPredicate過濾器作為匹配條件。從子節(jié)點中找到包含該條件的XCUIElementQuery對象
containingPredicate
傳入XCUIElementType和id作為匹配條件曾掂。從子節(jié)點中找到包含該條件的XCUIElementQuery對象
containingType
query用element表示形式惫谤,如果query中只有一個元素,可以講element當(dāng)成真正的element遭殉,執(zhí)行點擊等操作石挂,從這一方面來講XCUIElementQuery其實也是一種XCUIElement對象,只是是用來存放0~N個XCUIElement的容器险污。得到XCUIElement對象.
query中找到的元素數(shù)量,得到整數(shù)
count
query中根據(jù)accessibility element得到的元素數(shù)組痹愚。得到XCUIElement數(shù)組
allElementsBoundByAccessibilityElement
query中根據(jù)索引值得到的元素數(shù)組。得到XCUIElement數(shù)組
allElementsBoundByIndex
調(diào)試信息
debugDescription
@protocol XCUIElementTypeQueryProvider
@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 *otherElements;
XCUIApplication *app = [[XCUIApplication alloc] init];
// app.staticTexts 判斷靜態(tài)文本是否存在
XCTAssert(app.staticTexts[@“test1"].exists);
查詢用例:
實例化app
XCUIApplication *app = [[XCUIApplication alloc] init];
獲取當(dāng)前控制器的view元素
XCUIElement *element =
[[[[app.otherElementscontainingType:XCUIElementTypeNavigationBar identifier:@"View"]childrenMatchingType:XCUIElementTypeOther].elementchildrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element;
獲取當(dāng)前控制器的View上的Label(通過label的text)
獲取當(dāng)前控制器的View上的Button(通過Button的title)
XCUIElement *testLabel = [app.staticTexts[@"Label"] //testLabel的text為Label
XCUIElement *testButton = [app.buttons[@"Button"]; //testButton的Title為Button
獲取當(dāng)前elment元素上的UITextField元素, 如果有多個, 則通過Index來區(qū)分
XCUIElement *textField = [[element childrenMatchingType:XCUIElementTypeTextField] elementBoundByIndex:0];
[textField tap]; //點擊TextField
[textField typeText:@"test"]; //輸入test
當(dāng)前控制器的View上的Image(通過圖片的名字獲取)
XCUIElement *element = app.images[@"Image_1.png"]
測試:
XCTAssertTrue
XCTAssertEqualObjects
XCTAssertGreaterThan
......