所谓Launcher,指的是安卓的桌面管理程序,全部的应用图标都放在launcher上面。事实上这是一个非常easy的样例,仅仅是为了验证几点想法而已。

1.实验目的

做这个试验的目的有二

  • 尝试下窗口滑动函数swipe的使用
  • 好奇到底能不能正常的对安卓的Launcher进行指定package和activity进行測试

2.实验背景

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

例如以下图所看到的


3. 试验步骤

3.1 获得launcher的package和activity两个capabilities

能够通过HierarchyViewer直接查看获得

3.2 编码实现

package majcit.com.AppiumDemo;

import io.appium.java_client.android.AndroidDriver;

import java.net.URL;
import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities; import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat; /**
* Unit test for simple App.
*/
public class LauncherTest {
/**
* Create the test case
*
* @param testName name of the test case
*/
private AndroidDriver driver; @Before
public void setUp() throws Exception {
// set up appium
//File classpathRoot = new File(System.getProperty("user.dir"));
//File appDir = new File(classpathRoot, "apps");
//File app = new File(appDir, "NotePad.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","Android");
//capabilities.setCapability("platformVersion", "4.2");
//capabilities.setCapability("platformName", "Android");
//capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "com.miui.home");
capabilities.setCapability("appActivity", "com.miui.home.launcher.Launcher");
//capabilities.setCapability("appActivity", ".NotesList");
//capabilities.setCapability("autoLaunch", "false");
//capabilities.setCapability("noReset", true);
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
} @After
public void tearDown() throws Exception {
driver.quit();
} @Test
public void launchNotePad() throws InterruptedException{ WebElement el = null;
WebElement screen = null;
Point point = null;
Dimension size = null; boolean found = false;
int pageCount = 3; //Assume that there are totally 3 screens to be swiped. int xStart = -1;
int yStart = -1;
int xEnd = -1;
int yEnd = -1; //Get the start and end coordinates for swipe
Thread.sleep(3000);
screen = driver.findElementById("com.miui.home:id/cell_layout");
point = screen.getLocation();
size = screen.getSize();
xEnd = point.getX();
yEnd = point.getY() + size.getHeight()/2;
xStart = point.getX() + size.getWidth() - 5;
yStart = yEnd; System.out.println("starX:" + xStart +"\nstartY:" + yStart + "\nendX:" + xEnd + "\nendY:" + yEnd); //本来想通过推断屏幕上的几个小圆点来推断到底有多少个屏幕的,但发觉xPath根本不起效,父文件夹感觉根本起不了定位作用,仅仅有最后的//android.widget.ImageView起效,所以一下找出75个元素。 /*
List<WebElement> pageImages = driver.findElementsByXPath("//android.view.View/android.widget.LinearLayout/android.widget.ImageView");
assertThat(pageImages.size(),is(3));
for (WebElement e: pageImages) {
e.click();
}
*/ //Swipe all screens till get the expected control
int currentPage = 0;
while (found == false && currentPage < pageCount) {
found = true; currentPage += 1; try {
el = driver.findElementByName("Notes");
}catch (NoSuchElementException e) {
found = false;
System.out.println(e);
} if (found == true)
break; driver.swipe(xStart, yStart, xEnd, yEnd, 100);
Thread.sleep(1000);
} assertThat(found,is(true));
assertThat(el,notNullValue()); el.click(); } }

步骤说明大概例如以下:

  • 准备好必须的capabilities传送给appiumserver端。注意指定app,由于我们不须要又一次安装Launcher
  • 找到代表整个屏幕的控件,然后通过获取它的location和size属性来计算出滑动開始和结束的坐标。

    注意開始的坐标假设是屏幕的边界,须要调整下像素(样例中是减去5个像素)以防出错。

  • 通过滑动遍历每一个页面直到找到目标控件为止。

 

作者

自主博客

微信

CSDN

天地会珠海分舵

http://techgogogo.com

服务号:TechGoGoGo

扫描码:

http://blog.csdn.net/zhubaitian



