【问题标题】:Scroll the cells using UI Testing使用 UI 测试滚动单元格
【发布时间】:2017-04-16 21:00:46
【问题描述】:

有没有类似的方法

- (void)scrollByDeltaX:(CGFloat)deltaX deltaY:(CGFloat)deltaY;

iOS 版?

我认为上述方法仅适用于 OSX。 我想根据提供的 deltavalues 滚动我的 tableview。

提前致谢。

【问题讨论】:

    标签: ios uitableview scroll xctest


    【解决方案1】:

    这个对我有用的 Swift4 版本。希望它对未来的人有所帮助。

    let topCoordinate = XCUIApplication().statusBars.firstMatch.coordinate(withNormalizedOffset: .zero)
    let myElement = XCUIApplication().staticTexts["NameOfTextLabelInCell"].coordinate(withNormalizedOffset: .zero)
    // drag from element to top of screen (status bar)
    myElement.press(forDuration: 0.1, thenDragTo: topCoordinate)
    

    【讨论】:

      【解决方案2】:

      在 iOS 上,如果你想在元素方面移动,你可以使用XCUIElement.press(forDuration:thenDragTo:)

      要根据相对坐标移动,可以获取元素的XCUICoordinate,然后使用XCUICoordinate.press(forDuration:thenDragTo:)

      let table = XCUIApplication().tables.element(boundBy:0)
      
      // Get the coordinate for the bottom of the table view
      let tableBottom = table.coordinate(withNormalizedOffset:CGVector(dx: 0.5, dy: 1.0))
      
      // Scroll from tableBottom to new coordinate
      let scrollVector = CGVector(dx: 0.0, dy: -30.0) // Use whatever vector you like
      tableBottom.press(forDuration: 0.5, thenDragTo: tableBottom.withOffset(scrollVector))
      

      或者在 Objective-C 中:

      XCUIApplication *app = [[XCUIApplication alloc] init];
      XCUIElement *table = [app.tables elementBoundByIndex: 0];
      
      // Get the coordinate for the bottom of the table view
      XCUICoordinate *tableBottom = [table coordinateWithNormalizedOffset:CGVectorMake(0.5, 1.0)];
      
      // Scroll from tableBottom to new coordinate
      CGVector scrollVector = CGVectorMake(0.0, -30.0); // Use whatever vector you like
      [tableBottom pressForDuration:0.5 thenDragToCoordinate:[tableBottom coordinateWithOffset:scrollVector]];
      

      【讨论】:

      • 这段代码对我有用,但有一些变化(我在 WebView 而不是表格上使用它)。首先,.coordinate 方法在单词Normalized 中具有z 的参数(对于Swift 部分);其次,我必须将这个标准化偏移定位到 WebView 的中心(dxdy 都传递 0.5),因为 WebView 在底部有一些按钮;第三,我不得不将初始按下持续时间从 0.5 降低到 0.2,因为按住半秒会导致 WebView 选择文本。
      • 将我的 sn-p 更新为标准化的 z,对此感到抱歉!
      【解决方案3】:

      Oletha 的答案正是我想要的,但在 Objective-C 示例中有几个小错误。由于编辑被拒绝,我将其包含在此处作为其他任何人的回复:

      XCUIApplication *app = [[XCUIApplication alloc] init];
      XCUIElement *table = [app.tables elementBoundByIndex: 0];
      
      // Get the coordinate for the bottom of the table view
      XCUICoordinate *tableBottom = [table 
      coordinateWithNormalizedOffset:CGVectorMake( 0.5, 1.0)];
      
      // Scroll from tableBottom to new coordinate
      CGVector scrollVector = CGVectorMake( 0.0, -30.0); // Use whatever vector you like
      [tableBottom pressForDuration:0.5 thenDragToCoordinate:[tableBottom coordinateWithOffset:scrollVector]];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-09
        • 1970-01-01
        相关资源
        最近更新 更多