1

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()

`

Dan Leonard
  • 3,325
  • 1
  • 20
  • 32
  • put a break point just before the line that's ambiguous and and run `po app.debugDescription` in the Console, it'll print the accessibility hierarchy of the UI. Should help figuring out what the test host is seeing. – ishaq Feb 09 '16 at 17:53
  • Didn't reveal in there either. – Dan Leonard Feb 09 '16 at 18:45
  • This Solved my problem but does not work on 6S+ http://stackoverflow.com/questions/33422681/xcode-ui-test-ui-testing-failure-failed-to-scroll-to-visible-by-ax-action/33534187#33534187 – Dan Leonard Feb 09 '16 at 19:38

2 Answers2

1

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.

Dan Leonard
  • 3,325
  • 1
  • 20
  • 32
0
/*Sends a tap event to a hittable/unhittable element.*/
    extension XCUIElement {
        func forceTapElement() {
            if self.isHittable {
                self.tap()
            }
            else {
                let coordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0))
                coordinate.tap()
            }
        }
    }

Swift 4 Version.

julian.a
  • 1,163
  • 2
  • 9
  • 21