[老欧的自学]MonkeyRunner API
参数中,包含默认值的参数,为可选参数
MonkeyRunner.alert(string message, string title, string okTitle)
在脚本运行过程中,在PC端弹出敬告对话框.脚本暂停运行,直至关闭对话框.
参数:
message: 弹出对话框内容
title: 对话框的标题栏显示内容,默认值为"Alert"
okTitle : 对话框的按钮,默认值为"OK"
返回值:
不返回任何值.
例子:
MonkeyRunner.alert('Message','tip','sure')
实际应用:
可以使用在脚本运行之前,判断手机设备连接等.
MonkeyRunner.Chice(string message, iterable choices, string title)
在脚本运行过程中,在PC端弹出对话框,对话框中包含选择列表.脚本暂停运行,直至关闭对话框.
参数:
message: 弹出对话框显示内容
choices: 选择列表数组
title: 对话框标题内容.默认为"Input"
返回值:
如果用户选择并点击确认按钮,返回0-数组最大值.
如果用户关闭了对话框,返回值为-1.
例子:
MonkeyRunner.choice('message',['choice1','choice2','choice3'],'title')
实际应用:
可以在脚本运行之前,选择需要运行的脚本.
MonkeyRunner.help(string format)
打印出MonkeyRunner的帮助文档.
参数:
format: 可以输入"text"或者"html"进行输出
返回值:
没有
不知道为什么这个参数我是从来没有成功过
MonkeyRunner.input(string message, string initialValue, string title, string okTitle, string cancelTitle)
在脚本运行中,在PC端弹出可输入对话框.脚本暂停运行,直至对话框关闭
参数:
message: 对话框显示信息
initiaValue: 文本框显示文本.默认为空
title: 对话框标题内容.默认为"Input"
okTitle: 确认按钮的文本显示.默认为"OK"
cancelTitle: 取消按钮的文本显示.默认为"Cancel"
返回值:
如果用户点击确认按钮,将文本框的值返回
如果用户点击取消按钮,将返回一个空的String
例子:
MonkeyRunner.input('message','please input','title','ok','cancel')
实际应用:
用例执行之前,输入测试存放文件夹名称.
在脚本运行时,可以灵活输入文本,进行测试,不过感觉意义不大
MonkeyRunner.sleep(float seconds)
脚本暂停运行指定的时间,单位以秒计算.
参数:
seconds: 暂停的时间.
例子:
MonkeyRunner.sleep(1.5)
实际应用:
太多了
MonkeyRunner.waitForConnection(float timeout, string deviceId)
尝试对android设备或模拟器通过monkeyrunner进行连接
参数:
timeout: 等待超时时间,默认值为永久等待.
deviceId: 通过设备ID去设别手机或模拟器.如果只有一台手机的时候,不需要输入.
deviceId可以通过adb devices来获得
返回值:
连接成功后,建立一个MonkeyDevice.用来控制手机或模拟器.
例子:
device = MonkeyRunner.waitForConnection(1.5,'181fa9b2')
实际应用:
每个脚本必备,没啥可说的
deviceId可以用于多设备选择.用adb devices
附MonkeyRunner源码
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov. // Jad home page: http://kpdus.tripod.com/jad.html // Decompiler options: packimports(3) fieldsfirst ansi space // Source File Name: MonkeyRunner.java package com.android.monkeyrunner; import com.android.chimpchat.ChimpChat; import com.android.chimpchat.core.ChimpImageBase; import com.google.common.base.Functions; import com.google.common.base.Preconditions; import com.google.common.collect.Collections2; import java.util.Collection; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import org.python.core.*; // Referenced classes of package com.android.monkeyrunner: // MonkeyDevice, MonkeyImage, JythonUtils, MonkeyRunnerHelp public class MonkeyRunner extends PyObject implements ClassDictInit { private static final Logger LOG = Logger.getLogger(com/android/monkeyrunner/MonkeyRunner.getCanonicalName()); private static ChimpChat chimpchat; public MonkeyRunner() { } public static void classDictInit(PyObject dict) { JythonUtils.convertDocAnnotationsForClass(com/android/monkeyrunner/MonkeyRunner, dict); } static void setChimpChat(ChimpChat chimp) { chimpchat = chimp; } public static MonkeyDevice waitForConnection(PyObject args[], String kws[]) { ArgParser ap = JythonUtils.createArgParser(args, kws); Preconditions.checkNotNull(ap); long timeoutMs; try { double timeoutInSecs = JythonUtils.getFloat(ap, 0); timeoutMs = (long)(timeoutInSecs * 1000D); } catch (PyException e) { timeoutMs = 0x7fffffffffffffffL; } com.android.chimpchat.core.IChimpDevice device = chimpchat.waitForConnection(timeoutMs, ap.getString(1, ".*")); MonkeyDevice chimpDevice = new MonkeyDevice(device); return chimpDevice; } public static void sleep(PyObject args[], String kws[]) { ArgParser ap = JythonUtils.createArgParser(args, kws); Preconditions.checkNotNull(ap); double seconds = JythonUtils.getFloat(ap, 0); long ms = (long)(seconds * 1000D); try { Thread.sleep(ms); } catch (InterruptedException e) { LOG.log(Level.SEVERE, "Error sleeping", e); } } public static String help(PyObject args[], String kws[]) { ArgParser ap = JythonUtils.createArgParser(args, kws); Preconditions.checkNotNull(ap); String format = ap.getString(0, "text"); return MonkeyRunnerHelp.helpString(format); } public static void alert(PyObject args[], String kws[]) { ArgParser ap = JythonUtils.createArgParser(args, kws); Preconditions.checkNotNull(ap); String message = ap.getString(0); String title = ap.getString(1, "Alert"); String buttonTitle = ap.getString(2, "OK"); alert(message, title, buttonTitle); } public static String input(PyObject args[], String kws[]) { ArgParser ap = JythonUtils.createArgParser(args, kws); Preconditions.checkNotNull(ap); String message = ap.getString(0); String initialValue = ap.getString(1, ""); String title = ap.getString(2, "Input"); return input(message, initialValue, title); } public static int choice(PyObject args[], String kws[]) { ArgParser ap = JythonUtils.createArgParser(args, kws); Preconditions.checkNotNull(ap); String message = ap.getString(0); Collection choices = Collections2.transform(JythonUtils.getList(ap, 1), Functions.toStringFunction()); String title = ap.getString(2, "Input"); return choice(message, title, choices); } public static MonkeyImage loadImageFromFile(PyObject args[], String kws[]) { ArgParser ap = JythonUtils.createArgParser(args, kws); Preconditions.checkNotNull(ap); String path = ap.getString(0); com.android.chimpchat.core.IChimpImage image = ChimpImageBase.loadImageFromFile(path); return new MonkeyImage(image); } public static void alert(String message, String title, String okTitle) { Object options[] = { okTitle }; JOptionPane.showOptionDialog(null, message, title, -1, 1, null, options, options[0]); } public static int choice(String message, String title, Collection choices) { Object possibleValues[] = choices.toArray(); Object selectedValue = JOptionPane.showInputDialog(null, message, title, 3, null, possibleValues, possibleValues[0]); for (int x = 0; x < possibleValues.length; x++) if (possibleValues[x].equals(selectedValue)) return x; return -1; } public static String input(String message, String initialValue, String title) { return (String)JOptionPane.showInputDialog(null, message, title, 3, null, null, initialValue); } }