當前位置:
首頁 > 知識 > Monkey可視化工具開發

Monkey可視化工具開發

在用monkey做android壓力測試時,一般都會選擇直接運行monkey命令的形式,由於重複性較強,我就寫了個monkey可視化運行的工具,monkey參數可自行調整。下面先附上效果圖:

Monkey可視化工具開發

以下為代碼:

1.cmd命令執行工具類

[java] view plain copy

  1. import

    java.io.BufferedReader;
  2. import

    java.io.IOException;
  3. import

    java.io.InputStreamReader;
  4. import

    java.util.ArrayList;
  5. import

    java.util.List;
  6. public

    class

    CmdUtils {
  7. List<String>resultList=

    new

    ArrayList<String>();
  8. public

    List<String> excuteCmd(String command){
  9. //String command="adb devices";
  10. String line =

    null

    ;

  11. StringBuilder sb =

    new

    StringBuilder();
  12. Runtime runtime = Runtime.getRuntime();
  13. try

    {
  14. Process process = runtime.exec(command);
  15. BufferedReader bufferedReader =

    new

    BufferedReader(

    new

    InputStreamReader(process.getInputStream()));
  16. while

    ((line = bufferedReader.readLine()) !=

    null

    ) {
  17. resultList.add(line);
  18. }
  19. }

    catch

    (IOException e) {
  20. e.printStackTrace();
  21. }
  22. return

    resultList;
  23. }
  24. }

2.獲取連接設備工具類

[java] view plain copy

  1. import

    java.io.BufferedReader;
  2. import

    java.io.IOException;
  3. import

    java.io.InputStreamReader;
  4. import

    java.util.ArrayList;
  5. import

    java.util.List;
  6. public

    class

    AndroidDevicesInfo {
  7. List<String>devicesList=

    new

    ArrayList<String>();
  8. public

    List<String> getAndroidDevicesInfo(){
  9. String command="adb devices";
  10. String line =

    null

    ;

  11. StringBuilder sb =

    new

    StringBuilder();
  12. Runtime runtime = Runtime.getRuntime();
  13. try

    {
  14. Process process = runtime.exec(command);
  15. BufferedReader bufferedReader =

    new

    BufferedReader(

    new

    InputStreamReader(process.getInputStream()));
  16. while

    ((line = bufferedReader.readLine()) !=

    null

    ) {
  17. if

    (line.contains("device")){
  18. if

    (!line.contains("devices")){
  19. String deviceId=line.substring(0, line.indexOf("device"));
  20. devicesList.add(deviceId);
  21. }
  22. }
  23. }
  24. }

    catch

    (IOException e) {
  25. e.printStackTrace();
  26. }
  27. return

    devicesList;

  28. }
  29. }

3.載入指定設備的應用包集合

[java] view plain copy

  1. package

    MonkeyTool;
  2. import

    java.io.BufferedReader;
  3. import

    java.io.IOException;
  4. import

    java.io.InputStreamReader;
  5. import

    java.util.ArrayList;
  6. import

    java.util.List;
  7. public

    class

    AndroidPackageInfo {

  8. List<String>packageList=

    new

    ArrayList<String>();
  9. //獲取指定設備下的包集合
  10. public

    List<String> getAndroidPackageInfo(String deviceId){
  11. String command="adb -s "+deviceId+" shell pm list packages";
  12. String line =

    null

    ;
  13. StringBuilder sb =

    new

    StringBuilder();
  14. Runtime runtime = Runtime.getRuntime();
  15. try

    {
  16. Process process = runtime.exec(command);
  17. BufferedReader bufferedReader =

    new

    BufferedReader(

    new

    InputStreamReader(process.getInputStream()));
  18. while

    ((line = bufferedReader.readLine()) !=

    null

    ) {
  19. if

    (line.contains("package")){

  20. //截取包字元串放入list中
  21. String packageItem=line.substring(line.indexOf("package")+8,line.length());
  22. //String packageItem=line;
  23. packageList.add(packageItem);
  24. }
  25. }
  26. }

    catch

    (IOException e) {
  27. e.printStackTrace();
  28. }
  29. return

    packageList;
  30. }
  31. }

4.載入設備線程

