UItesting : Click on UIcollectionview then testing

2019-08-18 18:19发布

问题:

I am new at ui-testing. During recording, when I tap on ui-collection view first object is shown on UI and corresponding to code written in test-example method is:

XCUIElement *window = [[app childrenMatchingType:XCUIElementTypeWindow] elementBoundByIndex:0];
    XCUIElement *element2 = [[[[window childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element;
    [element2 tap]; 

And when automating the test-example method, it's unable to fetch first object of ui-collection view. please suggest a way to do that. Thanks in advance.

回答1:

Recording UITests tends to give you really long and ugly queries. I strongly suggest that you have a look on how to write UITests manually. It's really easy and the queries look much better.

For example: to tap on the first cell of your collection view all you need to do is this (providing that there is only one UICollectionView on the current screen):

Objective-C

- (void)testExample {
    XCUIApplication *app = [[XCUIApplication alloc] init];
    [app launch];

    [[app.collectionViews.cells elementBoundByIndex:0] tap];
}

Swift

func testExample() {
    let app = XCUIApplication()
    app.launch()

    // tap on the first collection view cell on the current screen
    app.collectionViews.cells.element(boundBy:0).tap()
}