Ios 如何使用Xcode UI自动化测试在警报文本字段中输入文本
这个问题真的困扰着我。阅读了官方文件、乔纳森·潘的书以及我能找到的所有在线教程。我已经构建了一个非常简单的应用程序来学习UI测试,但在第一步就遇到了难题。这是一个待办事项列表应用程序。我单击UIBarButtonim按钮,它显示一个对话框,其中有一个UITextField和两个按钮,OK和Cancel。这是IBActionIos 如何使用Xcode UI自动化测试在警报文本字段中输入文本,ios,xcode,testing,uialertview,ios-ui-automation,Ios,Xcode,Testing,Uialertview,Ios Ui Automation,这个问题真的困扰着我。阅读了官方文件、乔纳森·潘的书以及我能找到的所有在线教程。我已经构建了一个非常简单的应用程序来学习UI测试,但在第一步就遇到了难题。这是一个待办事项列表应用程序。我单击UIBarButtonim按钮,它显示一个对话框,其中有一个UITextField和两个按钮,OK和Cancel。这是IBAction @IBAction func showDialog(sender: UIBarButtonItem) { println("showDialog") var
@IBAction func showDialog(sender: UIBarButtonItem) {
println("showDialog")
var inputTextField: UITextField?
var alert:UIAlertController
alert = UIAlertController(title: "New Item", message: "Type item below", preferredStyle: UIAlertControllerStyle.Alert)
alert.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
textField.placeholder = "Item name" // need to choose correct keyboard and capitalise first letter of each word.
inputTextField = textField
})
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
if let itemName = inputTextField?.text {
println(itemName)
self.items.append(itemName)
self.tableView.reloadData()
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
我曾尝试通过录制然后添加警报处理程序来编写UI自动化测试,但它不起作用。这是我的测试脚本
var target = UIATarget.localTarget();
UIALogger.logWarning('script started');
target.frontMostApp().navigationBar().rightButton().tap();
UIATarget.onAlert = function onAlert(alert) {
var title = alert.name();
UIALogger.logWarning("Alert with title ’" + title + "’ encountered!");
target.frontMostApp().keyboard().typeString("Cheese");
alert.buttons()["OK"].tap();
return true;
}
我做错了什么?经过多次搜索,我找到了答案。如果我们想与警报交互,onAlert函数只返回true;如果我们只想解除警报,则返回false
var target = UIATarget.localTarget();
var app = target.frontMostApp();
var window = app.mainWindow();
window.navigationBar().rightButton().tap();
UIATarget.onAlert = function() {
return true;
}
app.keyboard().typeString("Cheese");
app.alert().buttons()["OK"].tap();