Gary Joy
Gary Joy

Reputation: 103

Can you simulate opening a file on a device in an XCUI test?

My iOS application is registered to handle certain file types and I want to include coverage for this in my automated tests.

Is there any way to simulate a file opening (e.g. after receiving a shared file or opening an attachment from an e-mail) in an XCUI test?

My "Plan B" is to host the files somewhere and then try and "open" them (in my tests) via Safari...

Upvotes: 0

Views: 317

Answers (1)

Gary Joy
Gary Joy

Reputation: 103

I went with Plan B. Here's my code...

func testForStackOverflow() {

    let myApplication = XCUIApplication()
    myApplication.launch()
    _ = myApplication.wait(for: .runningForeground, timeout: 10)
    
    let safari = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")
    safari.launch()
    _ = safari.wait(for: .runningForeground, timeout: 10)

    let fixturesUrl = "https://server.com/fixtures.zip"

    // This works with the "weird" address bar at the bottom in iOS 15.2
    safari.textFields["TabBarItemTitle"].tap()
    safari.textFields["URL"].typeText(fixturesUrl)
    safari.buttons["Go"].tap()
    safari.buttons["Download"].tap()

    let files = XCUIApplication(bundleIdentifier: "com.apple.DocumentsApp")
    files.launch()
    _ = files.wait(for: .runningForeground, timeout: 10)

    // This will only work in "List View" (not "Icon View")
    files.staticTexts["Downloads"].tap()
    
    files.staticTexts["fixtures.zip"].tap()
    files.staticTexts["fixtures"].tap()
    files.staticTexts["file.ext"].tap()
            
    myApplication.buttons["Done"].tap()
    
    // Cleanup
    files.activate()
    files.buttons["Downloads"].tap()
    files.staticTexts["fixtures"].press(forDuration: 2)
    files.buttons["Delete"].tap()

    files.staticTexts["fixtures.zip"].press(forDuration: 2)
    files.buttons["Delete"].tap()

    files.buttons["On My iPhone"].tap()
    
}

Upvotes: 1

Related Questions