51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2318|回复: 3
打印 上一主题 下一主题

android UiAutomator基本api的二次封装

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2018-4-10 15:10:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本人在使用UiAutomator做测试的时候,封装了很多方法,由于之前的文章并没有分享这些封装方法,导
致阅读不畅。本来打算再把图像识别和辅助类写完在分享,鉴于已经离职,UI这块很长时间不太会更新
代码了,就把所有的封装方法都分享出来了。里面有些过时的,暂时无用的大家可以忽略。

下面这个是对UiAutomator基本方法的封装,还有一个在测试报告生成的时候的基本方法封装,还有些辅
助类,改天我整理一下也发出来。
  1. package source;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.text.SimpleDateFormat;  
  8. import java.util.ArrayList;  
  9. import java.util.Date;  
  10. import java.util.List;  
  11. import java.util.Random;  
  12. import com.android.uiautomator.core.Configurator;  
  13. import com.android.uiautomator.core.UiDevice;  
  14. import com.android.uiautomator.core.UiObject;  
  15. import com.android.uiautomator.core.UiObjectNotFoundException;  
  16. import com.android.uiautomator.core.UiScrollable;  
  17. import com.android.uiautomator.core.UiSelector;  
  18. import com.android.uiautomator.testrunner.UiAutomatorTestCase;  
  19. import android.graphics.Bitmap;  
  20. import android.graphics.BitmapFactory;  
  21. import android.graphics.Color;  
  22. import android.graphics.Point;  
  23. import android.graphics.Rect;  
  24. import android.graphics.Bitmap.CompressFormat;  
  25. import android.os.RemoteException;  
  26. import android.view.KeyEvent;  
  27. import jp.jun_nama.test.utf7ime.helper.Utf7ImeHelper;  
  28. /**
  29. * @author ··-·尘
  30. * @E-mail:Fhaohaizi@163.com
  31. * @version 创建时间:2017年8月18日 上午10:53:24
  32. * @alter 修改时间:2017年9月12日 09:20:29
  33. * 类说明:基本api封装
  34. */  
  35. @SuppressWarnings("deprecation")  
  36. public class UiaLibrary extends UiAutomatorTestCase{  
  37.     public String LINE = "\r\n";  
  38. //  public static UiaLibrary library = null;  
  39. //  public static UiaLibrary getInstance() {  
  40. //      library = new UiaLibrary();  
  41. //      return library;  
  42. //  }  
  43.     public void swipeLeft() {//左滑  
  44.         int y = UiDevice.getInstance().getDisplayHeight();  
  45.         int x = UiDevice.getInstance().getDisplayWidth();  
  46.         UiDevice.getInstance().swipe(x-100, y/2, 100, y/2, 20);  
  47.         sleep(150);  
  48.         }  
  49.     public void swipeRight() {//右滑  
  50.         int y = UiDevice.getInstance().getDisplayHeight();  
  51.         int x = UiDevice.getInstance().getDisplayWidth();  
  52.         UiDevice.getInstance().swipe(100, y/2, x-100, y/2, 20);  
  53.         sleep(150);  
  54.         }  
  55.     public void swipeDown() {//下滑  
  56.         int y = UiDevice.getInstance().getDisplayHeight();  
  57.         int x = UiDevice.getInstance().getDisplayWidth();  
  58.         UiDevice.getInstance().swipe(x/2, 200, x/2, y-200, 20);  
  59.         sleep(150);  
  60.         }     
  61.     public void swipeUp() {//上滑  
  62.         int y = UiDevice.getInstance().getDisplayHeight();  
  63.         int x = UiDevice.getInstance().getDisplayWidth();  
  64.         UiDevice.getInstance().swipe(x/2, y-200, x/2, 200, 20);  
  65.         sleep(150);  
  66.         }  
  67.     public void swipUpLittle() {//上滑一点点  
  68.         int x = UiDevice.getInstance().getDisplayWidth()/2;  
  69.         int y = UiDevice.getInstance().getDisplayHeight()/2;  
  70.         UiDevice.getInstance().swipe(x, y+150, x, y-150, 20);  
  71.         sleep(150);  
  72.     }  
  73.     public void swipDownLittle() {//下拉一点点  
  74.         int x = UiDevice.getInstance().getDisplayWidth()/2;  
  75.         int y = UiDevice.getInstance().getDisplayHeight()/2;  
  76.         UiDevice.getInstance().swipe(x, y-150, x, y+150, 20);  
  77.         sleep(150);  
  78.     }  
  79.     public String getNow() {//获取当前时间  
  80.         Date time = new Date();  
  81.         SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  82.         String c = now.format(time);  
  83.         return c;  
  84.         }  
  85.     public void screenShot() {//截图name+time  
  86.         String name = getName();  
  87.         File file = new File("/mnt/sdcard/123/");  
  88.         if (!file.exists()) {  
  89.             file.mkdirs();  
  90.             }  
  91.         File files = new File(file.toString()+"/"+getDayHourMinute()+name+".png");  
  92.         UiDevice.getInstance().takeScreenshot(files);  
  93.         output("默认截图成功!");  
  94.         }  
  95.     //压缩图片  
  96.     public void compressPictureToJpeg(String oldPath, File newFile) throws FileNotFoundException {  
  97.         Bitmap bitmap = BitmapFactory.decodeFile(oldPath);//创建并实例化bitmap对象  
  98.         FileOutputStream out = new FileOutputStream(newFile);//创建文件输出流  
  99.         bitmap.compress(CompressFormat.JPEG, 100, out);//将图片转化为jpeg格式输出  
  100.     }  
  101.     //截取某个控件的图像  
  102.     public Bitmap getBitmapByResourceId(String id) throws UiObjectNotFoundException {  
  103.         Rect rect = getUiObjectByResourceId(id).getVisibleBounds();//获取控件的rect对象  
  104.         String path = screenShot("test");//截图  
  105.         Bitmap bitmap = BitmapFactory.decodeFile(path);//创建并实例化bitmap对象  
  106.         bitmap = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height());//截取bitmap实例  
  107.         return bitmap;  
  108.     }  
  109.     //获取某一坐标点的颜色值  
  110.     public int getColorPixel(int x, int y) {  
  111.         screenShot("test");//截图  
  112.         String path = "/mnt/sdcard/123/test.png";  
  113.         Bitmap bitmap = BitmapFactory.decodeFile(path);//新建并实例化bitmap对象  
  114.         int color = bitmap.getPixel(x, y);//获取坐标点像素颜色  
  115. //      output(color);//输出颜色值  
  116.         return color;  
  117.     }  
  118.     public int getRedPixel(int x, int y) {  
  119.         screenShot("test");//截图  
  120.         String path = "/mnt/sdcard/123/test.png";  
  121.         Bitmap bitmap = BitmapFactory.decodeFile(path);//新建并实例化bitmap对象  
  122.         int color = bitmap.getPixel(x, y);//获取坐标点像素颜色  
  123. //      output(color);//输出颜色值  
  124.         int red = Color.red(color);  
  125.         return red;  
  126.     }  
  127.     public int getGreenPixel(int x, int y) {  
  128.         screenShot("test");//截图  
  129.         String path = "/mnt/sdcard/123/test.png";  
  130.         Bitmap bitmap = BitmapFactory.decodeFile(path);//新建并实例化bitmap对象  
  131.         int color = bitmap.getPixel(x, y);//获取坐标点像素颜色  
  132. //      output(color);//输出颜色值  
  133.         int green = Color.green(color);  
  134.         return green;  
  135.     }  
  136.     public int getBluePixel(int x, int y) {  
  137.         screenShot("test");//截图  
  138.         String path = "/mnt/sdcard/123/test.png";  
  139.         Bitmap bitmap = BitmapFactory.decodeFile(path);//新建并实例化bitmap对象  
  140.         int color = bitmap.getPixel(x, y);//获取坐标点像素颜色  
  141. //      output(color);//输出颜色值  
  142.         int blue = Color.blue(color);  
  143.         return blue;  
  144.     }  
  145.     public int[] getRGBcolorPixel(int x, int y) {  
  146.         screenShot("testDemo");  
  147.         String path = "/mnt/sdcard/123/testDemo.png";  
  148.         Bitmap bitmap = BitmapFactory.decodeFile(path);  
  149.         int color = bitmap.getPixel(x, y);  
  150.         int red = Color.red(color);  
  151.         int green = Color.green(color);  
  152.         int blue = Color.blue(color);  
  153.         int[] rgb = {red, green, blue};  
  154.         return rgb;  
  155.     }  
  156.     //根据颜色判断状态  
  157.     public boolean isBlue(UiObject uiObject) throws UiObjectNotFoundException {  
  158.         screenShot("test");//截图  
  159.         String path = "/mnt/sdcard/123/test.png";  
  160.         Bitmap bitmap = BitmapFactory.decodeFile(path);//新建并实例化bitmap对象  
  161.         Rect rect = uiObject.getVisibleBounds();  
  162.         int x = rect.left;  
  163.         int xx = rect.right;  
  164.         int y = rect.top;  
  165.         int yy = rect.bottom;  
  166.         List<Integer> blueColor = new ArrayList<Integer>();  
  167.         for (int i = x; i < xx; i++) {  
  168.             for (int k = y;k < yy;k++) {  
  169.                 int color = bitmap.getPixel(i, k);//获取坐标点像素颜色  
  170.                 int red = Color.blue(color);  
  171.                 blueColor.add(red);  
  172.             }  
  173.         }  
  174.         int sum = 0;  
  175.         for (int i = 0;i<blueColor.size();i++) {  
  176.             sum += blueColor.get(i);  
  177.         }  
  178. //      output(sum/blueColor.size());  
  179.         return sum/blueColor.size() > 200?true:false;  
  180.     }  
  181.     /*
  182.      * 图像对比
  183.      * 默认图像宽高一致
  184.      */  
  185.     public boolean comparePicture(String path1, String path2, double limit) {  
  186.         Bitmap bitmap1 = BitmapFactory.decodeFile(path1);//创建并初始化bitmap对象  
  187.         Bitmap bitmap2 = BitmapFactory.decodeFile(path2);//创建并初始化bitmap对象  
  188.         int width = bitmap1.getWidth();//获取宽  
  189.         int height = bitmap1.getHeight();//获取高  
  190.         int total = 0;//统计相同次数  
  191.         int times = 0;//统计总次数  
  192.         //遍历像素点的颜色值,节省时间每次递增5个像素点  
  193.         for (int x = 0;x < width;x +=3) {  
  194.             for (int y = 0; y < height; y +=3) {  
  195.                 int oldPic = bitmap1.getPixel(x, y);//获取颜色值  
  196.                 int newPic = bitmap2.getPixel(x, y);//获取颜色值  
  197. //              int differ = Math.abs(ss - dd);//计算绝对差  
  198.                 times++;  
  199.                 if (oldPic == newPic) {//如果相等,则认为相同  
  200.                     total++;  
  201.                 }  
  202.             }  
  203.         }  
  204.         double differ = total*1.0/times;  
  205.         output(differ);  
  206.         return differ > 0.99?true:false;//返回统计结果  
  207.     }  
  208.     //获取视频播放进度条  
  209.     public double getVideoProgress(Bitmap bitmap) {  
  210.         int height = bitmap.getHeight();  
  211.         int width = bitmap.getWidth();  
  212.         List<Integer> date = new ArrayList<Integer>();  
  213.         for (int i = 0;i < width; i++) {  
  214.             int color = bitmap.getPixel(i, height / 2);  
  215.             int red = Color.red(color);  
  216. //          output(red);  
  217.             date.add(red);  
  218.             }  

复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2018-4-10 15:10:37 | 只看该作者
  1. int date1 = 0,date2 = 0,date3 = 0,date4 = 0;
  2. int status1 = 0,status2 = 0;
  3. for (int i = 1;i < date.size() - 1;i++) {
  4. if (date.get(i) == 255 && status1 == 0) {
  5. status1++;
  6. date1 = i;
  7. }
  8. if (date.get(i) == 238 && status2 == 0) {
  9. status2++;
  10. date2 = i;
  11. }
  12. if (date.get(i + 1) < 238 && date.get(i) == 238) {
  13. date3 = i;
  14. }
  15. if (date.get(i + 1) < 165 && date.get(i) == 165) {
  16. date4 = i;
  17. }
  18. }
  19. // output(date1, date2, date3, date4);
  20. // output((date2 + date3 - date1 * 2.00)/(date4 - date1)/2);
  21. return (date2 + date3 - date1 * 2.00)/(date4 - date1)/2;
  22. }
  23. public String getDayHourMinute() {//获取日期小时分钟
  24. Date time = new Date();
  25. SimpleDateFormat format = new SimpleDateFormat("dd-HH-mm");
  26. String name = format.format(time);
  27. return name;
  28. }
  29. public String screenShot(String name) {//截图并命名
  30. File file = new File("/mnt/sdcard/123/");
  31. if (!file.exists()) {
  32. file.mkdirs();
  33. }
  34. File files = new File("/mnt/sdcard/123/"+name+".png");
  35. UiDevice.getInstance().takeScreenshot(files);
  36. output(name + ".png 截图成功!");
  37. String path = "/mnt/sdcard/123/" + name + ".png";
  38. return path;
  39. }
  40. public String screenShot(int num) {//截图并命名
  41. File file = new File("/mnt/sdcard/123/");
  42. if (!file.exists()) {
  43. file.mkdirs();
  44. }
  45. File files = new File("/mnt/sdcard/123/"+num+".png");
  46. UiDevice.getInstance().takeScreenshot(files);
  47. output(num + ".png 截图成功!");
  48. String path = "/mnt/sdcard/123/" + num + ".png";
  49. return path;
  50. }
  51. public void circle(int x, int y, int r) {//画圆的方法
  52. double d = (double) (Math.PI/30);//角度
  53. double[] xxx = new double[61];
  54. for(int i=0;i<61;i++){
  55. xxx[i]=Math.cos(i*d);
  56. }
  57. //获取x坐标
  58. double[] yyy = new double[61];
  59. for(int i=1;i<61;i++){
  60. yyy[i]=Math.sin(i*d);
  61. }
  62. //获取y坐标
  63. int[] xxx1 = new int[61];
  64. for(int i=0;i<61;i++){
  65. xxx1[i]=(int) (xxx[i]*200);
  66. }
  67. //转化坐标值类型
  68. int[] yyy1 = new int[61];
  69. for(int i=0;i<61;i++){
  70. yyy1[i]=(int) (yyy[i]*200);
  71. }
  72. //转化坐标值类型
  73. Point[] p = new Point[61];
  74. for(int i=0;i<61;i++){
  75. p[i]=new Point();
  76. p[i].x = xxx1[i]+x;
  77. p[i].y = y-yyy1[i]+50;
  78. }
  79. //建立点数组
  80. UiDevice.getInstance().swipe(p, 2);
  81. }
  82. public void heart(int x, int y,int r) {//画心形的方法
  83. double d = (double) (Math.PI/30);
  84. double[] angle = new double[61];//设置角度差
  85. for(int i=0;i<61;i++){
  86. angle[i]=i*d;
  87. }
  88. //建立一个角度差double数组
  89. double[] ox = new double[61];
  90. for(int i=0;i<61;i++){
  91. ox[i]= r*(2*Math.cos(angle[i])-Math.cos(2*angle[i]));
  92. }
  93. //计算x坐标
  94. double[] oy = new double[61];
  95. for(int i=0;i<61;i++){
  96. oy[i]=r*(2*Math.sin(angle[i])-Math.sin(2*angle[i]));
  97. }
  98. //计算y坐标
  99. Point[] heart = new Point[61];
  100. for(int i=0;i<61;i++){
  101. heart[i] = new Point();
  102. heart[i].x = (int) oy[i]+x;
  103. heart[i].y = -(int) ox[i]+y;
  104. }
  105. //建立一个点数组,这里坐标一定要转化一下,不然是倒着的心形
  106. UiDevice.getInstance().swipe(heart, 2);
  107. }
  108. public UiObject getUiObjectByText(String text) {//通过文本获取控件
  109. return new UiObject(new UiSelector().text(text));
  110. }
  111. public UiObject getUiObjectByTextContains(String text) {
  112. return new UiObject(new UiSelector().textContains(text));
  113. }

  114. //通过text开始文字查找控件
  115. public UiObject getUiObjectByStartText(String text) {
  116. return new UiObject(new UiSelector().textStartsWith(text));
  117. }
  118. public UiObject getUiObjectByStartDesc(String desc) {
  119. return new UiObject(new UiSelector().descriptionStartsWith(desc));
  120. }
  121. public UiObject getUiObjectByTextClassName(String text,String classname) {//通过文本和类名获取控件
  122. return new UiObject(new UiSelector().text(text).className(classname));
  123. }
  124. public UiObject getUiObjectByTextResourceId(String text, String id) {//通过文本和id获取对象
  125. return new UiObject(new UiSelector().text(text).resourceId(id));
  126. }
  127. public UiObject getUiObjectByResourceIdClassName(String id, String type) {
  128. return new UiObject(new UiSelector().resourceId(id).className(type));
  129. }
  130. public UiObject getUiObjectByResourceId(String id) {//通过资源ID获取控件
  131. return new UiObject(new UiSelector().resourceId(id));
  132. }
  133. public UiObject getUiObjectByDesc(String desc) {//通过desc获取控件
  134. return new UiObject(new UiSelector().description(desc));
  135. }
  136. public UiObject getUiObjectByStartDescContains(String desc) {
  137. return new UiObject(new UiSelector().descriptionContains(desc));
  138. }
  139. public UiObject getUiObjectByDescContains(String desc) {
  140. return new UiObject(new UiSelector().descriptionContains(desc));
  141. }
  142. public UiObject getUiObjectByClassName(String type) {//通过classname获取控件
  143. return new UiObject(new UiSelector().className(type));
  144. }
  145. public UiObject getUiObjectByResourceIdIntance(String id, int instance) {//通过id和instance获取控件
  146. return new UiObject(new UiSelector().resourceId(id).instance(instance));
  147. }
  148. //长按控件
  149. public void longclickUiObectByResourceId(String id) throws UiObjectNotFoundException {
  150. int x = getUiObjectByResourceId(id).getBounds().centerX();
  151. int y = getUiObjectByResourceId(id).getBounds().centerY();
  152. UiDevice.getInstance().swipe(x, y, x, y, 300);//最后一个参数单位是5ms
  153. }
  154. public void longclickUiObectByDesc(String desc) throws UiObjectNotFoundException {
  155. int x = getUiObjectByDesc(desc).getBounds().centerX();
  156. int y = getUiObjectByDesc(desc).getBounds().centerY();
  157. UiDevice.getInstance().swipe(x, y, x, y, 300);//最后一个参数单位是5ms
  158. }
  159. public void longclickUiObectByText(String text) throws UiObjectNotFoundException {
  160. int x = getUiObjectByText(text).getBounds().centerX();
  161. int y = getUiObjectByText(text).getBounds().centerY();
  162. UiDevice.getInstance().swipe(x, y, x, y, 300);//最后一个参数单位是5ms
  163. }
  164. //点击中心
  165. public void clickCenter() {
  166. int x = UiDevice.getInstance().getDisplayWidth();
  167. int y = UiDevice.getInstance().getDisplayHeight();
  168. clickPiont(x/2, y/2);
  169. }
  170. public void writeText(String text) throws UiObjectNotFoundException{//输入文字
  171. getUiObjectByClassName("android.widget.EditText").setText(Utf7ImeHelper.e(text));
  172. }
  173. public UiScrollable getUiScrollabe() {//获取滚动控件
  174. return new UiScrollable(new UiSelector().scrollable(true));
  175. }
  176. public UiScrollable getUiScrollableByResourceId(String id) {//获取滚动对象
  177. return new UiScrollable(new UiSelector().scrollable(true).resourceId(id));
  178. }
  179. public void getChildByTextOfUiScrollableByClassName(String type, String text) throws UiObjectNotFoundException {
  180. getScrollableByClassName(type).getChildByText(new UiSelector().text(text), text).clickAndWaitForNewWindow();
  181. }
  182. public UiObject getUiObjectByResourIdIndex(String id, int index) {//通过ID和index获取控件
  183. return new UiObject(new UiSelector().resourceId(id).index(index));
  184. }
  185. public void randomClickOpiton() throws UiObjectNotFoundException {
  186. int num = getUiObjectByClassName("android.widget.ListView").getChildCount();
  187. int i = new Random().nextInt(num);
  188. getUiObjectByResourceIdIntance("com.gaotu100.superclass:id/simpleitemview_left_text", i).clickAndWaitForNewWindow();
  189. }
  190. public void outputBegin() {//输出开始
  191. System.out.println(getNow()+"..-. ...- 开始!");
  192. }
  193. public void outputNow() {//输出当前时间
  194. System.out.println(getNow());
  195. }
  196. public void outputOver() {//输出结束
  197. System.out.println(getNow()+"..-. ...- 结束!");
  198. }
  199. // public void output(String text) {//明显输出
  200. // System.out.println(text);
  201. // }
  202. // public void output(double num) {//明显输出
  203. // System.out.println(num);
  204. // }

