gpt4 book ai didi

ios - 使用 XCUIElementQuery 查询 tableHeaderView 中的按钮未找到

转载 作者:行者123 更新时间:2023-11-29 11:58:40 25 4
gpt4 key购买 nike

我想使用 Xcode UI Test 来帮助我自动化测试我的应用程序。

我的 View 层次结构如:UITableView->TableHeaderView->我的自定义 View ->设置按钮。

我想用 accessibilityIdentifier 查询我的按钮,我将它设置在我的 viewController 中,并使用 Simulator 的可访问性检查器检查它,并使用我的真实设备通过语音检查按钮是否可访问。

但是用我的代码,它不能成立:


app.tables.buttons["settingBtn"].tap()

为什么我的设置按钮是无障碍的,但是Xcode无法记录,自己写代码找不到设置按钮?

有人可以给我一些提示,以正确的方式使用 UIElementQuery 来访问我的按钮吗?

--- 编辑---

在自定义 View 中:

@implementation HeaderView

- (void)awakeFromNib {
[super awakeFromNib];

[self initAccessibility];
}

- (BOOL)isAccessibilityElement {
return NO;
}

- (NSArray *)accessibilityElements {
return @[self.settingButton];
}

- (NSInteger)accessibilityElementCount {
return [[self accessibilityElements] count];
}

- (NSInteger)indexOfAccessibilityElement:(id)element {
return [[self accessibilityElements] indexOfObject:element];
}

- (void)initAccessibility {
self.settingButton.isAccessibilityElement = YES;
self.settingButton.accessibilityIdentifier = @"settingBtn";
self.settingButton.accessibilityLabel = @"settingBtn";
}

- (IBAction)clickSettingButton:(id)sender {
if ([self.delegate respondsToSelector:@selector(clickSettingButton)]) {
[self.delegate clickSettingButton];
}
}

@end

在 View Controller 中:

- (void)initHeaderView {
self.headerView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([HeaderView class]) owner:self options:nil] firstObject];
self.headerView.delegate = self;
[self.headerView.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:kURLString]];
[self.headerView setFrame:CGRectMake(0, 0, SCREENWIDTH, (SCREENWIDTH * 9 / 16))];
self.headerViewHeight = self.headerView.bounds.size.height;
[self.tableView addSubview:self.headerView];
self.tableView.contentInset = UIEdgeInsetsMake(self.headerViewHeight, 0, 0, 0);
self.tableView.contentOffset = CGPointMake(0, -self.headerViewHeight + kNavigationBarHeight);
}

在 UI 测试代码中:

class PopupDialogUITests: XCTestCase {

override func setUp() {
super.setUp()

continueAfterFailure = false
XCUIApplication().launch()
}

override func tearDown() {
super.tearDown()
}

func testExample() {
let app = XCUIApplication()

app.tables.buttons["SettingBtn"].tap()
}

}

最佳答案

我知道你问这个问题已经一个多月了,但我注意到你仍然没有解决你的问题。在测试中指定可访问性标识符时,您在字符串的开头使用了大写字母“S”,而它应该是小写字母。

- (void)initAccessibility {
self.settingButton.isAccessibilityElement = YES;
self.settingButton.accessibilityIdentifier = @"settingBtn";
self.settingButton.accessibilityLabel = @"settingBtn";
}

app.tables.buttons["SettingBtn"].tap()

关于ios - 使用 XCUIElementQuery 查询 tableHeaderView 中的按钮未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38008917/

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