输出成JavaScript脚本是monkeytalk常用的工具。通常包括循环,控制语句,和随机数。
导出JavaScript脚本:生成MonkeyTalk脚本,点击“JavaScript”按钮,再点击“Export”按钮,MonkeyTalk将会生成.js文件,该文件名与.mt文件名相同。
执行Monkeytalk-js脚本,像执行普通的MonkeyTalk脚本一样:
Script Login.js Run
也可以不要文件扩展名,缺省扩展名,通常默认是运行.js脚本:
Script Login Run
一旦MonkeyTalk脚本导出成js脚本,用户就可以任意编辑。例如,有个脚本,在没有指定用户名和密码的时候,随机输入用户名和密码:
if (typeof Test == "undefined") { load("libs/Test.js"); };
Test.Login.prototype.run = function(usr, pwd) { usr = usr || randStr(); pwd = pwd || randStr();
this.app.input("username").enterText(usr); this.app.input("password").enterText(pwd); this.app.button("LOGIN").tap(); };
function randStr() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length));
return text; } |
每次保存.mt文件时,MonkeyTalk IDE都会生成一个js脚本的包装函数,以便可以直接准确的找到js脚本。这个生成的库文件,命名是YourProjectName.js,并且放在工程的libs目录下。该libs目录下同时也包含MonkeyTalk js API文件,名为MonkeyTalkAPI.js。
例如,下面的脚本:
# enterName.mt Vars name="foo" Input name EnterText ${name} Button OK Tap
保存enterName.mt时,一个js方法被生成,可以这样写:
this.app.enterName().run("bar")
或者
this.app.enterName().runWith("somedata.csv")
一个“call”方法也被生成,可以用来明确定义想要的参数(协助IDE完成命令),以便this.app.enterName().call("foo") 可以使用。
自定义命令同样产生包装。
// Call user.login.mt this.app.user("ethel").login("secret123");
MonkeyTalk可以不用Run或RunWith,就可以调用js。
用js编写MonkeyTalk脚本必须在开始位置加载工程的包装库。
load("libs/YourProjectName.js")
每个MonkeyTalk命令可以通过下面这种方式被js使用:
this.app.componentType("monkeyId").action(args...)
加载库创建的属性叫做this.app,这是一个在被测试之前引用的对象。this.app根据控件类型来提供方法。对每个控件类型(包括用户自定义的)而言,方法被定义为控件的名称(如:“button”、“radioButtons”)。用monkeyID检索匹配的控件,来调用这些方法。
举例查找控件:
// The OK Button
this.app.button("OK")
// The first table
this.app.table("#1")
一旦匹配上一个控件,你可以执行相应的操作。
每个控件有相应的可用的操作:
// Button OK Tap
this.app.button("OK").tap()
// Table countries Select France
this.app.table("countries").select("France")
JavaScriptAPI类比MonkeyTalk命令语法:
// Input name EnterText Bob
this.app.input("name").enterText("Bob")
timeout和thinktime是特定的属性,是js的可选项,包含在操作的字符串值参数中。
this.app.button("ok").tap({timeout:5000})
this.app.input("name").verify("foo",{timeout:5000})
this.app.label("message").verify("Hello")
可以在this.app后边加任何脚本的名称,作为一个方法,来调用该脚本。
this.app.scriptName().run(args...)
RunWith同样支持:
this.app.script("scriptName").runWith("somedata.csv");
// Call user.login.mt
// User ethel Login secret123
this.app.user("ethel").login("secret123")
所有的控件继承自View,因此会继承View的操作。
大多数时候,你导出一个js脚本,MonkeyTalk脚本会被一对一的关联,比如:
"Button LOGIN Tap"将被转换为 "this.app.input("LOGIN").tap();"
· select(value:String):void - Select an item by value.
· value - the value of the item to select.
· selectIndex(itemNumber:int):void - Selects anitem by index.
· itemNumber - the index of the item to select.
· longSelectIndex(itemNumber:int):void - Long press anitem by index.
· itemNumber - the index of the item to long press.
· enterDate(date:String):void - Change the current date value.
· date - A date with the format YYYY-MM-DD where YYYY isthe year, MM is the month (01-12), and DD is the day (01-31).
· shake():void - Shake the device.
· rotate(direction:String):void - Change the device orientation.
· direction - Left or Right (case insensitive)
· back():void - Navigate back. iOS: Pops the currentUINavigationItem (if there is one).
Android: Presses the hardware device key.
· forward():void - Navigate forward. iOS: Pushes thenext UINavigationItem, if there is one. Android: ignored.
· search():void - Press the search key. iOS:ignored. Android: Presses the device search key.
· menu():void - Press the menu key. iOS: ignored. Android:Presses the device menu key.
· vars(args:String):void - Document the named variables usedin the script.
· args - the named variables and their doc, in the formof: var='some doc' var2='other doc'
· script(doc:String):void - Document the script itself.
· doc - the doc
· selectIndex(itemNumber:int):void - Selects anitem by index.
· itemNumber - the index of the item to select.
· longSelectIndex(itemNumber:int):void - Long press anitem by index.
· itemNumber - the index of the item to long press.
· enterText(text:String, hitDone:boolean):void - Enter textinto the input field.
· text - the text to enter
· hitDone - iOS: if true, hit Done key after entering text. Android:ignored.
· clear():void - Clear text from the input field. iOS: Clears thefield. Android: ignored.
· select(value:String):void - Select an item by value.
· value - the value of the item to select.
· selectIndex(itemNumber:int):void - Selects anitem by index.
· itemNumber - the index of the item to select.
· longSelectIndex(itemNumber:int):void - Long press anitem by index.
· itemNumber - the index of the item to long press.
· select(value:String):void - Select an item by value.
· value - the value of the item to select.
· selectIndex(itemNumber:int):void - Selects anitem by index.
· itemNumber - the index of the item to select.
· longSelectIndex(itemNumber:int):void - Long press anitem by index.
· itemNumber - the index of the item to long press.
· select(value:float):void - Select a numeric component value
· value - the value to select
· select(value:float):void - Select a numeric component value
· value - the value to select
· run(args:String):String - Run the script with the givenargs.
· args - the arguments
· runWith(args:String):void - Data-drive the test script withthe given CSV data file.
· args - the arguments (where the first arg is the datafile filename)
scroll(x:int, y:int):void - Scroll to thespecified coordinates.
· x - the x-coordinate (horizontal)
· y - the y-coordinate (vertical
· run(args:String):String - Run the setup script with thegiven args.
· args - the arguments
· runWith(args:String):void - Data-drive the setup script withthe given CSV data file.
· args - the arguments (where the first arg is the datafile filename)
· select(value:float):void - Select a numeric component value
· value - the value to select
· select(value:String):void - Select an item by value.
· value - the value of the item to select.
· selectIndex(itemNumber:int):void - Selects anitem by index.
· itemNumber - the index of the item to select.
· longSelectIndex(itemNumber:int):void - Long press anitem by index.
· itemNumber - the index of the item to long press.
· selectRow(row:int, section:int):void - Select a row.
· row - the row to select
· section - the section containing the row. (Ignored onAndroid)
· scrollToRow(row:int, section:int):void - Scroll to arow by row number.
· row - the row to select
· section - the section containing the row. (Ignored onAndroid)
· scrollToRow(value:String):void - Scroll to arow by value.
· value - the value of the row to scroll to.
· setEditing(enabled:boolean):void -Enable/disable table editing. iOS: Enabled editing mode for table. Android:ignored.
· enabled - if true, enable editing, else disable editing.
· insert(row:int, section:int):void - Insert a rowinto the table. iOS: Inserts a row. Android: Ignored.
· row - the index of the row after which to insert a newrow.
· section - the section containing the row.
· remove(row:int, section:int):void - Remove a rowfrom the table. iOS: Deletes the row. Android: Ignored.
· row - the index of the row to be removed.
· section - the section containing the row.
· move(from:int, to:int):void - Move a row. iOS: Moves a row.Android: Ignored.
· from - the index of the row to be moved.
· to - the destination row for the move.
· select(value:String):void - Select an item by value.
· value - the value of the item to select.
· selectIndex(itemNumber:int):void - Selects anitem by index.
· itemNumber - the index of the item to select.
· longSelectIndex(itemNumber:int):void - Long press anitem by index.
· itemNumber - the index of the item to long press.
· run(args:String):String - Run the teardown script with thegiven args.
· args - the arguments
· runWith(args:String):void - Data-drive the teardown scriptwith the given CSV data file.
· args - the arguments (where the first arg is the datafile filename)
· run(args:String):String - Run the test script with the givenargs.
· args - the arguments
· runWith(args:String):void - Data-drive the test script withthe given CSV data file.
· args - the arguments (where the first arg is the datafile filename)
· enterText(text:String, hitDone:boolean):void - Enter textinto the input field.
· text - the text to enter
· hitDone - iOS: if true, hit Done key after entering text.Android: ignored.
· clear():void - Clear text from the input field. iOS: Clears thefield. Android: ignored.
· selectIndex(itemNumber:int):void - Selects anitem by index.
· itemNumber - the index of the item to select.
· longSelectIndex(itemNumber:int):void - Long press anitem by index.
· itemNumber - the index of the item to long press.
· define(args:String):void - Define the named variables used inthe script.
· args - the named variables (and optional default value),in the form of: var1 var2=default
· play():void - Play the video from the current playback point.
· pause():void - Stop the video at the current playback point.
· stop():void - Stop the video and set playback to the startingpoint.
· verify(expectedValue:String, propPath:String,failMessage:String):void
- Verifies that a property of the component has some expected value.
· expectedValue - the expected value of theproperty. If null, verifies the existence of the component.
· propPath - the property name or property path expression(defaults to 'value')
· failMessage - the custom failure message
· verifyNot(expectedValue:String, propPath:String,failMessage:String):void
- Verifies that a property of the component does not have some value.
· expectedValue - the value the component shouldn'thave. If null, verifies the non-existence of the component.
· propPath - the property name or property path expression(defaults to 'value')
· failMessage - the custom failure message
· verifyRegex(regex:String, propPath:String,failMessage:String):void
- Verifies that a property of the component matches some regular expression.
· regex - the regular expression to match
· propPath - the property name or property path expression(defaults to 'value')
· failMessage - the custom failure message
· verifyNotRegex(regex:String, propPath:String,failMessage:String):void
- Verifies that a property of the component does not have a value matching aregular expression.
· regex - the regular expression that should not match.
· propPath - the property name or property path expression(defaults to 'value')
· failMessage - the custom failure message
· verifyWildcard(wildcard:String, propPath:String,failMessage:String):void
- Verifies that a property of the component matches some wildcard expression.
· wildcard - the wildcard expression to match
· propPath - the property name or property path expression(defaults to 'value')
· failMessage - the custom failure message
· verifyNotWildcard(wildcard:String, propPath:String,failMessage:String):void
- Verifies that a property of the component does not have a value matching somewildcard expression.
· wildcard - the wildcard expression that should not match
· propPath - the property name or property path expression(defaults to 'value')
· failMessage - the custom failure message
· tap():void - Taps on the component. On Android, plays a'click'. On iOS, plays a
TouchDown/TouchMove/TouchUp sequence.
· longPress():void - Performs a long press on thecomponent. On Android, plays a 'longClick'. On iOS, plays a longPush gesture.
· touchDown(x:int, y:int):void - Start touching the component.
· x - x-coordinate of the touch
· y - y-coordinate of the touch
· touchMove(coords:int):void - Drag across the component
· coords - one or more (x,y) coordinate pairs specifying thepath of the drag gesture
· touchUp(x:int, y:int):void - Stop touching the component.
· x - x-coordinate of where touch is released
· y - y-coordinate of where touch is released
· pinch(scale:float, velocity:float):void - Pinch thecomponent.
· scale - The scale factor relative to the points of thetwo touches in screen coordinates
· velocity - The velocity of the pinch in scale factor persecond (read-only)
· swipe(direction:String):void - Swipe the component.
· direction - Left, Right, Up, or Down (case insensitive)
· drag(x1:int, y1:int, x2:int, y2:int):void - Touch down,drag across the component, then up
· x1 - starting x-coordinate
· y1 - starting y-coordinate
· x2 - ending x-coordinate
· y2 - ending y-coordinate
· get(variable:String, propPath:String):String
- Gets the value of the given property from the component, and set it into thegiven variable name.
· variable - the name of the variable to set
· propPath - the property name or path expression (defaults to'value')
· exec(method:String, args:String):String
- Call a method on a component. The method must take zero or more Stringarguments and return a String result.
· method - the method to call
· args - the String args to be supplied to the method