(iOS) UI Automation AlertPrompt button/textField accessiblity

Posted by lipd on Stack Overflow See other posts from Stack Overflow or by lipd
Published on 2011-05-11T20:20:52Z Indexed on 2012/09/27 9:37 UTC
Read the original article Hit count: 162

I'm having a bit of trouble with UI Automation (the built in to iOS tool) when it comes to alertView. First off, I'm not sure where I can set the accessibilityLabel and such for the buttons that are on the alertView. Secondly, although I am not getting an error, I can't get my textField to actually set the value of the textField to something. I'll put up my code for the alertView and the javaScript I am using for UI Automation.

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");

Here's what I'm using for the 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

Thanks for any help!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about iphone