1. package lecturer;
  2. import java.lang.*;
  3. import java.nio.Buffer;
  4. import java.util.Random;
  5. import java.io.File;
  6. import com.android.uiautomator.testrunner.UiAutomatorTestCase;
  7. import android.R.string;
  8. import android.os.RemoteException;
  9. import com.android.uiautomator.core.UiObject;
  10. import com.android.uiautomator.core.UiDevice;
  11. import com.android.uiautomator.core.UiSelector;
  12. import com.android.uiautomator.core.UiScrollable;
  13. import com.android.uiautomator.core.UiObjectNotFoundException;
  14. public class Lecturer extends UiAutomatorTestCase {
  15. public String Random_text(){ //生成随机手机字符串方法
  16. String a[] = {"a","b","c","d","e","f","g","h","w","j","k","l","n","m","o","p","q","r","s","t","u","v","w","x","Y","z"};
  17. String A[] = {"A","B","C","D","E","F","G","H","W","J","K","L","N","M","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
  18. String symbol[] = {"!","@","#","$","%","^","&","*","(",")","+","=","-",";","'","?","~","|","[","]","{","}","`"};
  19. String digital[] = {"1","2","3","4","5","6","7","8","9","0"};
  20. String set[][] = {a,A,symbol,digital};
  21. Random length = new Random();
  22. StringBuffer buffer = new StringBuffer();
  23. int length_a = length.nextInt(21)+1;
  24. for(int i=0;i<length_a;i++){
  25. Random random = new Random();
  26. Random random2 = new Random();
  27. int select = random.nextInt(4);
  28. if (set[select].equals(a)){
  29. buffer.append(set[select][random2.nextInt(26)]);
  30. }
  31. else if(set[select].equals(A)){
  32. buffer.append(set[select][random2.nextInt(26)]);
  33. }
  34. else if(set[select].equals(symbol)){
  35. buffer.append(set[select][random2.nextInt(23)]);
  36. }
  37. else if(set[select].equals(digital)){
  38. buffer.append(set[select][random2.nextInt(10)]);
  39. }
  40. }
  41. return buffer.toString();
  42. }
  43. public String Random_Value(){ //生成随机数值的方法
  44. String Value[] = {"1","2","3","4","5","6","7","8","9","0"};
  45. StringBuffer Value_buffe =new StringBuffer();
  46. for(int j=0; j<12;j++){
  47. Random Value_Random = new Random();
  48. Value_buffe.append(Value[Value_Random.nextInt(10)]);
  49. }
  50. return Value_buffe.toString();
  51. }
  52. public void Screenshot(String name) { //截取当前屏幕图片的方法
  53. File file = new File("/mnt/sdcard/log"+name+".png");
  54. UiDevice.getInstance().takeScreenshot(file);
  55. }
  56. public void startapp(String ojb){ //启动apk的方法
  57. StringBuffer app = new StringBuffer();
  58. app.append("am start -n");
  59. app.append(ojb);
  60. try{
  61. Process process = Runtime.getRuntime().exec(app.toString());
  62. process.waitFor();
  63. }
  64. catch (Exception e) {
  65. e.printStackTrace();
  66. System.out.println("NG! Start apk failed....");
  67. }
  68. }
  69. public void test_lecturer()throws UiObjectNotFoundException,RemoteException {
  70. for(int k=0;k<20;k++){
  71. try{
  72. UiDevice.getInstance().pressBack();
  73. sleep(1000);
  74. UiDevice.getInstance().sleep();
  75. sleep(2000);
  76. UiDevice.getInstance().wakeUp();
  77. }catch(RemoteException e1) {
  78. e1.printStackTrace();
  79. System.out.println("NO:"+k+"....wakeup Exception!.... ");
  80. sleep(2000);
  81. UiDevice.getInstance().sleep();
  82. sleep(2000);
  83. UiDevice.getInstance().wakeUp();
  84. }
  85. try{
  86. UiObject Unlock = new UiObject(new UiSelector().resourceId("com.android.systemui:id/preview_container")
  87. .className("android.widget.FrameLayout"));
  88. assertTrue("Unlock fail!...", Unlock.exists());
  89. Unlock.swipeUp(20);//滑屏解锁
  90. sleep(1000);
  91. }catch(UiObjectNotFoundException e2){
  92. e2.printStackTrace();
  93. System.out.println("NO:"+k+"....Unlock Exception!.... ");
  94. Screenshot("Unlock_Exception"+k);
  95. sleep(2000);
  96. UiDevice.getInstance().sleep();
  97. sleep(2000);
  98. UiDevice.getInstance().wakeUp();
  99. sleep(2000);
  100. UiObject Unlock = new UiObject(new UiSelector().resourceId("com.android.systemui:id/preview_container")
  101. .className("android.widget.FrameLayout"));
  102. assertTrue("Unlock fail!...", Unlock.exists());
  103. Unlock.swipeUp(20);//滑屏解锁
  104. }
  105. startapp("com.android.contacts/com.android.contacts.activities.PeopleActivity");//启动联系人
  106. sleep(2000);
  107. try{
  108. UiObject new_contact = new UiObject(new UiSelector().resourceId("com.android.contacts:id/floating_action_button")
  109. .description("添加新联系人"));
  110. assertTrue("new_contact fail!...",new_contact.exists());
  111. new_contact.clickAndWaitForNewWindow();
  112. }catch (UiObjectNotFoundException e3) {
  113. e3.printStackTrace();
  114. System.out.println("NO:"+k+"....new_contact Exception!.... ");
  115. Screenshot("new_contact"+k);
  116. UiDevice.getInstance().pressBack();
  117. sleep(1000);
  118. UiDevice.getInstance().pressBack();
  119. sleep(1000);
  120. UiDevice.getInstance().pressBack();
  121. sleep(1000);
  122. }
  123. try{
  124. UiObject Name = new UiObject(new UiSelector().text("姓名").className("android.widget.EditText"));
  125. assertTrue("Name fail!...", Name.exists());
  126. Name.click();
  127. Name.setText(Random_text());
  128. sleep(1000);
  129. }catch (UiObjectNotFoundException e4) {
  130. e4.printStackTrace();
  131. System.out.println("NO:"+k+"....Enter_the_contact_name Exception!.... ");
  132. Screenshot("Enter_the_contact_name"+k);
  133. UiDevice.getInstance().pressBack();
  134. sleep(1000);
  135. UiDevice.getInstance().pressBack();
  136. sleep(1000);
  137. UiDevice.getInstance().pressBack();
  138. sleep(1000);
  139. }
  140. try{
  141. UiObject Pinyin_of_name = new UiObject(new UiSelector().text("姓名拼音").className("android.widget.EditText"));
  142. assertTrue("Pinyin_of_name fail!...",Pinyin_of_name.exists());
  143. Pinyin_of_name.click();
  144. Pinyin_of_name.setText(Random_text());
  145. sleep(1000);
  146. }catch (UiObjectNotFoundException e5) {
  147. e5.printStackTrace();
  148. System.out.println("NO:"+k+"....Enter the name Pinyin Exception!.... ");
  149. Screenshot("Pinyin_of_name"+k);
  150. UiDevice.getInstance().pressBack();
  151. sleep(1000);
  152. UiDevice.getInstance().pressBack();
  153. sleep(1000);
  154. UiDevice.getInstance().pressBack();
  155. sleep(1000);
  156. }
  157. try{
  158. UiObject nickname = new UiObject(new UiSelector().text("昵称").className("android.widget.EditText"));
  159. assertTrue("nickname fail!...",nickname.exists());
  160. nickname.click();
  161. nickname.setText(Random_text());
  162. sleep(1000);
  163. }catch (UiObjectNotFoundException e6) {
  164. e6.printStackTrace();
  165. System.out.println("NO:"+k+"....Enter the nickname Exception!.... ");
  166. Screenshot("nickname"+k);
  167. UiDevice.getInstance().pressBack();
  168. sleep(1000);
  169. UiDevice.getInstance().pressBack();
  170. sleep(1000);
  171. UiDevice.getInstance().pressBack();
  172. sleep(1000);
  173. }
  174. try{
  175. UiScrollable Scroll_contact = new UiScrollable(new UiSelector().resourceId("com.android.contacts:id/contact_editor_fragment")
  176. .className("android.widget.ScrollView")); //定位滚动元素
  177. assertTrue("Scroll_contact fail!...",Scroll_contact.exists());
  178. UiObject number = new UiObject(new UiSelector().text("电话").className("android.widget.EditText"));
  179. Scroll_contact.scrollIntoView(number);
  180. sleep(1000);
  181. number.click();
  182. number.setText(Random_Value());
  183. sleep(1000);
  184. }catch(UiObjectNotFoundException e7){
  185. e7.printStackTrace();
  186. System.out.println("NO:"+k+"....Scroll and enter the number Exception!.... ");
  187. Screenshot("number"+k);
  188. UiDevice.getInstance().pressBack();
  189. sleep(1000);
  190. UiDevice.getInstance().pressBack();
  191. sleep(1000);
  192. UiDevice.getInstance().pressBack();
  193. sleep(1000);
  194. }
  195. try{
  196. UiObject determine = new UiObject(new UiSelector().resourceId("com.android.contacts:id/menu_done")
  197. .className("android.widget.TextView"));
  198. assertTrue("determine fail! ...",determine.exists());
  199. determine.clickAndWaitForNewWindow();
  200. }catch (UiObjectNotFoundException e8) {
  201. e8.printStackTrace();
  202. System.out.println("NO:"+k+"....determine Exception!.... ");
  203. Screenshot("determine"+k);
  204. UiDevice.getInstance().pressBack();
  205. sleep(1000);
  206. UiDevice.getInstance().pressBack();
  207. sleep(1000);
  208. UiDevice.getInstance().pressBack();
  209. sleep(1000);
  210. }
  211. sleep(1000);
  212. Screenshot("Screenshot_contacts"+k);
  213. sleep(2000);
  214. UiDevice.getInstance().pressBack();
  215. sleep(1000);
  216. UiDevice.getInstance().pressBack();
  217. sleep(1000);
  218. UiDevice.getInstance().pressBack();
  219. sleep(1000);
  220. }
  221. }
  222. }

UiAutomator 代码记录: 随机创建新联系人的更多相关文章

  1. EBS管理员为供应商创建新联系人流程

    管理员为供应商创建新联系人流程 /oracle/apps/pos/supplier/webui/ByrAddCntctPG oracle.apps.pos.supplier.webui.ByrAddC ...

  2. uiautomator 代码记录 : 随机发送短信

    package sms_test; import java.lang.*; import java.util.Random; import javax.microedition.khronos.egl ...

  3. 【OpenGL】代码记录01创建窗口

    创建空窗口: #include<iostream> // GLEW #define GLEW_STATIC #include <GL/glew.h> // GLFW #incl ...

  4. uiautomator 代码记录 :BT接收测试

    package rom; import java.lang.*; import java.util.Random; import java.io.File; import com.android.ui ...

  5. uiautomator 代码记录 : BT发送测试

    package rom; import java.lang.*; import java.util.Random; import java.io.File; import com.android.ui ...

  6. UiAutomator 代码记录 : 遍历桌面

    package test_one; import java.lang.*; import java.io.File; import com.android.uiautomator.core.UiDev ...

  7. Ubuntu创建新用户的正确姿势

    作者按:因为教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步<Ubuntu 创建新用户的正确姿势>原文地址.更欢迎来我的小站看更多原创内容:godbmw.com,进行&q ...

  8. git 创建新项目,下载工程,合并和更新工程简单应用记录

    以前使用SVN很顺手,现在公司使用git来管理代码,因此学习git的基本使用. 一.首先介绍下SVN和git的简单比较: SVN是使用得最多的版本控制管理工具. 1.是一个集中式的版本管理工具.所有的 ...

  9. 【neo4j】文件管理路径、数据备份、创建新数据库、导入数据等操作记录

    neo4j一般的配置路径如下 一.备份数据 使用neo4j-admin命令. 首先,先找到数据的存储路径,然后关闭数据库. 关闭数据库的语句如下: #切换到/bin目录下 ./neo4j stop 然 ...

随机推荐

  1. linux系统中安装JDK 查看安装的ava版本

    一.安装JDK 1.在/usr/目录下创建java目录 [root@localhost ~]# mkdir/usr/java[root@localhost ~]# cd /usr/java 2.下载j ...

  2. RedHat6.5创建本地yum源

    拷贝Linux操作系统的rhel-server-6.5-x86_64-dvd.iso镜像到装好的Linux中: #执行挂载命令 mount –o loop /media/rhel-server-6.5 ...

  3. linux环境下python的pdb调试方法

    一些常用指令: h(elp) [comman]  #打印可用指令及帮助信息 r(eturn)  #运行代码直到下一个断点或当前函数返回 b(reak) [[filename:]lineno | fun ...

  4. Delphi:窗体的扩展样式GWL_EXSTYLE用于SetWindowLong

    SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_TRANSPARENT or WS_EX_ ...

  5. influxDB 0.9 C# 读写类

    influxDB 0.9 C# 读写类   目前influxdb官网推荐的C#读写类是针对0.8版本的,截至本文写作之前,尚未发现有针对0.9的读写类. 我使用influxdb的是用于保存服务器的运行 ...

  6. Mysql 性能优化6【重要】 索引优化

    b tree索引 myisam 是通过物理位置来查找引用行的 innodb 是通过主键来查找引用行的 索引优化策略 b-tree索引对数据长度有限制,所以text等比较长的列可以建立前缀索引 btre ...

  7. win10和ubuntu16.04双系统时间同步

    在win10安装了ubuntu双系统,发现在两个系统见时间相差8个小时,这是由于windows和和ubuntu对于从主板取得时间后的处理方式不同,如果你把位置设为上海,ubuntu总是把主板时间当作u ...

  8. PREV-5_蓝桥杯_错误票据

    问题描述 某涉密单位下发了某种票据,并要在年终全部收回. 每张票据有唯一的ID号.全年所有票据的ID号是连续的,但ID的开始数码是随机选定的. 因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成 ...

  9. http系列(一)

    一.关于Url URI由URL和URN组成,URI即统一资源标识符,URL即统一资源定位符,URN即统一资源名称. 现在最常用的是URL. 二.http请求/响应报文 请求报文:请求行.请求头部.空行 ...

  10. 写在vue总结之前(二)

    都说要快速学会一个技术(会使用),比如一个框架,在实际的工作中做相关的项目是最快的学习方式.而为什么在实际的工作项目中去学习是最快的方式?个人的体会是,在实际的工作项目中,很多功能的实现是你不得不做的 ...