4. iOS測試經常使用方法

     1.    [XCUIElement exists]方法只能肯定這個View是否存在,即便不在當前屏幕上也返回True。若是要肯定View是否在屏幕可見範圍內,能夠判斷其Frame是否在Window的Frame內。windows

     XCUIElement *window = [app.windows elementBoundByIndex:0];
     if (CGRectContainsRect([window frame], [cell frame])) {
         [cell tap];
     }
app

     2.  等待一個控件出現的方法(登陸結果,是否已經跳轉到另外一個VC)
       lua

    左邊的VC點擊肯定會跳轉到右邊的VC
    XCUIElement *alertView = app.alerts.collectionViews.buttons[@"肯定"];
    if ([alertView exists]) {
        XCTAssertTrue([app.alerts.staticTexts[@"登陸成功"] exists]);
        XCUIElement *nextVC = app.staticTexts[@"B"];
       
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"exists == true"];
       
        [self expectationForPredicate:predicate evaluatedWithObject:nextVC handler:nil];
       
        [alertView tap];
       
        [self waitForExpectationsWithTimeout:5 handler:nil];    //最多等待5秒直到B出現
       
        XCTAssertTrue([app.staticTexts[@"B"] exists]);
    }
spa

   

   3.  查看Query的過程
   使用打印方法查看[XCUIElement(Query) debugDescription]。能夠從DebugDescription中識別控件的Bounds屬性識別控件,以下:debug

    

   

   4.  得到TableView的自帶控件3d

   

   XCUIElement *prepareDelete = app.buttons[@"Delete 1"];
   [prepareDelete tap]; //點擊左側編輯按鈕
       
   XCUIElement *delete = app.buttons[@"Delete"];
   [delete tap]; //點擊左滑出的Delete按鈕(可能不叫Delete)
orm

   

   

   XCUIElement *reorder = app.buttons[@"Reorder 1"];     //第一個Cell的重排按鈕(名稱爲Reorder和具體數據)
   XCUIElement *reorder2 = app.buttons[@"Reorder 2"];     //第二個cell的重排按鈕
   [reorder pressForDuration:1 thenDragToElement:reorder2]; //將第一個Cell移動到第二個Cell   
ip

   
   5.  如何滑動刷新element

   滑動刷新的就是實現足夠距離的滑動操做。it

   實現方式主要是兩種:

   1.從屏幕上取兩個點進行滑動   

   2.調用可滑動頁面內的控件的[SwipeUp/Down]方法。 

   例子:

   1. 取點滑動

   XCUIElement *cell = [app.tables.cells elementBoundByIndex:0]; //最上方的Cell
   XCUICoordinate *start = [cell coordinateWithNormalizedOffset:CGVectorMake(0, 0)];
   XCUICoordinate *end = [cell coordinateWithNormalizedOffset:CGVectorMake(0, 6)]; //屏幕外一個點,dy=6聽說是刷新要求的最小值
   [start pressForDuration:0 thenDragToCoordinate:end];
   

   2. Swipe

  XCUIElement *table = [app.tables elementBoundByIndex:0];

  [table swipeUp];

   PS:對自身不可滑動控件進行Swipe滑動,若控件在可滑動控件內,會致使這個可滑動控件滑動。如上滑TableviewCell會使其Tableview上滑。

    6.  按動硬件按鈕的方法
    XCUIDevice *device = [XCUIDevice sharedDevice];
    [device pressButton:XCUIDeviceButtonHome]; //枚舉只有三個值 Home鍵,音量Up,音量Down(模擬器只能按Home鍵)

    7.  點擊被遮擋控件的方法

    XCUIElement *cell = [collectView.cells elementBoundByIndex:i];
    if ([cell isHittable]) {
      [cell tap];
    } else {
      XCUICoordinate *coo = [cell coordinateWithNormalizedOffset:CGVectorMake(0, 0)];
      [coo tap];     }   PS:只能用點點擊時,若點在屏幕外,目前只會出現滑動操做。

相關文章
相關標籤/搜索