复制代码


回复 支持 反对

使用道具 举报

该用户从未签到

3#
 楼主| 发表于 2018-4-10 15:11:09 | 只看该作者
  1. // public void output(int num) {//方法重载
  2. // System.out.println("===="+num+"====");
  3. // }
  4. //明显输出
  5. public void output(String text) {
  6. System.out.println(text);
  7. }
  8. public void output(String ...text) {//方法重载
  9. for (int i = 0; i < text.length; i++) {
  10. System.out.println("第"+ (i+1) + "个:"+ text[i]);
  11. }
  12. }
  13. public void output(long num) {
  14. System.out.println(num);
  15. }
  16. public void output(long ...num) {//方法重载
  17. for (int i = 0; i < num.length; i++) {
  18. System.out.println("第"+ (i+1) + "个:"+ num[i]);
  19. }
  20. }
  21. public void output(double num) {
  22. System.out.println(num);
  23. }
  24. public void output(double ...num) {//方法重载
  25. for (int i = 0; i < num.length; i++) {
  26. System.out.println("第"+ (i+1) + "个:"+ num[i]);
  27. }
  28. }
  29. public void output(int num) {
  30. System.out.println(num);
  31. }
  32. public void output(int ...num) {//方法重载
  33. for (int i = 0; i < num.length; i++) {
  34. System.out.println("第"+ (i+1) + "个:"+ num[i]);
  35. }
  36. }
  37. public void outpu(Object ...object) {
  38. for (int i = 0; i < object.length; i++) {
  39. System.out.println("第"+ (i+1) + "个:"+ object[i]);
  40. }
  41. }
  42. public void outpu(Object object) {
  43. System.out.println(object.toString());
  44. }
  45. public void pressTimes(int keyCode, int times) {//对于一个按键按多次
  46. for(int i=0;i<times;i++){
  47. sleep(200);
  48. UiDevice.getInstance().pressKeyCode(keyCode);
  49. }
  50. }
  51. public void waitForUiObjectByText(String text) {//等待对象出现
  52. // Date start = new Date();
  53. // boolean key = true;
  54. // while(key){
  55. // sleep(200);
  56. // UiObject it = new UiObject(new UiSelector().text(text));
  57. // if (it.exists()) {
  58. // key = false;
  59. // }
  60. // Date end = new Date();
  61. // long time = end.getTime() - start.getTime();
  62. // if (time>10000) {
  63. // output("超过10s没有出现!");
  64. // key = false;
  65. // }
  66. // }
  67. getUiObjectByText(text).waitForExists(10000);
  68. }
  69. public void waitForUiObjectByStartText(String text) {
  70. getUiObjectByStartText(text).waitForExists(10000);
  71. }
  72. //输出时间差
  73. public void outputTimeDiffer(Date start, Date end) {
  74. long time = end.getTime() - start.getTime();
  75. double differ = (double)time/1000;
  76. output("总计用时"+differ+"秒!");
  77. }
  78. //获取子控件点击
  79. public void getScrollChildByText(String text) throws UiObjectNotFoundException {
  80. UiObject child = getUiScrollabe().getChildByText(new UiSelector().text(text), text);
  81. child.clickAndWaitForNewWindow();
  82. }
  83. //通过classname获取滚动控件
  84. public UiScrollable getScrollableByClassName(String type) {
  85. return new UiScrollable(new UiSelector().scrollable(true).className(type));
  86. }
  87. public void waitForUiObjectByClassName(String type) throws UiObjectNotFoundException {//等待控件出现
  88. getUiObjectByClassName(type).waitForExists(10000);
  89. }
  90. public void waitForUiObjectByTextContains(String text) {//等待对象出现
  91. // Date start = new Date();
  92. // boolean key = true;
  93. // while(key){
  94. // sleep(1000);
  95. // UiObject it = new UiObject(new UiSelector().textContains(text));
  96. // if (it.exists()) {
  97. // key = false;
  98. // }
  99. // Date end = new Date();
  100. // long time = end.getTime() - start.getTime();
  101. // if (time>10000) {
  102. // output("超过10s没有出现!");
  103. // key = false;
  104. // }
  105. // }
  106. getUiObjectByText(text).waitForExists(10000);
  107. }
  108. public void waitForUiObjectByDesc(String desc) {//等待对象出现
  109. // Date start = new Date();
  110. // boolean key = true;
  111. // while(key){
  112. // sleep(1000);
  113. // UiObject it = new UiObject(new UiSelector().description(desc));
  114. // if (it.exists()) {
  115. // key = false;
  116. // }
  117. // Date end = new Date();
  118. // long time = end.getTime() - start.getTime();
  119. // if (time>10000) {
  120. // output("超过10s没有出现!");
  121. // key = false;
  122. // }
  123. // }
  124. getUiObjectByDesc(desc).waitForExists(10000);
  125. }
  126. public void waitForUiObjectByResourceId(String id) {//等待对象出现
  127. // Date start = new Date();
  128. // boolean key = true;
  129. // while(key){
  130. // sleep(1000);
  131. // UiObject it = new UiObject(new UiSelector().resourceId(id));
  132. // if (it.exists()) {
  133. // key = false;
  134. // }
  135. // Date end = new Date();
  136. // long time = end.getTime() - start.getTime();
  137. // if (time>10000) {
  138. // output("超过10s没有出现!");
  139. // key = false;
  140. // }
  141. // }
  142. getUiObjectByResourceId(id).waitForExists(10000);
  143. }
  144. public void waitForUiObject(UiSelector selector) {//等待对象出现
  145. // Date start = new Date();
  146. // boolean key = true;
  147. // while(key){
  148. // sleep(1000);
  149. // UiObject it = new UiObject(selector);
  150. // if (it.exists()) {
  151. // key = false;
  152. // }
  153. // Date end = new Date();
  154. // long time = end.getTime() - start.getTime();
  155. // if (time>10000) {
  156. // output("超过10秒没有出现!");
  157. // key = false;
  158. // }
  159. // }
  160. new UiObject(selector).waitForExists(10000);
  161. }
  162. public String getTextByResourceId(String id) throws UiObjectNotFoundException {
  163. return getUiObjectByResourceId(id).getText();
  164. }
  165. public String getDescByResourceI1d(String id) throws UiObjectNotFoundException {
  166. return getUiObjectByResourceId(id).getContentDescription();
  167. }
  168. public String getTextByResourceIdClassName(String id,String type) throws UiObjectNotFoundException {
  169. return getUiObjectByResourceIdClassName(id, type).getText();
  170. }
  171. //获取兄弟控件的文本
  172. public String getTextByBrother(String myid, String brotherid) throws UiObjectNotFoundException {
  173. return getUiObjectByResourceId(myid).getFromParent(new UiSelector().resourceId(brotherid)).getText();
  174. }
  175. public void writeTextByResourceId(String id, String text) throws UiObjectNotFoundException {
  176. getUiObjectByResourceId(id).setText(Utf7ImeHelper.e(text));
  177. }
  178. public void clickPiont(int x, int y) {//点击某一个点
  179. UiDevice.getInstance().click(x, y);
  180. }
  181. public void getUiObjectByResoureIdAndclickRightHalf(String id) throws UiObjectNotFoundException {
  182. //获取控件大小

复制代码


回复 支持 反对

使用道具 举报

该用户从未签到

4#
 楼主| 发表于 2018-4-10 15:11:43 | 只看该作者
  1. Rect sss = getUiObjectByResourceId(id).getBounds();
  2. //计算中心偏移量
  3. clickPiont(sss.centerX()+sss.width()/4, sss.centerY());
  4. }
  5. //点击控件左半边
  6. public void getUiObjectByResoureIdAndclickLeftHalf(String id) throws UiObjectNotFoundException {
  7. //获取控件大小
  8. Rect sss = getUiObjectByResourceId(id).getBounds();
  9. //计算中心偏移量
  10. clickPiont(sss.centerX()-sss.width()/4, sss.centerY());
  11. }
  12. public void setShort() {//设置短等待
  13. Configurator.getInstance().setActionAcknowledgmentTimeout(500);
  14. }
  15. public void setFast() {//设置短等待
  16. Configurator.getInstance().setActionAcknowledgmentTimeout(100);
  17. }
  18. public void setLong() {//设置长等待
  19. Configurator.getInstance().setActionAcknowledgmentTimeout(1500);
  20. }
  21. //清除中文文本
  22. public void clearTextByResourceId(String id) throws UiObjectNotFoundException {
  23. String name = getUiObjectByResourceId(id).getText();
  24. // output(name.length());
  25. UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_MOVE_END);
  26. // UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_MOVE_HOME);
  27. //如果光标在最后
  28. pressTimes(KeyEvent.KEYCODE_DEL, name.length());
  29. //如果光标在最开始
  30. // pressTimes(KeyEvent.KEYCODE_FORWARD_DEL, name.length());
  31. }
  32. //把string类型转化为int
  33. public int changeStringToInt (String text) {
  34. // return Integer.parseInt(text);
  35. return Integer.valueOf(text);
  36. }
  37. //等待文本控件并点击
  38. public void waitForClassNameAndClick(String type) throws UiObjectNotFoundException {
  39. waitForUiObjectByClassName(type);
  40. // getUiObjectByText(type).waitForExists(10000);
  41. getUiObjectByClassName(type).clickAndWaitForNewWindow();
  42. }
  43. public void waitForTextAndClick(String text) throws UiObjectNotFoundException {
  44. waitForUiObjectByText(text);
  45. // getUiObjectByText(text).waitForExists(10000);
  46. getUiObjectByText(text).clickAndWaitForNewWindow();
  47. }
  48. //通过开始文字查找控件并点击
  49. public void waitForStartTextAndClick(String text) throws UiObjectNotFoundException {
  50. getUiObjectByStartText(text).waitForExists(10000);
  51. getUiObjectByStartText(text).clickAndWaitForNewWindow();
  52. }
  53. public void waitForTextContainsAndClick(String text) throws UiObjectNotFoundException {
  54. getUiObjectByTextContains(text).waitForExists(10000);
  55. getUiObjectByTextContains(text).clickAndWaitForNewWindow();
  56. }
  57. public void waitForStartDescAndClick(String desc) throws UiObjectNotFoundException {
  58. getUiObjectByStartDesc(desc).waitForExists(10000);
  59. getUiObjectByStartDesc(desc).clickAndWaitForNewWindow();
  60. }
  61. public void waitForDescContainsAndClick(String desc) throws UiObjectNotFoundException {
  62. getUiObjectByDescContains(desc).waitForExists(10000);
  63. getUiObjectByDescContains(desc).clickAndWaitForNewWindow();
  64. }
  65. //等待资源id并点击
  66. public void waitForResourceIdAndClick(String id) throws UiObjectNotFoundException {
  67. waitForUiObjectByResourceId(id);
  68. // getUiObjectByResourceId(id).waitForExists(10000);
  69. getUiObjectByResourceId(id).clickAndWaitForNewWindow();
  70. }
  71. //等待desc并点击
  72. public void waitForDescAndClick(String desc) throws UiObjectNotFoundException {
  73. waitForUiObjectByDesc(desc);
  74. getUiObjectByDesc(desc).clickAndWaitForNewWindow();
  75. }
  76. //打开APP
  77. public void startClassApp() throws IOException, InterruptedException {
  78. Runtime.getRuntime().exec("am start -n com.gaotu100.superclass/.activity.main.SplashActivity").waitFor();
  79. }
  80. public void startWechat() throws IOException, InterruptedException {
  81. Runtime.getRuntime().exec("am start -n com.tencent.mm/.ui.LauncherUI").waitFor();
  82. }
  83. //关闭APP
  84. public void stopClassApp() throws InterruptedException, IOException {
  85. sleep(500);
  86. Runtime.getRuntime().exec("am force-stop com.gaotu100.superclass").waitFor();
  87. sleep(500);
  88. }
  89. //打开alertover
  90. public void stopAlertover() throws InterruptedException, IOException {
  91. Runtime.getRuntime().exec("am force-stop com.alertover.app").waitFor();
  92. sleep(500);
  93. }
  94. //关闭alertover
  95. public void startAlertover() throws IOException, InterruptedException {
  96. sleep(500);
  97. Runtime.getRuntime().exec("am start -n com.alertover.app/.activity.LoginActivity").waitFor();
  98. }
  99. //打开或者关闭wifi
  100. public void closeOrOpenWifi() throws InterruptedException, IOException {
  101. Runtime.getRuntime().exec("am start -n run.wifibutton/.WifiButtonActivity").waitFor();
  102. sleep(1000);
  103. }
  104. //关闭微信
  105. public void stopWechat() throws InterruptedException, IOException {
  106. sleep(500);
  107. Runtime.getRuntime().exec("am force-stop com.tencent.mm").waitFor();
  108. Thread.sleep(500);
  109. }
  110. //关闭支付宝
  111. public void stopAlipay() throws InterruptedException, IOException {
  112. Runtime.getRuntime().exec("am force-stop com.eg.android.AlipayGphone").waitFor();
  113. Thread.sleep(500);
  114. }
  115. //打开APP
  116. public void startWishApp() throws IOException {
  117. Runtime.getRuntime().exec("am start -n com.chaojizhiyuan.superwish/.activity.main.SplashActivity");
  118. }
  119. //关闭APP
  120. public void stopWishApp() throws InterruptedException, IOException {
  121. sleep(500);
  122. Runtime.getRuntime().exec("am force-stop com.chaojizhiyuan.superwish").waitFor();
  123. sleep(500);
  124. }
  125. //获取随机数
  126. public int getRandomInt(int num) {
  127. return new Random().nextInt(num);
  128. }
  129. //验证跳转支付宝
  130. public void verifySkipAlipay() {
  131. waitForUiObjectByText("添加银行卡付款");
  132. String packagename = UiDevice.getInstance().getCurrentPackageName();
  133. assertEquals("跳转支付宝失败!", "com.eg.android.AlipayGphone", packagename);
  134. }
  135. //验证跳转支付宝
  136. public void verifySkipWechat() {
  137. waitForUiObjectByText("使用零钱支付");
  138. String packagename = UiDevice.getInstance().getCurrentPackageName();
  139. assertEquals("跳转微信失败!", "com.tencent.mm", packagename);
  140. }
  141. //屏幕提醒
  142. public void warningTester() throws RemoteException {
  143. UiDevice.getInstance().sleep();//灭屏
  144. sleep(1200);//休眠
  145. if (UiDevice.getInstance().isScreenOn()) {//获取屏幕状态
  146. return;//如果亮屏状态则结束运行
  147. } else {
  148. UiDevice.getInstance().wakeUp();//如果的灭屏状态则重新运行本方法
  149. warningTester();//递归
  150. }
  151. }
  152. //向前滚动
  153. public boolean scrollForward() throws UiObjectNotFoundException {
  154. return getUiScrollabe().scrollForward(50);
  155. }
  156. //向后滚动
  157. public boolean scrollBackward() throws UiObjectNotFoundException {
  158. return getUiScrollabe().scrollBackward(50);
  159. }
  160. public void deleteScreenShot() {//删除截图文件夹
  161. File file = new File("/mnt/sdcard/123/");
  162. if (file.exists()) {//如果file存在
  163. File[] files = file.listFiles();//获取文件夹下文件列表
  164. for (int i = 0; i < files.length; i++) {//遍历删除
  165. files[i].delete();
  166. }
  167. file.delete();//最后删除文件夹,如果不存在直接删除文件夹
  168. } else {
  169. output("文件夹不存在!");
  170. }

  171. }

  172. }
复制代码
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-11-17 22:26 , Processed in 0.067498 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表