简体   繁体   English

Xcuitest - 我怎样才能等到图像完成加载

[英]Xcuitest - How can I wait until image finished to load

I'm running my UiTests on app that uses images/videos from the camera roll/Google photos/Etc..我正在使用相机胶卷/谷歌照片/等中的图像/视频的应用程序上运行我的 UiTests。

All the images/videos are in CollectionView and each asset is in a specific cell.所有的图像/视频都在 CollectionView 中,每个资产都在一个特定的单元格中。

When the app opens the "footage screen" it contains 15 assets at the same time and I'm trying to create a function that waits until all of the images finish to load.当应用程序打开“素材屏幕”时,它同时包含 15 个资产,我正在尝试创建一个 function 等待所有图像完成加载。

Why do I need it: I have tests that select images/videos and sometimes the Xctest could select the first one because not all of the assets finish loading.为什么需要它:我有测试 select 图像/视频,有时 Xctest 可能是 select 第一个,因为并非所有资产都完成加载。

Note: I don't have any indication that tells me "all the assets finish to load".注意:我没有任何迹象表明“所有资产都已完成加载”。

Any suggestions?有什么建议么?

  • You should inspect your app.debugDescription before and after loading.您应该在加载前后检查您的app.debugDescription Find anything that you can asynchronously verify.查找可以异步验证的任何内容。

  • If necessary, add accessibility identifiers into app code, that are different before/after loading.如有必要,将可访问性标识符添加到应用程序代码中,这些标识符在加载之前/之后是不同的。 You should also add some accessibility labels to help VoiceOver and VoiceControl users to interact with your app.您还应该添加一些辅助功能标签以帮助 VoiceOver 和 VoiceControl 用户与您的应用程序交互。

  • You can use SUITCase for screenshot testing and awaits.您可以使用 SUITCase 进行屏幕截图测试和等待。 https://github.com/devexperts/suitcase/ https://github.com/devexperts/suitcase/

  • You can add some EarlGrey 2 asserts.您可以添加一些 EarlGrey 2 断言。 This testing framework is a little bit more flexible and automatically await async operations.这个测试框架更灵活一点,自动等待异步操作。 The good thing is you can use it alongside existing XCTest tests.好处是您可以将它与现有的 XCTest 测试一起使用。

  • Finally, you can just sleep(5) :D最后,您可以sleep(5) :D

There is expectation method in XCTest framework to test asynchronous work. XCTest框架中有期望方法来测试异步工作。

expectation 期待

Use this method to create XCTestExpectation instances that can be fulfilled when asynchronous tasks in your tests complete.使用此方法创建 XCTestExpectation 实例,这些实例可以在测试中的异步任务完成时完成。

You can create a function for the use of existence prediction like below:您可以创建一个 function 用于存在预测,如下所示:

// create a function to wait for prediction
func waitNotExistance(for element: XCUIElement, timeout: Double = 5) {
    let notExists = NSPredicate(format: "exists != 1")
    let elementShown = expectation(for: notExists, evaluatedWith: element)
    wait(for: [elementShown], timeout: timeout, enforceOrder: false)
}

Assume that you set accessibilityIdentifier " loading " as initial value and " loaded " after completion.假设您将accessibilityIdentifierloading ”设置为初始值,并在完成后“ loading ”。 You can wait until non-existence for them.你可以等到他们不存在。

Usage :用法

// your loading cell
let ladingElement = app.collectionViews.element(boundBy: 0).cells.matching(identifier: "loading").element

// wait until timeout reached 
waitNotExistance(for: ladingElement)

// assert here what you expect..

The second suggestion resolve the problem,第二个建议解决问题,

I've added an accessibility identifiers to the cells that containing the images 1: When the image is still loading "selectFootageCellImageEmpty" 2. When the image finishes loading "selectFootageCellImageLoaded" And now I wait until there is no "selectFootageCellImageEmpty" in the screen for example:我已经为包含图像的单元格添加了可访问性标识符 1:当图像仍在加载“selectFootageCellImageEmpty”时 2. 当图像完成加载“selectFootageCellImageLoaded” 现在我等到屏幕上没有“selectFootageCellImageEmpty”例子:

        let endOfLoop = Date().addingTimeInterval(TimeInterval(10))
        while app.collectionViews.element(boundBy: 2).cells.matching(identifier: "selectFootageCellImageEmpty").element.exists {
        sleep(1)
        print("Waiting for all images to finish loading")
        if Date() > endOfLoop {
            break
        }
    }
  • I could add an XCTAssert instead of the "break" line but I don't need it at the moment.我可以添加一个 XCTAssert 而不是“break”行,但我现在不需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM