UI 自动化警报提示按钮/文本字段可访问性

UI Automation AlertPrompt button/textField accessiblity

本文关键字:字段 文本 访问 按钮 自动化 提示 UI      更新时间:2023-09-26

当涉及到alertView时,我在UI Automation(内置于iOS工具)方面遇到了一些麻烦。首先,我不确定在哪里可以为alertView上的按钮设置可访问性标签等。其次,虽然我没有收到错误,但我无法让我的 textField 实际将 textField 的值设置为某些值。我将为alertView和用于UI Automation的javaScript提供我的代码。

UIATarget.onAlert = function onAlert(alert)
{
    // Log alerts and bail, unless it's the one we want
    var title = alert.name();
    UIALogger.logMessage("Alert with title '" + title + "' encountered!");
    alert.logElementTree();
    if (title == "AlertPrompt")
    {
        UIALogger.logMessage(alert.textFields().length + '');
        target.delay(1);
        alert.textFields()["AlertText"].setValue("AutoTest");
        target.delay(1);
        return true; // Override default handler
    }
    else
        return false;
}

var target = UIATarget.localTarget();
var application = target.frontMostApp(); 
var mainWindow = application.mainWindow();
mainWindow.logElementTree();
//target.delay(1);
//mainWindow.logElementTree();
//target.delay(1);
var tableView = mainWindow.tableViews()[0];
var button = tableView.buttons();
//UIALogger.logMessage("Num buttons: " + button.length);
//UIALogger.logMessage("num Table views: " + mainWindow.tableViews().length);
//UIALogger.logMessage("Number of cells: " + tableView.cells().length);
/*for (var currentCellIndex = 0; currentCellIndex < tableView.cells().length; currentCellIndex++)
{
    var currentCell = tableView.cells()[currentCellIndex];
    UIALogger.logStart("Testing table option: " + currentCell.name());
    tableView.scrollToElementWithName(currentCell.name());
    target.delay(1);
    currentCell.tap();// Go down a level
    target.delay(1);
    UIATarget.localTarget().captureScreenWithName(currentCell.name());
    //mainWindow.navigationBar().leftButton().tap(); // Go back
    target.delay(1);
    UIALogger.logPass("Testing table option " + currentCell.name());
}*/
UIALogger.logStart("Testing add item");
target.delay(1);
mainWindow.navigationBar().buttons()["addButton"].tap();
target.delay(1);
if(tableView.cells().length == 5)
    UIALogger.logPass("Successfully added item to table");
else
    UIALogger.logFail("FAIL: didn't add item to table");

这是我用于警报视图的内容

#import "AlertPrompt.h"
@implementation AlertPrompt
@synthesize textField;
@synthesize enteredText;
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle withOrientation:(UIInterfaceOrientation) orientation
{
    if ((self == [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil]))
    {
        self.isAccessibilityElement = YES;
        self.accessibilityLabel = @"AlertPrompt";
        UITextField *theTextField;
        if(orientation == UIInterfaceOrientationPortrait)
            theTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
        else
            theTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 30.0, 260.0, 25.0)];
        [theTextField setBackgroundColor:[UIColor whiteColor]]; 
        [self addSubview:theTextField];
        self.textField = theTextField;
        self.textField.isAccessibilityElement = YES;
        self.textField.accessibilityLabel = @"AlertText";
        [theTextField release];
        CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 0.0); 
        [self setTransform:translate];
    }
    return self;
}
- (void)show
{
    [textField becomeFirstResponder];
    [super show];
}
- (NSString *)enteredText
{
    return [self.textField text];
}
- (void)dealloc
{
    //[textField release];
    [super dealloc];
}
@end

感谢您的任何帮助!

如果你想为视图分配辅助功能名称,你很可能应该尝试为整个视图实现可访问性方法(我不确定你在标题中为 AlertPrompt 类子类是什么)。

尝试为 AlertPrompt 实现添加以下方法:

- (BOOL)isAccessibilityElement {
    return YES;
}
- (NSString *)accessibilityLabel {  // will translate to accessibility .name() in UI javascript
    return [NSString stringWithString:@"AlertPrompt"];
}
- (NSString *)accessibilityValue { // will translate to accessibility .value() in UI javascript
    return [NSString stringWithString:@"AlertString"];
}

然后记录元素树,并确保您可以通过UI javascript中的.name()和.value()方法访问视图/警报可访问性属性。