UI testing fails with error “Failed to get snapshot within 15.0s”

冷暖自知 提交于 2020-05-08 17:23:32

问题


I am having a table view with a huge amount of cells. I tried to tap a particular cell from this table view. But the test ending with this error:

Failed to get snapshot within 15.0s

I assume that, the system will take snapshot of the whole table view before accessing its element. Because of the huge number of cells, the snapshot taken time was not enough (15 secs is may be the system default time).

I manually set the sleep time / wait time (I put 60 secs). Still I can not access the cells after 60 seconds!!

A strange thing I found is, before accessing the cell, I print the object in Debugger like this:

po print XCUIApplication().cells.debugDescription

It shows an error like

Failed to get snapshot within 15.0s

error: Execution was interrupted, reason: internal ObjC exception breakpoint(-3)..

The process has been returned to the state before expression evaluation.

Again if I print the same object using

po print XCUIApplication().cells.debugDescription

Now it will print all cells in the tableview in Debugger view.

No idea about why this happens. Does anyone faced similar kind of issues? Help needed!!


回答1:


I assume that, the system will take snapshot of the whole table view before accessing its element.

Your assumption is correct but there is even more to the story. The UI test requests a snapshot from the application. The application takes this snapshot and then sends the snapshot to the test where the test finally evaluates the query. For really large snapshots (like your table view) that means that:

  1. The snapshot takes a long time for the application to generate and
  2. the snapshot takes a long time to send back to the test for query evaluation.

I'm at WWDC 2017 right now and there is a lot of good news about testing - specifically some things that address your exact problem. I'll outline it here but you should go watch WWDC 2017 Session 409 and skip to timestamp 17:10.

The first improvement is remote queries. This is where the test will transmit the query to the application, the application will evaluate that query remotely from the test and then only transmit back the result of that query. Expect a marginal improvement from this enhancement ~20% faster and ~30% less memory.

The second improvement is query analysis. This enhancement will reduce the size of snapshots taken by using a minimum attribute set for taking snapshots. This means that full snapshots of the view are not taken by default when evaluating a query. Example is if you're querying to tap a button, the snapshot is going to be limited to buttons within the view. This means writing less ambiguous queries are even more important. I.e. if you want to tap a navigation bar button, specify it in the query like app.navigationBars.buttons["A button"]. You'll see even more performance improvement from this enhancement ~50% faster and ~35% less memory

The last and most notable (and dangerous) improvement is what they're calling the First Match API. This comes with some trade offs/risks but offers the largest performance gain. It offers a new .firstMatch property that returns the first match for a XCUIElement query. Ambiguous matches that result in test failures will not occur when using .firstMatch, so you run the risk of evaluating or performing an action on an XCUIElement that you didn't intend to. Expect a performance increase of ~10x faster and no memory spiking at all.

So, to answer your question - update to Xcode 9, macOS High Sierra and iOS 11. Utilize .firstMatch where you can with highly specific queries and your issue of timing out snapshots should be solved. In fact the time out issue you're experiencing might already be solved with the general improvements you'll receive from remote queries and query analysis without you having to use .firstMatch!



来源:https://stackoverflow.com/questions/44429118/ui-testing-fails-with-error-failed-to-get-snapshot-within-15-0s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!