UI 自动化警报提示按钮/文本字段可访问性
当涉及到alertView时,我在UI自动化(iOS内置工具)方面遇到了一些麻烦。首先,我不确定在哪里可以为alertView 上的按钮设置accessibilityLabel 等。其次,虽然我没有收到错误,但我无法让我的 textField 实际将 textField 的值设置为某个值。我将提供用于 UI 自动化的 AlertView 和 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
感谢您的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想为视图分配辅助功能名称,那么您很可能应该尝试为整个视图实现辅助功能方法(我不确定您在标头中为 AlertPrompt 类创建子类)。
尝试为 AlertPrompt 实现添加这些方法:
然后记录元素树并确保您可以通过 UI javascript 中的 .name() 和 .value() 方法访问视图/警报可访问性属性。
If you want to assign accessibility name to your view most probably you should try to implement accessibility methods for whole view (i'm not sure what you are subclassing for AlertPrompt class in header).
Try to add these methods for the AlertPrompt implementation:
Then log element tree and make sure you can access your view/alert accessibility properties via .name() and .value() methods in UI javascript.