当前位置:   article > 正文

Android自动化测试-UiAutomator环境搭建_uiautomator 开发环境搭建

uiautomator 开发环境搭建

目录

一、环境准备

二、新建Android Studio工程

三、测试实例

总结:


一、环境准备

  1. 安装android sdk,并配置环境变量

  2. 安装android studio,国内访问官网受限,如果下载不到,可以到我的百度云盘下载:

    https://pan.baidu.com/s/1bpq5wK3

   此云盘中有uiautomator2所依赖的jar包,可以同时下载

   

二、新建Android Studio工程

  

  新建一个project,输入application name,下一步,

  

  默认选择,下一步,

  

  选择 empty activity:

  

  最后finish之后,切换到project视图;

  

  右击工程,新建一个libs,并把网盘中下载的uiautomator依赖的jar包,copy进来,并添加依赖,

  

  Add As Library之后,会弹出一个小框,选择app,点击OK

  

  这样我们的工程就建好了,左上角,把我们的project模式切换成android模式,

  

  现在android视图模式下,界面就比较简洁直观了,如下图所示:标注android test的地方,就是我们要写测试用例的包,

  

  新家一个java class,输入class name,现在我们就可以开开心心的写测试代码了

三、测试实例

  下面我们写一个例子,启动模拟器,模拟器home上有个chrome浏览器,操作步骤:点击chrome-输入www.baidu.com-enter;

  点击android studio上的 AVD manager,就可以启动模拟器,模拟器界面如下:

  

  

  测试用例:

    1. 点击chrome

    2. 输入www.baidu.com

    3. Enter

  代码如下:

    

        

  写好测试用例之后,我们就可以运行了,在运行之前,我们先看下运行配置:

  

  在配置文件中,一定要有如下一行代码,如果没有,可以自己加上:

  testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

   现在就可以运行了,打开你的模拟器,看下界面有什么效果:

  

  完整代码如下:

  1. 1 import android.app.Instrumentation;
  2. 2 import android.support.test.InstrumentationRegistry;
  3. 3 import android.support.test.runner.AndroidJUnit4;
  4. 4 import android.support.test.uiautomator.UiDevice;
  5. 5 import android.support.test.uiautomator.UiObject;
  6. 6 import android.support.test.uiautomator.UiObjectNotFoundException;
  7. 7 import android.support.test.uiautomator.UiSelector;
  8. 8 import android.view.KeyEvent;
  9. 9
  10. 10 import org.junit.Before;
  11. 11 import org.junit.Test;
  12. 12 import org.junit.runner.RunWith;
  13. 13
  14. 14 /**
  15. 15 * Created by tianxing on 2017/8/15.
  16. 16 */
  17. 17
  18. 18 @RunWith(AndroidJUnit4.class)
  19. 19 public class helloworld {
  20. 20
  21. 21 UiDevice uiDevice;
  22. 22 Instrumentation instrumentation;
  23. 23
  24. 24 @Before
  25. 25 public void setUp(){
  26. 26 instrumentation = InstrumentationRegistry.getInstrumentation();
  27. 27 uiDevice = UiDevice.getInstance(instrumentation);
  28. 28 }
  29. 29
  30. 30 @Test
  31. 31 public void launchChrome(){
  32. 32 UiObject chrome = uiDevice.findObject(new UiSelector().text("Chrome"));
  33. 33 UiObject searchContent = uiDevice.findObject(new UiSelector().text("Search or type URL"));
  34. 34
  35. 35 try {
  36. 36 chrome.click();
  37. 37 sleep(2000);
  38. 38 searchContent.setText("www.baidu.com");
  39. 39 uiDevice.pressKeyCode(KeyEvent.KEYCODE_ENTER);
  40. 40 } catch (UiObjectNotFoundException e) {
  41. 41 e.printStackTrace();
  42. 42 }
  43. 43
  44. 44 }
  45. 45
  46. 46 public void sleep(int mint){
  47. 47 try{
  48. 48 Thread.sleep(mint);
  49. 49 }catch (InterruptedException e){
  50. 50 e.printStackTrace();
  51. 51 }
  52. 52 }
  53. 53
  54. 54 }

总结:

感谢每一个认真阅读我文章的人!!!

我个人整理了我这几年软件测试生涯整理的一些技术资料,包含:电子书,简历模块,各种工作模板,面试宝典,自学项目等。欢迎大家点击下方名片免费领取,千万不要错过哦。

                                            

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号