OGeek|极客世界-中国程序员成长平台

标题: ios - 从 js 到 Objective-C 的谓词 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:36
标题: ios - 从 js 到 Objective-C 的谓词

我有一个 .js 文件,用于使用 Instruments.app 进行屏幕截图自动化,我在其中查找具有以下谓词的单元格:

var classScheduleCell = classScheduleTableView.cells().firstWithPredicate("isEnabled == 1 && NONE staticTexts.value BEGINSWITH 'No classes'").withValueForKey(1, "isVisible");

我想将该谓词转换为客观的 C UI 测试,因为我用于屏幕截图的 ruby​​ 脚本现在使用 UI 测试而不是 Instruments。使用相同的谓词失败

XCUIElement *firstCell = [classScheduleTableView.cells elementMatchingPredicate:[NSPredicate predicateWithFormat"isEnabled == 1 && NONE staticTexts.value BEGINSWITH 'No classes'"]];

看起来我可以改变谓词的第一部分

isEnabled == 1

enabled == true

关于如何使另一部分工作的任何想法?



Best Answer-推荐答案


我找到了一个解决方案,虽然不是最优雅的。我找不到让谓词像我在 UI 自动化中那样工作的方法,所以我使用了几个 for 循环来检查单元格标签的值。

NSPredicate *enabledCellsPredicate = [NSPredicate predicateWithFormat"enabled == true "];
XCUIElementQuery *enabledCellsQuery = [classScheduleTableView.cells matchingPredicate:enabledCellsPredicate];
int cellCount = enabledCellsQuery.count;
for (int i = 0; i < cellCount; i++) {
    XCUIElement *cellElement = [enabledCellsQuery elementBoundByIndex:i];
    XCUIElementQuery *cellStaticTextsQuery = cellElement.staticTexts;
    int textCount = cellStaticTextsQuery.count;
    BOOL foundNoClasses = NO;
    for (int j = 0; j < textCount; j++) {
        XCUIElement *textElement = [cellStaticTextsQuery elementBoundByIndex:j];
        if (textElement.value && [textElement.value rangeOfString:NSLocalizedString(@"No classes", nil) options:NSCaseInsensitiveSearch].location != NSNotFound) {
            foundNoClasses = YES;
            break;
        }
    }
    if (foundNoClasses == NO) {
        [cellElement tap];
        break;
    }
}

感谢@joe-masilotti 的帮助。

关于ios - 从 js 到 Objective-C 的谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33349909/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4