原文地址:http://blog.csdn.net/zhubaitian/article/details/39755553

所谓Launcher,指的是安卓的桌面管理程序,所有的应用图标都放在launcher上面。其实这是一个很简单的例子,只是为了验证几点想法而已。

1.实验目的

做这个试验的目的有二

  • 尝试下窗体滑动函数swipe的使用
  • 好奇究竟能不能正常的对安卓的Launcher进行指定package和activity进行测试

2.实验背景

过程是打算使用appium来启动launcher,然后滑动窗口去获取在第三个桌面的sdk自带应用”Notes“。如下图所示

3. 试验步骤

3.1 获得launcher的package和activity两个capabilities

可以通过HierarchyViewer直接查看获得

3.2 编码实现

  1. package majcit.com.AppiumDemo;
  2. import io.appium.java_client.android.AndroidDriver;
  3. import java.net.URL;
  4. import org.junit.Test;
  5. import org.junit.After;
  6. import org.junit.Before;
  7. import org.openqa.selenium.Dimension;
  8. import org.openqa.selenium.NoSuchElementException;
  9. import org.openqa.selenium.Point;
  10. import org.openqa.selenium.WebElement;
  11. import org.openqa.selenium.remote.DesiredCapabilities;
  12. import static org.hamcrest.Matchers.*;
  13. import static org.hamcrest.MatcherAssert.assertThat;
  14. /**
  15. * Unit test for simple App.
  16. */
  17. public class LauncherTest {
  18. /**
  19. * Create the test case
  20. *
  21. * @param testName name of the test case
  22. */
  23. private AndroidDriver driver;
  24. @Before
  25. public void setUp() throws Exception {
  26. // set up appium
  27. //File classpathRoot = new File(System.getProperty("user.dir"));
  28. //File appDir = new File(classpathRoot, "apps");
  29. //File app = new File(appDir, "NotePad.apk");
  30. DesiredCapabilities capabilities = new DesiredCapabilities();
  31. capabilities.setCapability("deviceName","Android");
  32. //capabilities.setCapability("platformVersion", "4.2");
  33. //capabilities.setCapability("platformName", "Android");
  34. //capabilities.setCapability("app", app.getAbsolutePath());
  35. capabilities.setCapability("appPackage", "com.miui.home");
  36. capabilities.setCapability("appActivity", "com.miui.home.launcher.Launcher");
  37. //capabilities.setCapability("appActivity", ".NotesList");
  38. //capabilities.setCapability("autoLaunch", "false");
  39. //capabilities.setCapability("noReset", true);
  40. driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  41. }
  42. @After
  43. public void tearDown() throws Exception {
  44. driver.quit();
  45. }
  46. @Test
  47. public void launchNotePad() throws InterruptedException{
  48. WebElement el = null;
  49. WebElement screen = null;
  50. Point point = null;
  51. Dimension size = null;
  52. boolean found = false;
  53. int pageCount = 3; //Assume that there are totally 3 screens to be swiped.
  54. int xStart = -1;
  55. int yStart = -1;
  56. int xEnd = -1;
  57. int yEnd = -1;
  58. //Get the start and end coordinates for swipe
  59. Thread.sleep(3000);
  60. screen = driver.findElementById("com.miui.home:id/cell_layout");
  61. point = screen.getLocation();
  62. size = screen.getSize();
  63. xEnd = point.getX();
  64. yEnd = point.getY() + size.getHeight()/2;
  65. xStart = point.getX() + size.getWidth() - 5;
  66. yStart = yEnd;
  67. System.out.println("starX:" + xStart +"\nstartY:" + yStart + "\nendX:" + xEnd + "\nendY:" + yEnd);
  68. //本来想通过判断屏幕上的几个小圆点来判断究竟有多少个屏幕的,但发觉xPath根本不起效,父目录感觉根本起不了定位作用,只有最后的//android.widget.ImageView起效,所以一下找出75个元素。
  69. /*
  70. List<WebElement> pageImages = driver.findElementsByXPath("//android.view.View/android.widget.LinearLayout/android.widget.ImageView");
  71. assertThat(pageImages.size(),is(3));
  72. for (WebElement e: pageImages) {
  73. e.click();
  74. }
  75. */
  76. //Swipe all screens till get the expected control
  77. int currentPage = 0;
  78. while (found == false && currentPage < pageCount) {
  79. found = true;
  80. currentPage += 1;
  81. try {
  82. el = driver.findElementByName("Notes");
  83. }catch (NoSuchElementException e) {
  84. found = false;
  85. System.out.println(e);
  86. }
  87. if (found == true)
  88. break;
  89. driver.swipe(xStart, yStart, xEnd, yEnd, 100);
  90. Thread.sleep(1000);
  91. }
  92. assertThat(found,is(true));
  93. assertThat(el,notNullValue());
  94. el.click();
  95. }
  96. }

步骤说明大概如下:

  • 准备好必须的capabilities传送给appium服务器端,注意指定app,因为我们不需要重新安装Launcher
  • 找到代表整个屏幕的控件,然后通过获取它的location和size属性来计算出滑动开始和结束的坐标。注意开始的坐标如果是屏幕的边界,需要调整下像素(例子中是减去5个像素)以防出错。
  • 通过滑动遍历每个页面直到找到目标控件为止。

