Search code examples
swifttestingautomationios9

Testing UIImagePicker


I am trying to test a UiImagePicker I have used the record feature on Xcode to get most of my test but it fails to capture an element for confirming the picture that I want to select. Same thing happens with the other option "Which is cancel" I recall some way of getting a list of all the elements on a view or something to that effect. So my question how do I get a reference to an option in a ImagePicker object in a view.

I am building for iOS9 and running Xcode7.2

my current test looks like this ` func testMenu(){ loginInApp(app) //Gets you past the login

    app.buttons["LogoButton"].tap() //entry point for menuView

    XCTAssert(app.buttons["BR"].exists)
    let brButton = app.buttons["BR"]
    brButton.tap()

    let photosFromSheet = app.sheets["Where would you like to get photos from?"]
    XCTAssert(photosFromSheet.exists)
    photosFromSheet.staticTexts["Where would you like to get photos from?"].tap()
     XCTAssert(photosFromSheet.collectionViews.buttons["Chose from Library"].exists && photosFromSheet.buttons["Cancel"].exists)
    photosFromSheet.collectionViews.buttons["Chose from Library"].tap()
     XCTAssert(app.tables/*@START_MENU_TOKEN@*/.buttons["Moments"]/*[[".cells[\"Moments\"].buttons[\"Moments\"]",".buttons[\"Moments\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.exists)
    app.tables/*@START_MENU_TOKEN@*/.buttons["Moments"]/*[[".cells[\"Moments\"].buttons[\"Moments\"]",".buttons[\"Moments\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.tap()
    XCTAssert(app.collectionViews.cells["Photo, Landscape, March 12, 2011, 4:17 PM"].exists)
    app.collectionViews.cells["Photo, Landscape, March 12, 2011, 4:17 PM"].tap()

XCTAssert(app.childrenMatchingType(.Window).elementBoundByIndex(0).childrenMatchingType(.Other).elementBoundByIndex(2).childrenMatchingType(.Other).element.exists)

// Here is where things get ambiguous and do not actually select anything when the tests run.

app.childrenMatchingType(.Window).elementBoundByIndex(0).childrenMatchingType(.Other).elementBoundByIndex(2).childrenMatchingType(.Other).element.tap()


    brButton.tap()
    photosFromSheet.buttons["Cancel"].tap()
    app.buttons["LogoButton"].tap()

`


Solution

  • The UIImagePicker is apparently not hittable so this is the workaround

    If you see tap() in documentation it says

    /*!
     * Sends a tap event to a hittable point computed for the element.
     */
    - (void)tap;
    
    
    /*Sends a tap event to a hittable/unhittable element.*/
    extension XCUIElement {
        func forceTapElement() {
            if self.hittable {
                self.tap()
            }
            else {
                let coordinate: XCUICoordinate = self.coordinateWithNormalizedOffset(CGVectorMake(0.0, 0.0))
                coordinate.tap()
            }
        }
    }
    

    Now you can call as

    button.forceTapElement()
    

    This does not work on the 6S+ tho. Hopefully that will be resolved soon.