[java] view plain copy

  1. package

    MonkeyTool;
  2. import

    java.util.List;
  3. import

    javax.swing.JComboBox;
  4. //載入設備任務
  5. public

    class

    DevicesThread

    extends

    Thread{

  6. AndroidDevicesInfo androidDevicesInfo;
  7. Object obj;
  8. String[] s;
  9. JComboBox devicesComBox;
  10. List<String> list;
  11. String devicesId;
  12. public

    DevicesThread(JComboBox devicesComBox) {
  13. // TODO Auto-generated constructor stub
  14. this

    .devicesComBox=devicesComBox;
  15. //System.out.println("aa");
  16. }
  17. @Override
  18. public

    void

    run() {
  19. // TODO Auto-generated method stub
  20. super

    .run();
  21. // TODO Auto-generated method stub
  22. devicesId=(String) devicesComBox.getSelectedItem();
  23. System.out.println(devicesId);
  24. androidDevicesInfo=

    new

    AndroidDevicesInfo();
  25. list=androidDevicesInfo.getAndroidDevicesInfo();
  26. devicesComBox.removeAllItems();
  27. if

    (list.size()==0){
  28. devicesComBox.addItem("無設備連接");
  29. }
  30. else

    {
  31. for

    (String item : list) {
  32. System.out.println(item);
  33. devicesComBox.addItem(item);
  34. }
  35. }
  36. }
  37. }

[java] view plain copy

  1. package

    MonkeyTool;
  2. import

    java.util.List;
  3. import

    javax.swing.JComboBox;
  4. //載入包名任務
  5. public

    class

    PackageThread

    extends

    Thread {
  6. AndroidDevicesInfo androidDevicesInfo;
  7. AndroidPackageInfo androidPackageInfo;
  8. Object obj;
  9. String[] s;
  10. JComboBox devicesComBox;
  11. JComboBox packageComBox;
  12. List<String> list;
  13. String devicesId;
  14. public

    PackageThread(JComboBox devicesComBox,JComboBox packageComBox,String devicesId) {
  15. // TODO Auto-generated constructor stub
  16. this

    .devicesComBox=devicesComBox;
  17. this

    .packageComBox=packageComBox;
  18. this

    .devicesId=devicesId;
  19. }
  20. @Override
  21. public

    void

    run() {
  22. // TODO Auto-generated method stub
  23. super

    .run();
  24. // TODO Auto-generated method stub
  25. devicesId=(String) devicesComBox.getSelectedItem();
  26. System.out.println(devicesId);
  27. androidPackageInfo=

    new

    AndroidPackageInfo();
  28. list=androidPackageInfo.getAndroidPackageInfo(devicesId);
  29. packageComBox.removeAllItems();
  30. if

    (list.size()==0){
  31. packageComBox.addItem("未識別應用包名");
  32. }
  33. else

    {
  34. for

    (String item : list) {
  35. System.out.println(item);
  36. packageComBox.addItem(item);
  37. }
  38. }
  39. }
  40. }

6.xml工具類

[java] view plain copy

  1. package

    MonkeyTool;
  2. import

    java.io.File;
  3. import

    java.io.FileReader;
  4. import

    java.util.ArrayList;
  5. import

    java.util.HashMap;
  6. import

    java.util.List;
  7. import

    java.util.Map;
  8. import

    javax.xml.parsers.DocumentBuilder;
  9. import

    javax.xml.parsers.DocumentBuilderFactory;
  10. import

    javax.xml.parsers.ParserConfigurationException;
  11. import

    org.w3c.dom.Document;
  12. import

    org.w3c.dom.Element;
  13. import

    org.w3c.dom.Node;
  14. import

    org.w3c.dom.NodeList;
  15. //xml解析類
  16. public

    class

    ReadMonkeyConfig {
  17. List<String>case_list;
  18. Map<String, String> map;
  19. List<HashMap<String, String>>monkeyArgs;
  20. //獲取id名
  21. public

    List XmlToList_id()

    throws

    Exception {
  22. case_list=

    new

    ArrayList<>();
  23. // File xmlFile = new File("src/Testcase.xml");
  24. // File xmlFile = new File("E:/YallaProject/Testcase.xml");
  25. File file=

    new

    File("");
  26. System.out.println(file.getAbsolutePath());
  27. File xmlFile=

    new

    File(file.getAbsolutePath()+"\MonkeyConfig.xml");
  28. DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  29. DocumentBuilder builder = builderFactory.newDocumentBuilder();
  30. Document doc = builder.parse(xmlFile);
  31. doc.getDocumentElement().normalize();
  32. System.out.println("Root element: "+doc.getDocumentElement().getNodeName());
  33. NodeList nList = doc.getElementsByTagName("case");
  34. for

    (

    int

    i = 0 ; i<nList.getLength();i++){
  35. Node node = nList.item(i);
  36. System.out.println("Node name: "+ node.getNodeName());
  37. Element ele = (Element)node;
  38. // System.out.println("----------------------------");
  39. if

    (node.getNodeType() == Element.ELEMENT_NODE){
  40. case_list.add(ele.getAttribute("id"));
  41. }
  42. }
  43. return

    case_list;
  44. }
  45. //根據id名獲取各項配置
  46. public

    List getConfigData(String args)

    throws

    Exception {
  47. map=

    new

    HashMap<String,String>();
  48. List<Map<String, String>> list =

    new

    ArrayList<Map<String, String>>();;
  49. File file=

    new

    File("");
  50. System.out.println(file.getAbsolutePath());
  51. File xmlFile=

    new

    File(file.getAbsolutePath()+"\MonkeyConfig.xml");
  52. DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  53. DocumentBuilder builder = builderFactory.newDocumentBuilder();
  54. Document doc = builder.parse(xmlFile);
  55. doc.getDocumentElement().normalize();
  56. System.out.println("Root element: "+doc.getDocumentElement().getNodeName());
  57. NodeList nList = doc.getElementsByTagName("case");
  58. for

    (

    int

    i = 0 ; i<nList.getLength();i++){
  59. Node node = nList.item(i);
  60. System.out.println("Node name: "+ node.getNodeName());
  61. Element ele = (Element)node;
  62. if

    (node.getNodeType() == Element.ELEMENT_NODE){
  63. if

    (ele.getAttribute("id").equals(args)){
  64. map.put("touchEvent", ele.getElementsByTagName("touchEvent").item(0).getTextContent());
  65. map.put("motionEvent", ele.getElementsByTagName("motionEvent").item(0).getTextContent());
  66. map.put("switchEvent", ele.getElementsByTagName("switchEvent").item(0).getTextContent());
  67. map.put("pinchzoomEvent", ele.getElementsByTagName("pinchzoomEvent").item(0).getTextContent());
  68. map.put("trackballEvent", ele.getElementsByTagName("trackballEvent").item(0).getTextContent());
  69. map.put("navEvent", ele.getElementsByTagName("navEvent").item(0).getTextContent());
  70. map.put("majornavEvent", ele.getElementsByTagName("majornavEvent").item(0).getTextContent());
  71. map.put("syskeysEvent", ele.getElementsByTagName("syskeysEvent").item(0).getTextContent());
  72. map.put("flipEvent", ele.getElementsByTagName("flipEvent").item(0).getTextContent());
  73. map.put("anyeventEvent", ele.getElementsByTagName("anyeventEvent").item(0).getTextContent());
  74. map.put("throttle", ele.getElementsByTagName("throttle").item(0).getTextContent());
  75. map.put("seed", ele.getElementsByTagName("seed").item(0).getTextContent());
  76. map.put("times", ele.getElementsByTagName("times").item(0).getTextContent());
  77. list.add(map);
  78. //case_list.add(ele.getElementsByTagName("touchEvent").item(0).getTextContent());
  79. }
  80. }
  81. }
  82. return

    list;
  83. }
  84. }

