I have an addition to the answer of @deepak-terse:
You can use index instead of the string filename, so you don't depend on the simulator.
func selectFromCameraRoll(_ app: XCUIApplication, index: Int = 1) {
app.tables.cells.element(boundBy: 1).tap()
app.collectionViews.cells.element(boundBy: index).tap()
}
You can even randomize your selected photo using arc4random_uniform()
UPDATE
In case somebody finds this usable, here is the helper function for the Camera in the UI Testing I use when testing on real device:
func addPhotoCamera(_ app: XCUIApplication) {
let pleaseSelectSheet = app.sheets.element
// Take Picture button, it is first:
pleaseSelectSheet.buttons.element(boundBy: 0).tap()
// this monstrosity finds Capture button
let element = app
.children(matching: .window).element(boundBy: 0)
.children(matching: .other).element
.children(matching: .other).element
.children(matching: .other).element
.children(matching: .other).element
let photoCapture = element.children(matching: .other).element
.children(matching: .other).element(boundBy: 1)
.children(matching: .other).element
photoCapture.tap()
// I have slow computer, so I need this so test does not fail
sleep(5)
app.buttons["Use Photo"].tap()
}
UPDATE 2
On the device the Camera Roll above did not work because usually there are a lot of photos so tapping first that is not on the screen won't help. I ended up using the following snippet:
let photoCells = app.collectionViews.cells
if Platform.isSimulator {
photoCells.element(boundBy: index).tap()
} else {
photoCells.allElementsBoundByIndex.last!.firstMatch.tap()
}
where Platform.isSimulator
part is taken from https://stackoverflow.com/a/30284266/2875219:
import Foundation
struct Platform {
static var isSimulator: Bool {
return TARGET_OS_SIMULATOR != 0
}
}
And the whole piece of code together:
struct Platform {
static var isSimulator: Bool {
return TARGET_OS_SIMULATOR != 0
}
}
extension XCUIElement {
func tapIfExists() {
if exists {
tap()
}
}
}
// MARK: - Helper functions
extension XCTestCase {
func addPhotoCamera(_ app: XCUIApplication) {
let pleaseSelectSheet = app.sheets.element
// ["Take Picture"].tap()
pleaseSelectSheet.buttons.element(boundBy: 0).tap()
// use coordinates and tap on Take picture button
let element = app
.children(matching: .window).element(boundBy: 0)
.children(matching: .other).element
.children(matching: .other).element
.children(matching: .other).element
.children(matching: .other).element
let photoCapture = element.children(matching: .other).element
.children(matching: .other).element(boundBy: 1)
.children(matching: .other).element
photoCapture.tap()
sleep(5)
app.buttons["Use Photo"].tap()
}
func addPhotoLibrary(_ app: XCUIApplication, index: Int = 0) {
let pleaseSelectSheet = app.sheets["Add Photo"]
pleaseSelectSheet.buttons.element(boundBy: 1).tap()
sleep(10)
// Camera Roll
app.tables.cells.element(boundBy: 1).tap()
sleep(2)
let photoCells = app.collectionViews.cells
if Platform.isSimulator {
photoCells.element(boundBy: index).tap()
} else {
photoCells.allElementsBoundByIndex.last!.firstMatch.tap()
}
sleep(2)
app.buttons["Choose"].tapIfExists()
}
}