原文地址: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. Android使用开源项目Xutils实现多线程下载文件

    #1.下载utils项目 https://github.com/wyouflf/xUtils #2布局文件里实现UI <LinearLayout xmlns:android="http ...

  2. HDU 2289 Cup(可以二分法,但是除了它的一半?)

    这道题目.运营商做数学题?算上两个子主题做?顶多算一个水主要议题... 首先,没有实际的二分法,但是,我们发现了一个新的解决方案,以取代二分法. 若果按照i从0,每次添加0.00000001我一直枚举 ...

  3. ZendStudio10.6.1如何安装最新的集成svn小工具?

    选择Help菜单->Install New Software...在Work with输入http://subclipse.tigris.org/update_1.10.x,等待完成后,.除了S ...

  4. nodejs 复制、移动文件

    对路径没有做验证 复制文件 var fs = require('fs'); var path = require('path'); var fileName = "coverflow-3.0 ...

  5. C#之关于时间的整理

    今天在整理C#的异步编程的时候,看到一个Stopwatch类.让我想起了,时候整理一下C#关于时间的类,望补充.斧正. DataTime类 表示时间上的一刻,即某个时间节点,通常以日期和当天的时间表示 ...

  6. POJ3090_Visible Lattice Points【欧拉函数】

    Visible Lattice Points Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5653 Accepted: 333 ...

  7. POJ 3207 Ikki&#39;s Story IV - Panda&#39;s Trick (2-SAT)

    职务地址:id=3207">POJ 3207 找好矛盾关系.矛盾关系是(2,5)和(3,6)这两个仅仅能一个在外边,一个在里边.利用这个矛盾关系来建图. 能够用在外边和里边来当1和0, ...

  8. Oracle cloud control 12c 的启动与关闭

    Oracle cloud control 12c整个安装比較复杂,光是安装路径的选择,登录password,端口号等众多个配置不免让人眼花缭乱,目不暇接.本文描写叙述的是安装完成后怎样获取安装时设定的 ...

  9. ASP.NET MVC (Razor)开发

    ASP.NET MVC (Razor)开发 过去我们使用过一些周报工具来完成项目组或部门的周报填写与考核工作,但多少有些不理想,要么功能太过简单,要么功能特别繁杂,不接地气,使用不便. 后来我们就考虑 ...

  10. poj3903 Stock Exchange(最长上升子序列)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3903">http://poj.org/problem?id=3903 Descrip ...