Stop scrolling on UITableView, finally during XCUI TestCase

In my one there UITableViewwill be more than 10 lines. I want to scroll to the last line while it UITestCaseworks.

I wrote below code to scroll to the last line.

-(void)scrollToElement:(XCUIElement *)element application:(XCUIApplication *)app{
    while ([self visible:element withApplication:app]) {
        XCUIElement *searchResultTableView = app.tables[@"searchResultView"];
        XCUICoordinate *startCoord = [searchResultTableView coordinateWithNormalizedOffset:CGVectorMake(0.5, 0.5)];
        XCUICoordinate *endCoord = [startCoord coordinateWithOffset:CGVectorMake(0.0, -262)];
        [startCoord pressForDuration:0.01 thenDragToCoordinate:endCoord];
    }
}
-(BOOL)visible:(XCUIElement *)element withApplication:(XCUIApplication *)app{
    if (element.exists && !CGRectIsEmpty(element.frame) && element.isHittable) {
        return CGRectContainsRect([app.windows elementBoundByIndex:0].frame, element.frame);
    } else {
        return FALSE;
    }
}

An I called the method above in my one of the methods UITestCasein the code below

XCUIElement *searchResultTableView = app.tables[@"searchResultView"];
[self waitForElementToAppear:searchResultTableView withTimeout:30];


XCUIElement *table = [app.tables elementBoundByIndex:0];
XCUIElement *lastCell = [table.cells elementBoundByIndex:table.cells.count - 1];
[self scrollToElement:lastCell application:app];

By this code, I can scroll to the last line, but after reaching the last line, he continues to do so that the scroll tools cannot stop scrolling.

Please help me scroll only the last line, and then it should stop scrolling so that I can perform the following action.

I answer StackOverFlow, but not one of them meets my requirements.

Thanks in advance.

+4
1

. " " TestKit.

.

//app           : is your current instance of appliaction
//listTable     : is a Table which you've found via accessibility identifier
//loadMoreTest  : is a parameter to determine whether code should perform test for loadmore feature or not

- (void)testScrollableTableForApplication:(XCUIApplication *)app
                                 forTable:(XCUIElement *)listTable
                         withLoadMoreTest:(BOOL)loadMoreTest {

    [listTable accessibilityScroll:UIAccessibilityScrollDirectionUp];
    [listTable swipeUp];

    if (loadMoreTest) {
        __block BOOL isLoadMoreCalled;
        __block XCUIElement *lastCell;
        __block __weak void (^load_more)();
        void (^loadMoreCall)();
        load_more = loadMoreCall = ^() {
            XCUIElementQuery *tablesQuery = app.tables;
            XCUIElementQuery *cellQuery = [tablesQuery.cells containingType:XCUIElementTypeCell identifier:@"LoadMoreCell"];
            lastCell = cellQuery.element;
            if ([lastCell elementIsWithinWindowForApplication:app]) {
                [self waitForElementToAppear:lastCell withTimeout:2];
                [lastCell tap];
                isLoadMoreCalled = true;
                [self wait:2];
            }
            [listTable swipeUp];
            if (!isLoadMoreCalled) {
                load_more();
            }
        };
        loadMoreCall();
    }
}


- (void)waitForElementToAppear:(XCUIElement *)element withTimeout:(NSTimeInterval)timeout
{
    NSUInteger line = __LINE__;
    NSString *file = [NSString stringWithUTF8String:__FILE__];

    NSPredicate *existsPredicate = [NSPredicate predicateWithFormat:@"exists == 1"];
    [self expectationForPredicate:existsPredicate evaluatedWithObject:element handler:nil];

    [self waitForExpectationsWithTimeout:timeout handler:^(NSError * _Nullable error) {
        if (error != nil) {
            NSString *message = [NSString stringWithFormat:@"Failed to find %@ after %f seconds",element,timeout];
            [self recordFailureWithDescription:message inFile:file atLine:line expected:YES];
        }
    }];
}

XCUIElement XCUIElement + Helper.m .

#import <XCTest/XCTest.h>

@interface XCUIElement (Helper)

/// Check whether current XCUIElement is within current window or not
- (BOOL)elementIsWithinWindowForApplication:(XCUIApplication *)app ;

@end

@implementation XCUIElement (Helper)

/// Check whether current XCUIElement is within current window or not
/*
@description: we need to check particular element frame and window frame is intersecting or not, to get perfectly outcome whether element is currently visible on screen or not, because if element has not yet appeared on screen then also the flag frame, exists and hittable can become true
*/
- (BOOL)elementIsWithinWindowForApplication:(XCUIApplication *)app {
    if (self.exists && !CGRectIsEmpty(self.frame) && self.hittable)
        return CGRectContainsRect(app.windows.allElementsBoundByIndex[0].frame, self.frame);
    else
        return false;
}

@end

" ",

cell.accessibilityIdentifier = @"LoadMoreCell";

- testScrollableTableForApplication, Tableview , ( ). , . , , .

: while while, .

, !

!!

+8

Source: https://habr.com/ru/post/1692622/


All Articles