How to add an image for multiple locales using XCTest

An annoying thing I’ve had to deal with recently is adding an image (both where you take a photo with the camera and also from the gallery).

You might think that sounds pretty straight forward, but when you want to run your test in multiple locales then “Choose from library” and “Upload photo” etc just doesn’t quite cut it.

Attempts to record the steps I took within the gallery view also proved futile, as I could not actually record what I did there.

Not sure why I didn’t opt for waitForExpectation, and used a sleep though.

Here is how I went about adding images to my tests:

 func testAddPhoto() {
    let addPhotoButton = app.tabBars
                            .buttons[LocalizedString("add.photo.button")]
                            .firstMatch
    let photoLibraryButton = app
                                .staticTexts[LocalizedString("photo.library.button")]
                                .firstMatch
    let takePhotoButton = app
                            .staticTexts[LocalizedString("take.photo.button")]
                            .firstMatch

    addPhotoButton.tap()

    XCTAssertEqual(waiterResultWithExpectation(photoLibraryButton), .completed)
    XCTAssertTrue(photoLibraryButton.isEnabled)
    XCTAssertTrue(takePhotoButton.isEnabled)

    photoLibraryButton.tap()

    //here I am selecting the first image album
    app.cells.element(boundBy:0).tap()

    //opted for a sleep to give it time for the images to appear
    sleep(1)

    //selecting the most recent image
    app.cells.element(boundBy:0).tap()
 }