Appium測试安卓Launcher以滑动窗口获得目标应用的更多相关文章

  1. 【转】Appium测试安卓Launcher以滑动窗体获得目标应用

    原文地址:http://blog.csdn.net/zhubaitian/article/details/39755553 所谓Launcher,指的是安卓的桌面管理程序,所有的应用图标都放在laun ...

  2. Appium - WebView測试(Android)

    Appium - WebView測试 作者: Max.Bai 时间: 2015/07 Appium - WebView測试(Android) 如今App都是混合型的.有原生的也包括WebView的,a ...

  3. (4.5.4)Android測试TestCase单元(Unit test)測试和instrumentationCase单元測试

    Android单元和instrumentation单元測试 Developing Android unit and instrumentation tests Android的单元測试是基于JUnit ...

  4. [DeeplearningAI笔记]卷积神经网络3.1-3.5目标定位/特征点检测/目标检测/滑动窗口的卷积神经网络实现/YOLO算法

    4.3目标检测 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.1目标定位 对象定位localization和目标检测detection 判断图像中的对象是不是汽车--Image clas ...

  5. MAC中在eclipse luna上搭建移动平台自己主动化測试框架(UIAutomator/Appium/Robotium/MonkeyRunner)关键点记录

    这几天由于原来在用的hp laptop的电池坏掉了,机器一不小心就断电.所以仅仅能花时间在自己的mackbook pro上又一次搭建整套环境.大家都知道搭建好开发环境是个非常琐碎须要耐心的事情,特别是 ...

  6. Appium IOS 自己主动化測试初探

    手机平台的自己主动化測试工具非常多,之前研究过了安卓和苹果的原生自己主动化測试框架.经一些同事介绍,貌似Appium是个不错的工具. 想记录一下研究的结果,也算是篇干货的文章 在网上也看了一些视频.个 ...

  7. iOS自己主动化測试的那些干货

    前言 假设有測试大佬发现内容不正确.欢迎指正,我会及时改动. 大多数的iOS App(没有持续集成)迭代流程是这种 也就是说.測试是公布之前的最后一道关卡.假设bug不能在測试中发现,那么bug 就会 ...

  8. 【Android开发-6】了解内情,我们须要一些调试和測试手段

    前言:人生不可能十全十美,总会有些遗憾存在,经历过遗憾,我们才懂的什么是生活. 程序也一样.追求完美,就必定会有经历bug存在的时候. 经历过不断的bug磨练.我们技术才会不断的成长.对于调试bug, ...

  9. Android单元測试之JUnit

    随着近期几年測试方面的工作慢慢火热起来.常常看见有招聘測试project师的招聘信息.在Java中有单元測试这么一个JUnit 方式,Android眼下主要编写的语言是Java,所以在Android开 ...

随机推荐

  1. Rotate List leetcode java

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...

  2. Math Number 数值类 包装类 数学计算 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. 交叉编译git

    git依赖openssl.zlib. 首先编译openssl ./Configure linux-armv4 shared 修改Makefile,CC.RANLIB.MAKEDEPPROG为对应的交叉 ...

  4. Reduce Task的学习笔记

    MapReduce五大过程已经分析过半了.上次分析完Map的过程,着实花费了我的非常多时间.只是收获非常大,值得了额,这次用相同的方法分析完了Reduce的过程,也算是彻底摸透了MapReduce思想 ...

  5. UITableViewCell 取消分隔线

    方法一: [historyTodayTableVC setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 方法二: [historyTodayTa ...

  6. 开源 免费 java CMS - FreeCMS1.5-数据对象-guestbook

    下载地址:http://code.google.com/p/freecms/ guestbook 从FreeCMS 1.5 开始支持 在使用留言相关标签时,标签会封装guestbook供页面调用. 属 ...

  7. 启动mysql出现1067错误

    0. 打开mysql\bin\my.ini,查找[mysqld],在[mysqld]下面添加一行文字,skip-grant-tables 即组成 [mysqld] skip-grant-tables[ ...

  8. git的color configura

    git color的配置 Git多颜色输出 Git默认的输出是单一颜色的,不仅不够美观,也不容易阅读.实际上,Git本身就支持用多种颜色来显示其输出的信息,只需在命令行中运行以下命令来修改git的设置 ...

  9. js输出指定n位数的随机数的随机整数方法【转发】

    /** * Description:[输出指定n位数的随机数的随机整数] * * @param n 指定n位数 * */ function RndNum(n) { var rnd = "&q ...

  10. python之函数用法file()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法file() #file() #说明:file()内建函数它的功能等于open(),但 ...