XCUITest 意外行为

XCUITest unexpected behaviour

我有一个带有 tableView 和详细信息的简单项目 vc。 tableView 显示带有 "cell (n)" 文本的 20 行,详细信息视图显示带有按下单元格的标签。 我想断言,只要点击一个单元格,我就会在 detail vc 标签的 tableView 中找到文本。因此,例如,如果我点击包含 "cell 3" 的单元格 3,我想获取此文本,而不是对其进行硬编码,并断言我可以在详细信息 vc.[=13 中找到此文本=]

func testCanNavigateToDetailVCWithTheTextFromCell() {
    let labelInTableView = app.staticTexts["cell 3"]

    labelInTableView.tap()

    let labelInDetailVC = app.staticTexts[labelInTableView.label]
    XCTAssertTrue(labelInDetailVC.exists)
}

这似乎有效。但我想这样做:

func testCanNavigateToDetailVCWithTheTextFromCellV2() {
    let cell = app.tables.element.cells.element(boundBy: 3)  //Get the third cell of the unique tableView

    cell.tap()

    let textFromPreviousCell = cell.staticTexts.element(boundBy: 0).label  //Since is a "Basic" cell it only has one label.
    //I will also want to set an accessilibtyIdentifier to the label and access it via cell.staticTexts["id"].label
    let labelInDetailVC = app.staticTexts[textFromPreviousCell]
    XCTAssertTrue(labelInDetailVC.exists)
}

我用这个问题建立了一个项目here

问题是您试图在点击单元格后获取单元格的文本。这意味着单元格不再出现在屏幕上(新屏幕已经出现)。您需要做的就是更改行 cell.tap()let textFromPreviousCell = cell.staticTexts.element(boundBy: 0).label 的顺序。请参阅下面的新功能:

func testCanNavigateToDetailVCWithTheTextFromCellV2() {
    let cell = app.tables.element.cells.element(boundBy: 3)  //Get the third cell of the unique tableView

    let textFromPreviousCell = cell.staticTexts.element(boundBy: 0).label  //Since is a "Basic" cell it only has one label.

    cell.tap()

    let labelInDetailVC = app.staticTexts[textFromPreviousCell]
    XCTAssertTrue(labelInDetailVC.exists)
}