Where is the screenshot file from Xcode UI Test?
Asked Answered
L

4

33

I'm interested how to find the location of a screenshot which is created during UI Tests of my app. I would like to understand how Fastlane Snapshot grabs this screenshot. After couple of hours of research I can't find location of those screenshots.

Lambeth answered 17/5, 2016 at 18:23 Comment(1)
Be very careful with that folder!!! In continuous integration systems it can quickly fill up with millions of screenshots, at which point you are royally screwed (you will run out of space and good luck trying to rm -rf it).Persnickety
C
28

Screenshots are stored inside your Derived Data folder. You can find this by going to Xcode, Window > Projects and click the little arrow next to your derived data path for the project.

From your project derived data folder (should be the name of your folder with a load of letters after it), go to Logs/Test.

Inside the Test folder will be test summary .plist files with UUIDs in their names (one per test run) and a folder called Attachments - the screenshots are in here.

If any of these folders aren't there, make sure you've run the tests first.

To work out which screenshot you want, you'll need to dig through the *_TestSummaries.plist file. Open it in Xcode and follow the test summary down to the SubActivity you're interested in. Some SubActivities don't have screenshots, and will have the HasScreenshotData key set to NO.

When you find a SubActivity with screenshot data, match the UUID for that SubActivity with the filename of the screenshot in the Attachments folder.

My screenshots were located at: /Users/{username}/Library/Developer/Xcode/DerivedData/{project_name}-tywebfjsswmghapfjfbzhuazjf/Logs/Test/Attachments.

In the .plist, I followed this path to find details of the screenshot data for an event: TestableSummaries > Item 0 > Tests > Item 0 > Subtests > Item 0 > Subtests > Item 0 > ActivitySummaries > Item 1 > SubActivities > Item 0.

Crossbred answered 18/5, 2016 at 13:15 Comment(3)
@Crossbred Do you know which activities cause screenshots ? It seems to me that a tap causes a shot, but it's all trial and error.Morgan
@Morgan I'm not 100% sure but most gestures (taps, swipes, scrolls...) cause screenshots, and screenshots are also often taken while locating elements or during assessing expectations. Fastlane's snapshot tool uses a rotation to an unknown orientation to trigger a screenshot event (which has no effect on the app): github.com/fastlane/fastlane/blob/master/snapshot/README.md - you can use this if you want to be in control of some screenshots. :)Crossbred
@Crossbred I already have seen the trick in github.com/fastlane/fastlane/blob/master/snapshot/lib/assets/… I was just not sure about. Turned out I can try catch it and my test loop goes on. Great! You can mention it in #39301447 so I can give you credit.Morgan
H
27

In Xcode 11, the contents of an .xcresults file changed to no longer include an Attachments folder that's browsable in Finder. You can still download individual attachments from the Test Report as shown in yoAlex5's answer, but to get the attachments without using the Xcode GUI, you have to use Apple's xcresulttool or a third party tool that leverages xcresulttool like xcparse. See this blog post for more information: https://www.chargepoint.com/engineering/xcparse/

Hardball answered 29/10, 2019 at 17:21 Comment(1)
Thank you so much. This was driving me crazy.First
D
18

Xcode screenshot file from UI Test

To make screenshot you can call the next method

func makeScreenShot() {
    let screenshot = XCUIScreen.main.screenshot()
    let fullScreenshotAttachment = XCTAttachment(screenshot: screenshot)
    fullScreenshotAttachment.lifetime = .keepAlways

    add(fullScreenshotAttachment)
}

You can find screenshots in Xcode -> Report navigator -> select your test

enter image description here

Or go to

DerivedData -> ProjectName... -> Logs -> Test -> find .xcresult -> Show Package Contents -> Attachments
#For example
/Users/<mac_name>/Library/Developer/Xcode/DerivedData/<project_name>-<...>/Logs/Test/Test-<target_name>-<...>.xcresult

[Xcode run tests]

Deuteranopia answered 28/5, 2019 at 15:46 Comment(1)
Maybe add the method inside extension XCTestCase {}Bonne
B
0

Show screenshot gallery in Xcode after UITest

  1. Run the UITest (and take screenshots)
  2. left side-menu -> Report navigator (far to the right) -> Test
  3. Then far to the right there is a "burger bar menu button" press that
  4. then press gallery. This will show screenshots that was taken with the UITest

if no screenshots show up do this:

Screenshots are automatically deleted: here is how to change the config: https://mcmap.net/q/411787/-how-to-turn-off-automatic-screenshots-on-ui-testing-xcode

Bonne answered 28/3, 2023 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.