0

当涉及到 alertView 时,我在 UI 自动化(iOS 内置工具)方面遇到了一些麻烦。首先,我不确定在哪里可以为 alertView 上的按钮设置accessibilityLabel 等。其次,虽然我没有收到错误,但我无法让我的 textField 将 textField 的值实际设置为某个值。我将为 alertView 和我用于 UI 自动化的 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");

这是我用于 alertView 的内容

#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

谢谢你的帮助!

4

1 回答 1

0

如果您想为您的视图分配可访问性名称,您很可能应该尝试为整个视图实现可访问性方法(我不确定您在标题中为 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() 方法访问您的视图/警报可访问性属性。

于 2011-05-12T10:30:48.477 回答