Objective -C XCTest:如何在表格单元格中一直向上滑动

Posted

技术标签:

【中文标题】Objective -C XCTest:如何在表格单元格中一直向上滑动【英文标题】:Objective -C XCTests: how to swipe all the way up in a table cells 【发布时间】:2017-08-03 00:58:33 【问题描述】:

我有一个包含 7 个单元格的表格(将来可能会增加)。只有 4 个单元格是可见的,并且可以从 screen1 上的 UI 中点击。我想点击第 7 个单元格转到 screen2 并且我成功地能够向下滚动(检查单元格是否可点击并一次滚动一个单元格)并点击第 7 个单元格但是一旦我回到 screen1,我想全部滑动到一次顶部(一次不滚动一个单元格)。我尝试使用“swipeup”,但这样做时它正在点击单元格,我被导航到我不想发生的 screen2(并且测试失败)。 我怎样才能做到这一点? 提前致谢。 向下滚动代码:

 int elementIndex = 0;
NSArray *cells = tableView.cells.allElementsBoundByIndex;

for(XCUIElement *cell in cells)
NSString *name = transactionCell.staticTexts[@"Name"].label;
            if (!cell.hittable) 
                [Testfunc scrollDown:tableView withApp:app index:elementIndex-1];

            
        [cell tap];
        elementIndex++;
        //do stuff if this is the correct cell else go to next cell
        

+ (void) scrollDown:(XCUIElement*)tableView withApp:(XCUIApplication*)app index:(int)elemntIndex 
    XCUIElementQuery* tableCells = tableView.cells;
    int tableCount = (int)tableCells.count;
    if(index > 0 && elemntIndex < tableCells.count-1)
        XCUIElement* startElement = [tableCells elementBoundByIndex:elemntIndex];
        XCUIElement* endElement = [tableCells elementBoundByIndex:elemntIndex-1];
        [startElement pressForDuration:0.1 thenDragToElement:endElement];

    

我尝试通过以下方式一直向上滚动:

XCUIElement *elementSwipe = transactionCell.staticTexts[name];
[elementSwipe swipeUp];

然后它在向上滚动之前点击单元格,然后我被导航到 screen2。然后测试失败。

【问题讨论】:

【参考方案1】:

您可以尝试点击状态栏,例如: XCUIApplication().coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.02)).tap()

它会一直滚动到顶部。

【讨论】:

【参考方案2】:

点击状态栏,它会滚动到顶部。

let app = XCUIApplication()
app.statusBars.element(boundBy: 0).tap()

【讨论】:

以上是关于Objective -C XCTest:如何在表格单元格中一直向上滑动的主要内容,如果未能解决你的问题,请参考以下文章