I have a sample text file in my UITests target. I want to copy this file to the documents directory of the app so that when I perform tests on uploading of file in my app, I can select it via Files app and upload it.
Asked
Active
Viewed 1,136 times
7
-
1why downvote? please explain why – mownier Oct 29 '19 at 04:42
-
I didn't downvote, but what have you tried? This shouldn't be much different than copying files inside your app except in your test file. Or you could put a check in your app to see if tests are running and if so, copy the file. – shim Oct 29 '19 at 05:37
-
please upvote. i just tried copying the file in the bundle to the documents directory of the app. but when i looked into the files app, the file is not here. take note that i am doing some ui tests. – mownier Oct 29 '19 at 06:51
-
1Have you updated your info plist to include `LSSupportsOpeningDocumentsInPlace` and `UIFileSharingEnabled` or `UISupportsDocumentBrowser`? ([check out this article for example](https://www.appcoda.com/files-app-integration/)) Also if you want upvotes you should update your question with more detail, indicating what you have tried and provide information to reproduce the problem: [more info here](https://stackoverflow.com/help/how-to-ask). – shim Oct 29 '19 at 14:42
-
i will check that out. thank you. – mownier Oct 29 '19 at 22:30
-
1i added those keys in the plist but still the file is not found in the files app during UI testing. however, i managed to use application's launch arguments to pass the url of the file to be copied and also the custom name when copied to the destination. by using this, the file is now visible in the files app. – mownier Oct 29 '19 at 23:20
1 Answers
6
This is possible by using the XCUIApplication's launchArguments. It needs to include the following keys in the plist of the App: LSSupportsOpeningDocumentsInPlace
and UIFileSharingEnabled
or UISupportsDocumentBrowser
.
// File: FileUploadUITests.swift
// Target: UITests
func launchApplication() {
let fileName = "__File_12345678910.txt"
let app = XCUIApplication()
app.launchArguments.append("-fileUrlPath")
app.launchArguments.append(sampleTextFileURL().path)
app.launchArguments.append("-fileName")
app.launchArguments.append(fileName)
app.launch()
}
func sampleTextFileURL() -> URL {
let bundle = Bundle(for: FileUploadUITests.self)
return bundle.url(forResource: "text_file_example", withExtension: "txt")!
}
// File: TestHelper.swift
// Target: App
@discardableResult
func processArgumentsForTesting() -> Bool {
if let index = ProcessInfo.processInfo.arguments.index(where: { $0 == "-fileUrlPath" }) {
let path = ProcessInfo.processInfo.arguments[index + 1]
let url = URL(string: path)!
let fileName: String?
if let index = ProcessInfo.processInfo.arguments.index(where: { $0 == "-fileName" }) {
fileName = ProcessInfo.processInfo.arguments[index + 1]
} else {
fileName = nil
}
copyTestFileToDocumentDirectory(url: url, fileName: fileName)
return true
}
return false
}
private let fileManager = FileManager.default
private var documentDirectoryURLOfTheApp: URL {
let paths = fileManager.urls(for: .documentDirectory, in: .allDomainsMask)
let documentDirectoryPath = paths.first!
return documentDirectoryPath
}
@discardableResult
private func copyTestFileToDocumentDirectory(url: URL, fileName: String? = nil) -> Bool {
let directory = documentDirectoryURLOfTheApp
let destination = directory.appendingPathComponent(fileName ?? url.lastPathComponent).path
let isOkay: Bool
do {
if fileManager.fileExists(atPath: destination) {
try fileManager.removeItem(atPath: destination)
}
try fileManager.copyItem(atPath: url.path, toPath: destination)
isOkay = true
} catch {
isOkay = false
}
return isOkay
}
// File: AppDelegate.swift
// Target: App
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
processArgumentsForTesting()
return true
}

mownier
- 1,719
- 2
- 13
- 27
-
Do you know how to place a file in the doc folder in macOS for a sandboxed app? – Sentry.co Apr 07 '23 at 11:38