Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否可以将UITests目标中的文件复制到应用程序的documents目录?_Ios_Swift_Xctest_Xcuitest - Fatal编程技术网

Ios 是否可以将UITests目标中的文件复制到应用程序的documents目录?

Ios 是否可以将UITests目标中的文件复制到应用程序的documents目录?,ios,swift,xctest,xcuitest,Ios,Swift,Xctest,Xcuitest,我的UITests目标中有一个示例文本文件。我想将此文件复制到应用程序的documents目录中,这样当我在应用程序中执行文件上载测试时,我可以通过文件应用程序选择并上载它。这可以通过使用XCUI应用程序的launchArguments实现。它需要在应用程序的plist中包含以下键:LSSupportsOpeningDocumentsInPlace和UIFileSharingEnabled或UISupportsDocumentBrowser // File: FileUploadUITests.

我的UITests目标中有一个示例文本文件。我想将此文件复制到应用程序的documents目录中,这样当我在应用程序中执行文件上载测试时,我可以通过文件应用程序选择并上载它。

这可以通过使用XCUI应用程序的launchArguments实现。它需要在应用程序的plist中包含以下键:
LSSupportsOpeningDocumentsInPlace
UIFileSharingEnabled
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
}

为什么要投否决票?请解释为什么我没有投反对票,但你试过什么?除了在测试文件中复制文件外,这与在应用程序中复制文件没有太大区别。或者,你可以在应用程序中进行检查,查看测试是否正在运行,如果正在运行,请复制该文件。请向上投票。我只是尝试将捆绑包中的文件复制到应用程序的documents目录。但是当我查看文件应用程序时,文件不在这里。请注意,我正在进行一些ui测试。您是否已更新信息列表,以包括
LSSupportsOpeningDocumentsInPlace
UIFileSharingEnabled
UISupportsDocumentBrowser
?()此外,如果您想要更新投票,您应该更新您的问题,提供更多详细信息,说明您尝试了什么,并提供重现问题的信息:。我会检查一下。非常感谢。