4. iOS测试常用方法

     1.    [XCUIElement exists]方法只能确定这个View是否存在,即使不在当前屏幕上也返回True。如果要确定View是否在屏幕可见范围内,可以判断其Frame是否在Window的Frame内。

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

     2.  等待一个控件出现的方法(登录结果,是否已经跳转到另一个VC)
      

    左边的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]);
    }

   

   3.  查看Query的过程
   使用打印方法查看[XCUIElement(Query) debugDescription]。可以从DebugDescription中识别控件的Bounds属性识别控件,如下:

    

   

   4.  获得TableView的自带控件

   

   XCUIElement *prepareDelete = app.buttons[@"Delete 1"];
   [prepareDelete tap]; //点击左侧编辑按钮
       
   XCUIElement *delete = app.buttons[@"Delete"];
   [delete tap]; //点击左滑出的Delete按钮(可能不叫Delete)

   

   

   XCUIElement *reorder = app.buttons[@"Reorder 1"];     //第一个Cell的重排按钮(名称为Reorder和具体数据)
   XCUIElement *reorder2 = app.buttons[@"Reorder 2"];     //第二个cell的重排按钮
   [reorder pressForDuration:1 thenDragToElement:reorder2]; //将第一个Cell移动到第二个Cell   

   
   5.  如何滑动刷新

   滑动刷新的就是实现足够距离的滑动操作。

   实现方式主要是两种:

   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:只能用点点击时,若点在屏幕外,目前只会出现滑动操作。

原文地址:https://www.cnblogs.com/luerniu/p/6393155.html