Guys this is method for UICollectionView which works for me.
+ (id) stepToClickCollectionView{
return [KIFTestStep stepWithDescription:@"Collection Click step" executionBlock:^KIFTestStepResult(KIFTestStep *step, NSError **error) {
NSString *collectionViewSection = @"MyCollection";
UIAccessibilityElement *element = [[UIApplication sharedApplication] accessibilityElementWithLabel:collectionViewSection];
KIFTestCondition(element, error, @"View with label %@ not found", collectionViewSection);
UICollectionView *collectionView = (UICollectionView*)[UIAccessibilityElement viewContainingAccessibilityElement:element];
KIFTestCondition([collectionView isKindOfClass:[UICollectionView class]], error, @"Specified view is not a UICollectionView");
KIFTestCondition(collectionView, error, @"UICollection view with label %@ not found", collectionViewSection);
NSInteger numberOfItems = [collectionView.dataSource collectionView:collectionView numberOfItemsInSection:0];
NSLog(@"Count %d",numberOfItems);
if (numberOfItems > 0) {
int randomClicks = [TestUtilites generateRandomIndex:numberOfItems];
NSLog(@"-------- random click %d",randomClicks);
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:randomClicks inSection:0];
UICollectionViewCell *collectiobViewCell = [collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
if (!collectiobViewCell) {
// KIFTestCondition( [[collectionView numberOfSections]== 0], error, @"Section is not found in UICollection view",[collectionView numberOfSections]);
// KIFTestCondition( [collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]], error, @"Row is not found in section of UICollectionView view", );
}
[collectionView tappablePointInRect:collectiobViewCell.frame];
collectiobViewCell = [collectionView cellForItemAtIndexPath:indexPath];
if (!collectiobViewCell) {
KIFTestCondition([indexPath section] < [collectionView numberOfSections], error, @"Section %d is not found in '%@' Collection view", [indexPath section], collectionViewSection);
KIFTestCondition([indexPath row] < [collectionView numberOfItemsInSection:[indexPath section]], error, @"Row %d is not found in section %d of '%@' Collection view", [indexPath row], [indexPath section], collectionViewSection);
[collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
collectiobViewCell = [collectionView cellForItemAtIndexPath:indexPath];
}
KIFTestCondition(collectiobViewCell, error, @"Collection view cell at index path %@ not found", indexPath);
CGRect cellFrame = [collectiobViewCell.contentView convertRect:[collectiobViewCell.contentView frame] toView:collectionView];
[collectionView tapAtPoint:CGPointCenteredInRect(cellFrame)];
}
return KIFTestStepResultSuccess;
}];
}