iosxcodeios-ui-automationswift3.2

Issue facing for xcode-ui-testing in XCode 9


UI Test for an ios-app, developed in XCode 8 and swift 3.2.

Facing problem to deal with ScrollViews, collectionViews after upgrade XCode to 9

I can tap and access the Buttons, StaticTexts, TextFields elements. But I can not tap or access the collectionviews, scrollviews, tableviews elements on XCode9 and Swift 3.2.

Suppose in the previous XCode version (i.e, XCode 8.3) I used the code app.collectionViews.collectionViews.cells.images.element(boundBy: 0).tap()
to tap on Home page(collectionViews). But this code is not working in XCode 9.

I tried to get the exact element using uitest recording feature. By using recording I got the code -

app.collectionViews.otherElements.containing(.textField, identifier:"StoryboardTitleTextField").children(matching: .collectionView).element.tap().

But this code isn't working too. So how can I resolve this?

Thanks


Solution

  • Recently i also faced the similar type of issue after upgrading my project(Swift 3.2) into XCode 9.

    The issue is

     app.collectionViews.collectionViews.cells.images.element(boundBy: 0).isHittable is returning false. 
    

    Thats why default tap() method is not working for hierarchical elements.

    I just did the below procedure and that worked fine.

    Step 1: Create an extension like below

    extension XCUIElement {
    func tryClick() {
        if self.isHittable {
            self.tap()
        }
        else {
            let coordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5))
            coordinate.doubleTap()
            //coordinate.press(forDuration: 0.5)
        }
      }
    }
    

    Step 2: click on the element using the instance method created above instead of using direct tap() method.

    app.collectionViews.collectionViews.cells.images.element(boundBy: 0).tryClick()
    

    Note: try using

    CGVector(dx:0.5, dy:0.5) or CGVector(dx:0.0, dy:0.0)
    

    Hope it will solve your issue. It worked like a charm for me.