关于ios使用Automation自动测试

最近看了一篇英文文章觉得写的非常好,所以根据本人所学总结过来,不过英文水平有限,只能半猜半测试的弄过来,先说明本人主攻技术,所以英文如果翻译有错,请多担待。

自动测试可以测试性能等,学习之前,先讲讲需要用到的工具:xcode需要4.0以上,不过4.5以上commond-line可以截图和使用脚本。

从xcode4.0以后就增加了一个UIAutomation框架,既可以用在模拟器也能用在真机上。有文档可以参看参考资料。

首先,如果不想自己写例子就下载一个参考资料1中的例子吧,先在模拟器上使用吧。使用Product > Profile命令启动,可看到instruments启动了,这时选择profile,


进入 了Automation,先点击红色按钮停止。


这个时候可以写script了,也可以import一个js文件,这两种都可以通过左侧的scripts里面的add按钮进行操作:


这里就懒得create了,直接import一个例子中写好的js文件吧。


将右侧的显示区设置为script区:


可以看到里面的内容变成了:

var testName = "Test 1";

var target = UIATarget.localTarget();

var app = target.frontMostApp();

var window = app.mainWindow();

//--

UIALogger.logStart( testName );

app.logElementTree();


//-- select the elements

UIALogger.logMessage( "Select the first tab" );

var tabBar = app.tabBar();

var selectedTabName = tabBar.selectedButton().name();

if (selectedTabName != "First") {

tabBar.buttons()["First"].tap();

}

//-- tap on the text fiels

UIALogger.logMessage( "Tap on the text field now" );


var recipeName = "Unusually Long Name for a Recipe";

window.textFields()[0].setValue(recipeName);


target.delay( 2 );


//-- tap on the text fiels

UIALogger.logMessage( "Dismiss the keyboard" );


app.logElementTree();


app.keyboard().buttons()["Return"].tap();


var textValue = window.staticTexts()["RecipeName"].value();


UIALogger.logMessage( "text value=" + textValue  );

if (textValue === recipeName){

UIALogger.logPass( testName ); 

}

else{

UIALogger.logFail( testName ); 

}

点击红色按钮运行一下吧,可以看到一系列的动作出现了,都是自动的哦。下面就来进行详细的分析一下吧。不过今天不太想写了,所以还是明天来吧。这些代码很简单,一看基本就明白了。不过明天还是会进行详细解说的。


参考资料:

1、iOS Automated Tests with UIAutomation

2、UI Automation JavaScript Reference

3、About Instruments

4、wwdc

猜你喜欢

转载自blog.csdn.net/rsp19801226/article/details/12160601