while 循环条件不重新评估

While loop condition not reevaluating

我的视频播放器选择并播放给定的单元格。 我的目标是等到另一个视频播放后再继续代码。

    while app.tables.elementBoundByIndex(0).cells.elementBoundByIndex(0).selected {}

不知何故,在 while 循环中,重新评估不会发生(当不再选择单元格时:cell.selected -> true),但是在调试器中放置断点并手动检查时,cell.selected -> 错误

运行 在 while 循环中检查视图层次结构不会导致在每个循环之间更新视图层次结构。将针对视图层次结构的缓存版本进行检查,除非您与应用程序交互,否则不会更新该视图层次结构。

为确保每次检查更新视图层次结构,您应该使用expectationForPredicate:evaluatedWithObject:handler: and waitForExpectationsWithTimeout:handler:

class MyTestCase: XCTestCase {
    let cell = app.tables.elementBoundByIndex(0).cells.elementBoundByIndex(0)
    let notSelectedPredicate = NSPredicate(format: "selected == false")
    expectationForPredicate(notSelectedPredicate, evaluatedWithObject: cell, handler: nil)
    waitForExpectationsWithTimeout(10, handler: nil)
    // proceed with the test...
}

这样,每次检查都会刷新要检查的视图层次结构。