当涉及到 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
谢谢你的帮助!