3

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

Ali Azam
  • 2,047
  • 1
  • 16
  • 25

1 Answers1

2

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.

Mahmud Riad
  • 1,169
  • 1
  • 8
  • 19
  • Thanks mahmud. Excellent job you have done. It is also working for me. Can you tell me what does `CGVector(dx:0.5, dy:0.5)` do here in the extension method. – Ali Azam Nov 27 '17 at 15:18
  • @AliAzam It is the co-ordinate x , y location you want to tap on that element. Please let me know if it solved your issue or not. – Mahmud Riad Nov 28 '17 at 07:07