接着上一篇《如何使用UIAutomation进行iOS自动化测试(Part I)》
3、经验分享(让你生活变得更简单)
类库Tune-up介绍
现在你应该基本上知道如何编写测试代码了。但你慢慢地会发现,你会经常写到一些重复的,冗余的,黏糊糊的代码,像下面一样:
var target = UIATarget.localTarget(); var app = target.frontMostApp(); var window = app.mainWindow(); |
这也是为什么我们会用到一个小的Javascript类库来简化我们写的UIAutomation测试的原因。你可以去https://github.com/alexvollmer/tuneup_js获取这个类库,然后将它复制到你的测试目录下面。现在让我们使用Tune-Up类库来重新编写我们的Test1.js:
#import "tuneup/tuneup.js" test("Test 1", function(target, app) { var window = app.mainWindow(); 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(); assertEquals(recipeName, textValue); }); |
Tune-Up可以避免你编写重复的代码,同时还给你提供了各种好用的断言方法:
ssertTrue(expression, message), assertMatch(regExp, expression, message), assertEquals(expected, received, message), assertFalse(expression, message), assertNull(thingie, message), assertNotNull(thingie, message), assertNull(thingie, message), assertNotNull(thingie, message) |
等等
你也可以很容易的扩展这个类库:例如,你可以通过将方法加入到uiautomation-ext.js:里面来为UIATarget类加一个logDevice方法:
extend(UIATarget.prototype, { logDevice: function(){ UIALogger.logMessage("Dump Device:"); UIALogger.logMessage(" model: " + UIATarget.localTarget().model()); UIALogger.logMessage(" rect: " + JSON.stringify(UIATarget.localTarget().rect())); UIALogger.logMessage(" name: "+ UIATarget.localTarget().name()); UIALogger.logMessage(" systemName: "+ UIATarget.localTarget().systemName()); UIALogger.logMessage(" systemVersion: "+ UIATarget.localTarget().systemVersion()); } }); |