Skip to content

Commit d71c257

Browse files
committed
Merge pull request #414 from danydev/waitForCellImproved
Let 'waitForCellAtIndexPath' actually wait for a cell at a given index p...
2 parents 9eeb458 + 6d13605 commit d71c257

1 file changed

Lines changed: 33 additions & 31 deletions

File tree

Classes/KIFUITestActor.m

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -732,37 +732,34 @@ - (UITableViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inTableView
732732
if (![tableView isKindOfClass:[UITableView class]]) {
733733
[self failWithError:[NSError KIFErrorWithFormat:@"View is not a table view"] stopTest:YES];
734734
}
735-
736-
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
737-
735+
738736
// If section < 0, search from the end of the table.
739737
if (indexPath.section < 0) {
740738
indexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:tableView.numberOfSections + indexPath.section];
741739
}
742-
740+
743741
// If row < 0, search from the end of the section.
744742
if (indexPath.row < 0) {
745743
indexPath = [NSIndexPath indexPathForRow:[tableView numberOfRowsInSection:indexPath.section] + indexPath.row inSection:indexPath.section];
746744
}
747-
748-
if (!cell) {
749-
if (indexPath.section >= tableView.numberOfSections) {
750-
[self failWithError:[NSError KIFErrorWithFormat:@"Section %d is not found in table view", (int)indexPath.section] stopTest:YES];
751-
}
752-
753-
if (indexPath.row >= [tableView numberOfRowsInSection:indexPath.section]) {
754-
[self failWithError:[NSError KIFErrorWithFormat:@"Row %d is not found in section %d of table view", (int)indexPath.row, (int)indexPath.section] stopTest:YES];
755-
}
756-
757-
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
758-
[self waitForTimeInterval:0.5];
759-
cell = [tableView cellForRowAtIndexPath:indexPath];
760-
}
761-
745+
746+
[self runBlock:^KIFTestStepResult(NSError **error) {
747+
748+
KIFTestWaitCondition(indexPath.section < tableView.numberOfSections, error, @"Section %ld is not found in table view", (long)indexPath.section);
749+
750+
KIFTestWaitCondition(indexPath.row < [tableView numberOfRowsInSection:indexPath.section], error, @"Row %ld is not found in section %ld of table view", (long)indexPath.row, (long)indexPath.section);
751+
752+
return KIFTestStepResultSuccess;
753+
}];
754+
755+
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
756+
[self waitForTimeInterval:0.5];
757+
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
758+
762759
if (!cell) {
763760
[self failWithError:[NSError KIFErrorWithFormat: @"Table view cell at index path %@ not found", indexPath] stopTest:YES];
764761
}
765-
762+
766763
return cell;
767764
}
768765

@@ -791,23 +788,28 @@ - (UICollectionViewCell *)waitForCellAtIndexPath:(NSIndexPath *)indexPath inColl
791788
if (item < 0) {
792789
item += [collectionView numberOfItemsInSection:section];
793790
}
794-
795-
if (section >= collectionView.numberOfSections) {
796-
[self failWithError:[NSError KIFErrorWithFormat:@"Section %d is not found in collection view", (int)section] stopTest:YES];
797-
}
798-
799-
if (item >= [collectionView numberOfItemsInSection:section]) {
800-
[self failWithError:[NSError KIFErrorWithFormat:@"Item %d is not found in section %d of collection view", (int)item, (int)section] stopTest:YES];
801-
}
802-
791+
803792
indexPath = [NSIndexPath indexPathForItem:item inSection:section];
804-
[collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally | UICollectionViewScrollPositionCenteredVertically animated:YES];
793+
794+
[self runBlock:^KIFTestStepResult(NSError **error) {
795+
796+
KIFTestWaitCondition(indexPath.section < collectionView.numberOfSections, error, @"Section %ld is not found in collection view", (long)indexPath.section);
797+
798+
KIFTestWaitCondition(indexPath.row < [collectionView numberOfItemsInSection:indexPath.section], error, @"Item %ld is not found in section %ld of collection view", (long)indexPath.row, (long)indexPath.section);
799+
800+
return KIFTestStepResultSuccess;
801+
}];
802+
803+
[collectionView scrollToItemAtIndexPath:indexPath
804+
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally | UICollectionViewScrollPositionCenteredVertically
805+
animated:YES];
805806
[self waitForTimeInterval:0.5];
806807
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
807-
808+
808809
if (!cell) {
809810
[self failWithError:[NSError KIFErrorWithFormat: @"Collection view cell at index path %@ not found", indexPath] stopTest:YES];
810811
}
812+
811813
return cell;
812814
}
813815

0 commit comments

Comments
 (0)