gpt4 book ai didi

ios - 最后在 XCUI TestCase 期间停止在 UITableView 上滚动

转载 作者:可可西里 更新时间:2023-11-01 04:24:19 28 4
gpt4 key购买 nike

在我的 UITableView 中有超过 10 行。我想在 UITestCase 运行时滚动到最后一行。

我在下面编写了滚动到最后一行的代码。

-(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;
}
}

我已经通过下面的代码在我的 UITestCase 方法之一中调用了上面的方法

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];

通过这段代码,我可以滚动到最后一行,但在到达最后一行后,它继续滚动意味着无法停止滚动。

请帮我滚动到最后一行,然后它应该停止滚动以便我可以执行下一个 Action 事件。

我引用了 StackOverFlow 答案,但没有一个符合我的要求。

提前致谢。

最佳答案

我在我的一个项目中遇到过类似的问题。因为我想通过 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's frame and window's 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 滚动到底部,这样我就可以加载更多单元格(在你的情况下是最后一个单元格)。然后我执行操作 Tap 从服务器获取新记录。然后我再次滚动表格以验证是否已从服务器获取新记录。

提示:您可以将递归函数替换为do whilewhile 循环来实现同样的效果。

希望这对您有所帮助!

编码愉快!!

关于ios - 最后在 XCUI TestCase 期间停止在 UITableView 上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48418858/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com