UI Testing: Failed to get refreshed snapshot

After pressing a button that loads a new page I get an error saying "UI Testing Failure - Failed to get refreshed snapshot"


The previous page where I press the button works fine. All the objects can be tapped and modified.

Once I move onto the next page nothing can be pressed. I remember during the WWDC video on UI Testing the guy said that when the displays change the elements are re-evalutated to get the most current data. Is that not working right now? Has anyone else had this problem/know of a fix?

howard-jd, I am experiencing the same thing with regard to the waitForExpectation call. I was seeing it happening when I had a table with many cells, and a lot of customized content. I found that if I cut down on the number of cells, and therefore the number of items the UITest had to query for a match to "element", it would no longer fail.


I wonder if UITest is just not equipped to handle a ton of content on screen.

By any chance did you experience this only on a device, and not the simulator, as vinayakap and howard-jd experienced?


I am experiencing this intermittently as well, also about every 3rd try, but I actually don't see it on a device.

I It looks like the element to tap is there but the test does not get it. This is why even waiForExpectationsWithTimeout does not help to avoid this bug.


What helped for me is doing some scrolling before tapping like this:

    XCUIElement *scrollView = app.scrollViews.element;
    [scrollView swipeUp];
    [scrollView swipeDown];
   
    [myButton tap];

I suspect that works because it's in a way idling the app and it gives the accessibility framework time to catch up.

I am still seeing this in Xcode 7.3 beta. Filed a radar:

24271237

I think its trying to synthesize an event while the app is still loading cells

If you dont want to change how the app works you can force it to wait until all the cells are loaded.


Here's a helper funciton I wrote:


func waitForTableCellsCountExact(appUI_Element: XCUIElementQuery, exactly: UInt) {
        let x = appUI_Element
        expectationForPredicate(NSPredicate(format: "count == \(exactly)"), evaluatedWithObject: x, handler: nil)
        waitForExpectationsWithTimeout(30, handler: nil)
    }


i.e If you know there should 50 cells present in your table view

waitForTableCellsCountExact(app.tables.cells, exactly: 50)


set a breakpoint at the failure and use the debugger to find how many cells are loaded

po XCUIApplication().tables.cells.count

wait a little and do it again

po XCUIApplication().tables.cells.count


If the number doesnt change that should give you a good indication of the number of cells to wait for before proceeding.


if it does change wait for a few minutes then check again. If your app is loading cells continuosly forever this wont work, but you probably shouldnt be loading cells forever anyway

Sadly, this is not working for me.


I have a similar context as jamhughes with a UIPickerView showing 100 000 rows.


I tried your helper function and it only moved the crash there.

I am not aware if it could actually handle 100,000 cells, but I guess you could try and wait for all of them to load.


The helper funciton has the following line...

waitForExpectationsWithTimeout(30, handler: nil)


The 30 is the number of seconds it waits. if you are actually loading all of the elements, then you could increase this number.


Have you let the app load all the elements before? If so get an estimate of how long it took. If it took 3 minutes try changing that line to

waitForExpectationsWithTimeout(180, handler: nil)


Not sure if it will work but I geuss its worth a shot.

Do you still want bug reports for this issue? I don't want to take the time if it's just going to get resolved Duplicate, unless it helps.

Believe I'm seeing this in Xcode 7.3 too. Bug # is 25427379

Had this issue just after app launch in Xcode 7.3. Adding a sleep(1) seems to have fixed it.

Joar, do you know of any updates regarding this bug? thanks.

Woohoo. Thanks so much @chasehollandmsft. Adding a sleep(1) seems to fix the issue for me. I encountered this problem when UI Tests fail waiting for the "OK" button from Facebook after clicking on "Log in with Facebook". Below is my new helper function for this:


private func waitForElementToAppear(element: XCUIElement,
                                        file: String = #file, line: UInt = #line) {
         //Wait for a UI element to appear.  See http://masilotti.com/xctest-helpers/

          // Need this to avoid "Failure getting snapshot error." See https://forums.developer.apple.com/thread/6437
        sleep(1)

        let existsPredicate = NSPredicate(format: "exists == true")
        expectationForPredicate(existsPredicate,
                                evaluatedWithObject: element, handler: nil)
    
        waitForExpectationsWithTimeout(10) { (error) -> Void in
            if (error != nil) {
                let message = "Failed to find \(element) after 10 seconds."
                self.recordFailureWithDescription(message,
                                                  inFile: file, atLine: line, expected: true)
            }
        }
    }

I've got the same problem. The problem is intermittent. It happens about every a hundred run.


In my situation, a UIPageViewController has 8 UICollectionViewControllers as childViewControllers. Sometimes, taping on cell won't be responsive. It happens about every a hundred run.


    @available(iOS 9.0, *)
    func testExample() {
        XCUIDevice.sharedDevice().orientation = .Portrait

        let app = XCUIApplication()
        let image = app.scrollViews.otherElements.collectionViews.cells.otherElements.containing(.staticText, identifier: "will do").children(matchingType: .image).element(boundBy: 0)
        image.swipeDown()
        image.tap()
  
        let button = app.navigationBars["willDo"].buttons["Back"]
        button.tap()
        for _ in 0...98 {
            image.swipeDown()
            image.tap()
            button.tap()
        }
    }

Due to thie post https://forums.developer.apple.com/message/194178#194178
I am finding that the problem is with sim/devices using 9.0^ and that updating your operating system and using iOS 10.1, the probelm seems to be resolved. I am still unsure of its long term stability, but worth a try.

UI Testing: Failed to get refreshed snapshot
 
 
Q