【转】Appium测试安卓Launcher以滑动窗体获得目标应用的更多相关文章

  1. Appium測试安卓Launcher以滑动窗口获得目标应用

    所谓Launcher,指的是安卓的桌面管理程序,全部的应用图标都放在launcher上面.事实上这是一个非常easy的样例,仅仅是为了验证几点想法而已. 1.实验目的 做这个试验的目的有二 尝试下窗口 ...

  2. Appium测试安卓apk遇到的问题及解决方法

    1.Showing error - “Returned value cannot be converted to WebElement: {ELEMENT=1}  解决方法:https://sqa.s ...

  3. 使用appium框架测试安卓app时,获取toast弹框文字时,前一步千万不要加time.sleep等等待时间。

    使用appium框架测试安卓app时,如果需要获取toast弹框的文案内容,那么再点击弹框按钮之前,一定记得千万不要加time.sleep()等待时间,否则有延迟,一直获取不到: 获取弹框的代码: m ...

  4. 一文带你趟过mac搭建appium测试环境的遇到的坑

    做UI自动化,最难的一步就是在环境搭建上,怎么去搭建一个UI自动化测试的环境,会难住很多人,在Mac上搭建appium如何搭建呢,本文带着大家去领略如何在mac上搭建appium测试环境.下面就是详细 ...

  5. Qt使用QGraphicsView实现滑动窗体效果

    QGraphicsView用来显示一个滚动视图区的QGraphicsScene内容.QGraphicsScene提供了QGraphicsItem的容器功能.通常与QGraphicsView一起使用来描 ...

  6. 【亲测】Appium测试Android混合应用时,第二次切换到WebView失败

    要解决的问题:Appium测试Android混合应用时,第二次切换到WebView时失败 原因分析:在用Appium测试Android混合应用时,当程序第一次切换到WebView时,可以正常进行自动化 ...

  7. leetcode ---双指针+滑动窗体

    一:Minimum Size Subarray Sum(最小长度子数组的和O(N)) 题目: Given an array of n positive integers and a positive ...

  8. Appium使用Python运行appium测试的实例

    Appium使用Python运行appium测试的实例 一.  Appium之介绍 https://testerhome.com/topics/8038 详情参考--https://testerhom ...

  9. 滑动窗体的最大值(STL的应用+剑指offer)

    滑动窗体的最大值 參与人数:767时间限制:1秒空间限制:32768K 通过比例:21.61% 最佳记录:0 ms|8552K(来自 ) 题目描写叙述 给定一个数组和滑动窗体的大小.找出全部滑动窗体里 ...

随机推荐

  1. Codeforces 12D Ball 树形阵列模拟3排序元素

    主题链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...

  2. POST和Get辨析

    在Form里面,能够使用post也能够使用get.它们都是method的合法取值,可是两者也有不同,主要差别在于传递和获取參数的方式不同 一.Get方法: 1.參数的传递方式: 通过URL请求来传递用 ...

  3. spring集成quartz

    spring集成quartz 注意:出现异常"Caused by: java.lang.IncompatibleClassChangeError: class org.springframe ...

  4. Redis于windows在安装

    下载的windows版本号是redis-2.0.2,解压到D盘下: D:\redis-2.0.2 进到该文件夹下,有下列文件: redis-server.exe:服务程序 redis-check-du ...

  5. Hibernate在关于一对多,多对一双向关联映射

    [Hibernate]之关于一对多,多对一双向关联映射 因为一对多.和多对一的双向关联映射基本上一样,所以这里就一起写下来! Annotations配置 @Entity @Table(name=&qu ...

  6. PreparedStatement批量处理的一个Framework(原创)

    场景:批量进行DML操作,但涉及的表不同,不能使用executeBatch() 需求:(1)如果DML中有一个错误时,要全部回滚:(2)如果全部正确,要全部执行: 解决方案: package jdbc ...

  7. gem 安装nokigiri

    在mac上安装nokogiri的时候各种报错,终于安装成功一次,备份命令. ➜ ~ sudo gem install nokogiri -- --use-system-libraries --with ...

  8. 【Eclipse高速发展-文章插件】google V8,对于调试Nodejs

    安装的的时候能够通过 Help>Eclipse Marketplace的 搜索方式安装 或者 Help>Install New Software Name:googleV8 Locatio ...

  9. (大数据工程师学习路径)第二步 Vim编辑器----查找替换

    一.字符的替换及撤销(Undo操作) 1.替换和撤销(Undo)命令 替换和Undo命令都是针对普通模式下的操作 命令 说明 r+<待替换字母> 将游标所在字母替换为指定字母 R 连续替换 ...

  10. WPF:警惕TextBox会占用过多内存

    原文:WPF:警惕TextBox会占用过多内存 问题源自这篇文章:WPF的TextBox产生内存泄露的情况. 整个问题是这样的,文章作者演示使用类似下方的代码来不停地像WPF的TextBox控件赋值: ...