[java] view plain copy

  1. package

    MonkeyTool;
  2. import

    java.util.List;
  3. import

    javax.swing.JComboBox;
  4. //展示monkey配置項線程
  5. public

    class

    MonkeyConfigThread

    extends

    Thread {
  6. String[] s;
  7. JComboBox monkeyComBox;
  8. ReadMonkeyConfig config;
  9. List<String> list;
  10. String devicesId;
  11. public

    MonkeyConfigThread(JComboBox monkeyComBox) {
  12. this

    .monkeyComBox=monkeyComBox;
  13. }
  14. @Override
  15. public

    void

    run() {
  16. super

    .run();
  17. config=

    new

    ReadMonkeyConfig();
  18. try

    {
  19. list=config.XmlToList_id();
  20. monkeyComBox.removeAllItems();
  21. if

    (list.size()==0){
  22. monkeyComBox.addItem("配置文件中未配置");
  23. }
  24. else

    {
  25. for

    (String item : list) {
  26. System.out.println(item);
  27. monkeyComBox.addItem(item);
  28. }
  29. }
  30. }

    catch

    (Exception e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35. }

8.設備面板代碼

[java] view plain copy

  1. package

    MonkeyTool;
  2. import

    java.awt.event.ActionEvent;
  3. import

    java.awt.event.ActionListener;
  4. import

    java.util.List;
  5. import

    javax.swing.JButton;
  6. import

    javax.swing.JComboBox;
  7. import

    javax.swing.JLabel;
  8. import

    javax.swing.JPanel;
  9. import

    javax.swing.JTextField;
  10. public

    class

    JpanelDevices

    extends

    JPanel{
  11. AndroidDevicesInfo androidDevicesInfo;
  12. AndroidPackageInfo androidPackageInfo;
  13. Object obj;
  14. String[] s;
  15. JComboBox devicesComBox;
  16. JComboBox packageComBox;
  17. List<String> list;
  18. String devicesId;
  19. String devicesId_s;
  20. JButton jButton;
  21. public

    String getDevice(){
  22. //JComboBox<String> devicesComBox=new JComboBox<>();
  23. //this.devicesComBox=devicesComBox;
  24. return

    (String) devicesComBox.getSelectedItem();
  25. }
  26. public

    JpanelDevices(){
  27. setBounds(0,15, 720,30);
  28. setLayout(

    null

    );
  29. initView();
  30. addView();
  31. clickListenser();
  32. getDevice();
  33. }
  34. //初始化控制項
  35. public

    void

    initView(){
  36. jButton=

    new

    JButton("刷新設備");
  37. jButton.setBounds(10,0,100,25);
  38. devicesComBox=

    new

    JComboBox();
  39. packageComBox=

    new

    JComboBox();
  40. devicesComBox.setBounds(170, 0,192, 25);
  41. packageComBox.setBounds(438, 0,265, 25);
  42. }
  43. public

    void

    addView(){
  44. add(jButton);
  45. add(devicesComBox);
  46. add(packageComBox);
  47. }
  48. private

    void

    clickListenser(){
  49. jButton.addActionListener(

    new

    ActionListener() {
  50. //點擊刷新按鈕,刷新設備Id
  51. @Override
  52. public

    void

    actionPerformed(ActionEvent e) {
  53. new

    DevicesThread(devicesComBox).start();
  54. }
  55. });
  56. devicesComBox.addActionListener(

    new

    ActionListener() {
  57. @Override
  58. public

    void

    actionPerformed(ActionEvent e) {
  59. // TODO Auto-generated method stub
  60. new

    PackageThread(devicesComBox,packageComBox,devicesId).start();
  61. }
  62. });
  63. }
  64. }

[java] view plain copy

  1. package

    MonkeyTool;
  2. import

    java.awt.Color;
  3. import

    java.awt.event.ActionEvent;
  4. import

    java.awt.event.ActionListener;
  5. import

    java.util.HashMap;
  6. import

    java.util.List;
  7. import

    javax.swing.BorderFactory;
  8. import

    javax.swing.JButton;
  9. import

    javax.swing.JCheckBox;
  10. import

    javax.swing.JComboBox;
  11. import

    javax.swing.JLabel;
  12. import

    javax.swing.JOptionPane;
  13. import

    javax.swing.JPanel;
  14. import

    javax.swing.JTextField;
  15. public

    class

    JpanelMonkey

    extends

    JPanel{
  16. ReadMonkeyConfig config=

    new

    ReadMonkeyConfig();
  17. JpanelMonkey jpanelMonkey;
  18. JLabel touchLable,motionLable,appSwitchLable,throttle,seedLable,timesLable,IgnoreCrashLable,IgnoreTimeoutLable,IgnoreSecurityExceptionLable,KillProcessAfterErrorLable,MonitorNativeCrashLable,pinchzoomLable,trackballLable,navLable,majornavLable,syskeysLable,flipLable,anyeventLable;
  19. JTextField touchTextField,motionTextField,appswtichTextField,throttleTextField,seedTextField,timesTextField,pinchzoomTextField,trackballTextField,navTextField,majornavTextField,syskeysTextField,flipTextField,anyeventTextField;
  20. JButton button,button2,monkeyConfigLable,startConfigWrite;
  21. JCheckBox jCheckBox;
  22. JCheckBox jCheckBox2;
  23. JCheckBox jCheckBox3;
  24. JCheckBox jCheckBox4;
  25. JCheckBox jCheckBox5;
  26. JComboBox monkeyComboBox;
  27. String devicesId_s;
  28. StringBuilder builder,builderEventPercent;
  29. String clickPercent,swipePercent,swichPercent,waitTime,seed,times,pinchzoom,trackball,nav,majornav,syskeys,flip,anyevent;
  30. //CmdUtils cmdUtils=new CmdUtils();
  31. public

    JpanelDevices jpanelDevices;
  32. //構造方法穿參實時獲取值
  33. public

    JpanelMonkey(JpanelDevices jpanelDevices){
  34. setBorder(BorderFactory.createTitledBorder("分組框")); //設置面板邊框,實現分組框的效果,此句代碼為關鍵代碼
  35. setBorder(BorderFactory.createLineBorder(Color.blue));
  36. setLayout(

    null

    );
  37. setBounds(0,55,720,500);
  38. initView();
  39. addView();
  40. this

    .jpanelDevices=jpanelDevices;
  41. clickLinstner(jpanelDevices);
  42. new

    MonkeyConfigThread(monkeyComboBox).start();
  43. }
  44. //以下為監聽事件
  45. private

    void

    clickLinstner(JpanelDevices jpanelDevices) {
  46. //啟動monkey
  47. button.addActionListener(

    new

    ActionListener() {
  48. public

    void

    actionPerformed(ActionEvent e) {
  49. new

    GoMonkey().start();
  50. }
  51. });
  52. //停止monkey
  53. button2.addActionListener(

    new

    ActionListener() {
  54. @Override
  55. public

    void

    actionPerformed(ActionEvent e) {
  56. // TODO Auto-generated method stub
  57. new

    StopMonkey().start();
  58. }
  59. });
  60. //獲取monkey配置項
  61. monkeyConfigLable.addActionListener(

    new

    ActionListener() {
  62. @Override
  63. public

    void

    actionPerformed(ActionEvent e) {
  64. // TODO Auto-generated method stub
  65. new

    MonkeyConfigThread(monkeyComboBox).start();
  66. }
  67. });
  68. //monkey配置項寫入
  69. startConfigWrite.addActionListener(

    new

    ActionListener() {
  70. @Override
  71. public

    void

    actionPerformed(ActionEvent e) {
  72. // TODO Auto-generated method stub
  73. new

    SetMonkeyConfig().start();
  74. }
  75. });
  76. }
  77. //寫入monkey配置項線程
  78. class

    SetMonkeyConfig

    extends

    Thread{
  79. @Override
  80. public

    void

    run() {
  81. // TODO Auto-generated method stub
  82. super

    .run();
  83. try

    {
  84. String id=(String) monkeyComboBox.getSelectedItem();
  85. List<HashMap<String, String>>monkeyArgs=config.getConfigData(id);
  86. touchTextField.setText(monkeyArgs.get(0).get("touchEvent"));
  87. motionTextField.setText(monkeyArgs.get(0).get("motionEvent"));
  88. appswtichTextField.setText(monkeyArgs.get(0).get("switchEvent"));
  89. throttleTextField.setText(monkeyArgs.get(0).get("throttle"));
  90. seedTextField.setText(monkeyArgs.get(0).get("seed"));
  91. timesTextField.setText(monkeyArgs.get(0).get("times"));
  92. pinchzoomTextField.setText(monkeyArgs.get(0).get("pinchzoomEvent"));
  93. trackballTextField.setText(monkeyArgs.get(0).get("trackballEvent"));
  94. navTextField.setText(monkeyArgs.get(0).get("navEvent"));
  95. majornavTextField.setText(monkeyArgs.get(0).get("majornavEvent"));
  96. syskeysTextField.setText(monkeyArgs.get(0).get("syskeysEvent"));
  97. flipTextField.setText(monkeyArgs.get(0).get("flipEvent"));
  98. anyeventTextField.setText(monkeyArgs.get(0).get("anyeventEvent"));
  99. }

    catch

    (Exception e) {
  100. // TODO Auto-generated catch block
  101. JOptionPane.showConfirmDialog(

    null

    , "配置文件不存在","錯誤提示", JOptionPane.DEFAULT_OPTION);
  102. }
  103. }
  104. }
  105. class

    StopMonkey

    extends

    Thread{
  106. CmdUtils cmdUtils=

    new

    CmdUtils();
  107. @Override
  108. public

    void

    run() {
  109. // TODO Auto-generated method stub
  110. super

    .run();
  111. if

    ((String)jpanelDevices.devicesComBox.getSelectedItem()!=

    null

    ){
  112. devicesId_s=" -s "+(String)jpanelDevices.devicesComBox.getSelectedItem();
  113. }
  114. else

    {
  115. devicesId_s=" ";
  116. }
  117. //獲取monkey進程並kill
  118. String commands="adb"+devicesId_s+" shell ps |grep monkey";
  119. System.out.println(cmdUtils.excuteCmd(commands));
  120. String log_pid_result=cmdUtils.excuteCmd(commands).get(0);
  121. System.out.println(log_pid_result);
  122. String g=log_pid_result.substring(7,15);
  123. System.out.println(g);
  124. System.out.println(Thread.currentThread().getName());
  125. cmdUtils.excuteCmd("adb "+devicesId_s+" shell kill "+g);
  126. System.out.println("ok");
  127. }
  128. }
  129. //monkey啟動線程
  130. class

    GoMonkey

    extends

    Thread{
  131. CmdUtils cmdUtils=

    new

    CmdUtils();
  132. @Override
  133. public

    void

    run() {
  134. // TODO Auto-generated method stub
  135. super

    .run();
  136. String devicesId=(String) jpanelDevices.devicesComBox.getSelectedItem();
  137. String packageName=(String) jpanelDevices.packageComBox.getSelectedItem();
  138. builder=

    new

    StringBuilder();
  139. builderEventPercent=

    new

    StringBuilder();
  140. if

    (jCheckBox.isSelected()){
  141. builder.append(" --ignore-crashes");
  142. }
  143. else

    {
  144. builder.append("");
  145. }
  146. if

    (jCheckBox2.isSelected()){
  147. builder.append(" --ignore-timeouts");
  148. }
  149. else

    {
  150. builder.append("");
  151. }
  152. if

    (jCheckBox3.isSelected()){
  153. builder.append(" --ignore-security-exceptions");
  154. }
  155. else

    {
  156. builder.append("");
  157. }
  158. if

    (jCheckBox4.isSelected()){
  159. builder.append(" --kill-process-after-error");
  160. }
  161. else

    {
  162. builder.append("");
  163. }
  164. if

    (jCheckBox5.isSelected()){
  165. builder.append(" --monitor-native-crashes");
  166. }
  167. else

    {
  168. builder.append("");
  169. }
  170. System.out.println(builder);
  171. if

    (touchTextField.getText().equals("")==

    false

    ){
  172. clickPercent=" --pct-touch "+touchTextField.getText().toString();
  173. builderEventPercent.append(clickPercent);
  174. }
  175. else

    {
  176. clickPercent="";
  177. }
  178. if

    (motionTextField.getText().equals("")==

    false

    ){
  179. swipePercent=" --pct-motion "+motionTextField.getText().toString();
  180. builderEventPercent.append(swipePercent);
  181. }
  182. else

    {
  183. swipePercent="";
  184. }
  185. if

    (appswtichTextField.getText().equals("")==

    false

    ){
  186. swichPercent=" --pct-appswitch "+appswtichTextField.getText().toString();
  187. builderEventPercent.append(swichPercent);
  188. }
  189. else

    {
  190. swichPercent="";
  191. }
  192. if

    (throttleTextField.getText().equals("")==

    false

    ){
  193. waitTime=" --throttle "+throttleTextField.getText().toString();
  194. }
  195. else

    {
  196. waitTime="";
  197. }
  198. if

    (seedTextField.getText().equals("")==

    false

    ){
  199. seed=" -s "+seedTextField.getText().toString();
  200. }
  201. else

    {
  202. seed="";
  203. }
  204. if

    (timesTextField.getText().equals("")){
  205. JOptionPane.showConfirmDialog(

    null

    , "必須輸入運行次數","輸入提示", JOptionPane.DEFAULT_OPTION);
  206. }
  207. else

    {
  208. times=" "+timesTextField.getText().toString();
  209. }
  210. if

    (pinchzoomTextField.getText().equals("")){
  211. pinchzoom="";
  212. }
  213. else

    {
  214. pinchzoom=" --pct-pinchzoom "+pinchzoomTextField.getText().toString();
  215. builderEventPercent.append(pinchzoom);
  216. }
  217. if

    (trackballTextField.getText().equals("")){
  218. trackball="";
  219. }
  220. else

    {
  221. trackball=" --pct-trackball "+trackballTextField.getText().toString();
  222. builderEventPercent.append(trackball);
  223. }
  224. if

    (navTextField.getText().equals("")){
  225. nav="";
  226. }
  227. else

    {
  228. nav=" --pct-nav "+navTextField.getText().toString();
  229. builderEventPercent.append(nav);
  230. }
  231. if

    (majornavTextField.getText().equals("")){
  232. majornav="";
  233. }
  234. else

    {
  235. majornav=" --pct-majornav "+majornavTextField.getText().toString();
  236. builderEventPercent.append(majornav);
  237. }
  238. if

    (syskeysTextField.getText().equals("")){
  239. syskeys="";
  240. }
  241. else

    {
  242. syskeys=" --pct-syskeys "+syskeysTextField.getText().toString();
  243. builderEventPercent.append(syskeys);
  244. }
  245. if

    (flipTextField.getText().equals("")){
  246. flip="";
  247. }
  248. else

    {
  249. flip=" --pct-flip "+flipTextField.getText().toString();
  250. builderEventPercent.append(flip);
  251. }
  252. if

    (anyeventTextField.getText().equals("")){
  253. anyevent="";
  254. }
  255. else

    {
  256. anyevent=" --pct-anyevent "+anyeventTextField.getText().toString();
  257. builderEventPercent.append(anyevent);
  258. }
  259. System.out.println(devicesId);
  260. System.out.println(packageName);
  261. //String monkeyCommand="adb -s "+devicesId+" shell monkey -p "+packageName+waitTime+clickPercent+swipePercent+swichPercent+builder.toString()+seedLable+timesLable;
  262. //System.out.println(j.getSelectedItem().toString());
  263. String monkeyCommand="adb -s "+devicesId+" shell monkey -p "+packageName+waitTime+builderEventPercent.toString()+builder.toString()+seed+times;
  264. System.out.println(monkeyCommand);
  265. System.out.println(cmdUtils.excuteCmd(monkeyCommand));
  266. }
  267. }
  268. //初始化控制項
  269. private

    void

    initView(){
  270. touchLable=

    new

    JLabel("Touch Event (%)");
  271. touchLable.setBounds(10, 30,100, 25);
  272. motionLable=

    new

    JLabel("Motion Event (%)");
  273. motionLable.setBounds(10, 57,100, 25);
  274. appSwitchLable=

    new

    JLabel("Appswitch (%)");
  275. appSwitchLable.setBounds(10, 84,100, 25);
  276. pinchzoomLable=

    new

    JLabel("pinchzoom (%)");
  277. pinchzoomLable.setBounds(10, 111,100, 25);
  278. trackballLable=

    new

    JLabel("trackball (%)");
  279. trackballLable.setBounds(10,138,100, 25);
  280. navLable=

    new

    JLabel("nav (%)");
  281. navLable.setBounds(10,165,100, 25);
  282. majornavLable=

    new

    JLabel("majornav (%)");
  283. majornavLable.setBounds(10,192,100, 25);
  284. syskeysLable=

    new

    JLabel("syskeys (%)");
  285. syskeysLable.setBounds(10,219,100, 25);
  286. flipLable=

    new

    JLabel("flip (%)");
  287. flipLable.setBounds(10,246,100, 25);
  288. anyeventLable=

    new

    JLabel("anevent (%)");
  289. anyeventLable.setBounds(10,273,100, 25);
  290. throttle=

    new

    JLabel("Throttle (S)");
  291. throttle.setBounds(10, 300,100, 25);
  292. seedLable=

    new

    JLabel("Seed");
  293. seedLable.setBounds(10, 327,100, 25);
  294. timesLable=

    new

    JLabel("Times");
  295. timesLable.setBounds(10, 354,100, 25);
  296. IgnoreCrashLable=

    new

    JLabel("Ignore Crash");
  297. IgnoreCrashLable.setBounds(228, 30,120, 25);
  298. IgnoreTimeoutLable=

    new

    JLabel("Ignore Timeout");
  299. IgnoreTimeoutLable.setBounds(228, 57,150, 25);
  300. IgnoreSecurityExceptionLable=

    new

    JLabel("Ignore Security Exception");
  301. IgnoreSecurityExceptionLable.setBounds(228, 84,150, 25);
  302. KillProcessAfterErrorLable=

    new

    JLabel("Kill Process After Error");
  303. KillProcessAfterErrorLable.setBounds(228, 111,150, 25);
  304. MonitorNativeCrashLable=

    new

    JLabel("Monitor Native Crash");
  305. MonitorNativeCrashLable.setBounds(228, 138,150, 25);
  306. touchTextField=

    new

    JTextField();
  307. touchTextField.setBounds(113, 30,80, 25);
  308. motionTextField=

    new

    JTextField();
  309. motionTextField.setBounds(113, 57,80, 25);
  310. appswtichTextField=

    new

    JTextField();
  311. appswtichTextField.setBounds(113, 84,80, 25);
  312. pinchzoomTextField=

    new

    JTextField();
  313. pinchzoomTextField.setBounds(113, 111,80, 25);
  314. trackballTextField=

    new

    JTextField();
  315. trackballTextField.setBounds(113, 138,80, 25);
  316. navTextField=

    new

    JTextField();
  317. navTextField.setBounds(113, 165,80, 25);
  318. majornavTextField=

    new

    JTextField();
  319. majornavTextField.setBounds(113, 192,80, 25);
  320. syskeysTextField=

    new

    JTextField();
  321. syskeysTextField.setBounds(113, 219,80, 25);
  322. flipTextField=

    new

    JTextField();
  323. flipTextField.setBounds(113, 246,80, 25);
  324. anyeventTextField=

    new

    JTextField();
  325. anyeventTextField.setBounds(113, 273,80, 25);
  326. throttleTextField=

    new

    JTextField();
  327. throttleTextField.setBounds(113, 300,80, 25);
  328. seedTextField=

    new

    JTextField();
  329. seedTextField.setBounds(113, 327,80, 25);
  330. timesTextField=

    new

    JTextField();
  331. timesTextField.setBounds(113,354,80, 25);
  332. jCheckBox=

    new

    JCheckBox("Ignore Crash");
  333. jCheckBox.setBounds(202, 30,170, 25);
  334. jCheckBox2=

    new

    JCheckBox("Ignore Timeout");
  335. jCheckBox2.setBounds(202, 57,170, 25);
  336. jCheckBox3=

    new

    JCheckBox("Ignore Security Exception");
  337. jCheckBox3.setBounds(202, 84,170, 25);
  338. jCheckBox4=

    new

    JCheckBox("Kill Process After Error");
  339. jCheckBox4.setBounds(202, 111,170, 25);
  340. jCheckBox5=

    new

    JCheckBox("Monitor Native Crash");
  341. jCheckBox5.setBounds(202, 138,170, 25);
  342. monkeyConfigLable=

    new

    JButton("Monkey一鍵配置選擇");
  343. monkeyConfigLable.setBounds(205,170,170, 25);
  344. monkeyComboBox=

    new

    JComboBox<>();
  345. monkeyComboBox.setBounds(375,170,150, 25);
  346. startConfigWrite=

    new

    JButton("配置一鍵寫入");
  347. startConfigWrite.setBounds(527,170,150, 25);
  348. button=

    new

    JButton("Excute Monkey");
  349. button.setBounds(205, 250,150, 25);
  350. button2=

    new

    JButton("Stop Monkey");
  351. button2.setBounds(360,250,150, 25);
  352. }
  353. private

    void

    addView (){
  354. add(touchLable);
  355. add(motionLable);
  356. add(appSwitchLable);
  357. add(throttle);
  358. add(seedLable);
  359. add(timesLable);
  360. add(pinchzoomLable);
  361. add(trackballLable);
  362. add(navLable);
  363. add(majornavLable);
  364. add(syskeysLable);
  365. add(flipLable);
  366. add(anyeventLable);
  367. add(button);
  368. add(button2);
  369. add(touchTextField);
  370. add(motionTextField);
  371. add(appswtichTextField);
  372. add(throttleTextField);
  373. add(seedTextField);
  374. add(timesTextField);
  375. add(pinchzoomTextField);
  376. add(trackballTextField);
  377. add(navTextField);
  378. add(majornavTextField);
  379. add(syskeysTextField);
  380. add(flipTextField);
  381. add(anyeventTextField);
  382. add(jCheckBox);
  383. add(jCheckBox2);
  384. add(jCheckBox3);
  385. add(jCheckBox4);
  386. add(jCheckBox5);
  387. add(monkeyConfigLable);
  388. add(monkeyComboBox);
  389. add(startConfigWrite);
  390. }
  391. }

10.主類代碼

[java] view plain copy

  1. package

    MonkeyTool;
  2. import

    java.awt.Container;
  3. import

    javax.swing.JFrame;
  4. import

    javax.swing.WindowConstants;
  5. public

    class

    MonkeyMain

    extends

    JFrame {
  6. public

    MonkeyMain(){
  7. setTitle("MonkeyTool");
  8. setLayout(

    null

    );
  9. setBounds(0,0,720,500);
  10. JpanelDevices jpanelDevices=

    new

    JpanelDevices();
  11. JpanelMonkey jpanelMonkey=

    new

    JpanelMonkey(jpanelDevices);
  12. Container container=getContentPane();
  13. container.add(jpanelDevices);
  14. container.add(jpanelMonkey);
  15. setLayout(

    null

    );
  16. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  17. setVisible(

    true

    );
  18. }
  19. public

    static

    void

    main(String []args){
  20. new

    MonkeyMain();
  21. }
  22. }

Monkey可視化工具開發

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

Tengine 安裝配置全過程
打開和寫入文件(fopen和fopen s)

TAG:程序員小新人學習 |