【转】Appium测试安卓Launcher以滑动窗体获得目标应用
原文地址:http://blog.csdn.net/zhubaitian/article/details/39755553
所谓Launcher,指的是安卓的桌面管理程序,所有的应用图标都放在launcher上面。其实这是一个很简单的例子,只是为了验证几点想法而已。
1.实验目的
做这个试验的目的有二
- 尝试下窗体滑动函数swipe的使用
- 好奇究竟能不能正常的对安卓的Launcher进行指定package和activity进行测试
2.实验背景
3. 试验步骤
3.1 获得launcher的package和activity两个capabilities
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传送给appium服务器端,注意指定app,因为我们不需要重新安装Launcher
- 找到代表整个屏幕的控件,然后通过获取它的location和size属性来计算出滑动开始和结束的坐标。注意开始的坐标如果是屏幕的边界,需要调整下像素(例子中是减去5个像素)以防出错。
- 通过滑动遍历每个页面直到找到目标控件为止。
【转】Appium测试安卓Launcher以滑动窗体获得目标应用的更多相关文章
- Appium測试安卓Launcher以滑动窗口获得目标应用
所谓Launcher,指的是安卓的桌面管理程序,全部的应用图标都放在launcher上面.事实上这是一个非常easy的样例,仅仅是为了验证几点想法而已. 1.实验目的 做这个试验的目的有二 尝试下窗口 ...
- Appium测试安卓apk遇到的问题及解决方法
1.Showing error - “Returned value cannot be converted to WebElement: {ELEMENT=1} 解决方法:https://sqa.s ...
- 使用appium框架测试安卓app时,获取toast弹框文字时,前一步千万不要加time.sleep等等待时间。
使用appium框架测试安卓app时,如果需要获取toast弹框的文案内容,那么再点击弹框按钮之前,一定记得千万不要加time.sleep()等待时间,否则有延迟,一直获取不到: 获取弹框的代码: m ...
- 一文带你趟过mac搭建appium测试环境的遇到的坑
做UI自动化,最难的一步就是在环境搭建上,怎么去搭建一个UI自动化测试的环境,会难住很多人,在Mac上搭建appium如何搭建呢,本文带着大家去领略如何在mac上搭建appium测试环境.下面就是详细 ...
- Qt使用QGraphicsView实现滑动窗体效果
QGraphicsView用来显示一个滚动视图区的QGraphicsScene内容.QGraphicsScene提供了QGraphicsItem的容器功能.通常与QGraphicsView一起使用来描 ...
- 【亲测】Appium测试Android混合应用时,第二次切换到WebView失败
要解决的问题:Appium测试Android混合应用时,第二次切换到WebView时失败 原因分析:在用Appium测试Android混合应用时,当程序第一次切换到WebView时,可以正常进行自动化 ...
- leetcode ---双指针+滑动窗体
一:Minimum Size Subarray Sum(最小长度子数组的和O(N)) 题目: Given an array of n positive integers and a positive ...
- Appium使用Python运行appium测试的实例
Appium使用Python运行appium测试的实例 一. Appium之介绍 https://testerhome.com/topics/8038 详情参考--https://testerhom ...
- 滑动窗体的最大值(STL的应用+剑指offer)
滑动窗体的最大值 參与人数:767时间限制:1秒空间限制:32768K 通过比例:21.61% 最佳记录:0 ms|8552K(来自 ) 题目描写叙述 给定一个数组和滑动窗体的大小.找出全部滑动窗体里 ...
随机推荐
- 【solr这四个主题】大约VelocityResponseWriter
一个.大约Velocity基本配置 在Solr在,可以以多种方式返回搜索结果,作为一个简单的文字回复(XML.JSON.CSV等待),能够返回velocity.js等格式.而VelocityRespo ...
- int a[5]={1,2,3,4,5};printf("%d\n", *((int*)(&a+1)-2);
有说服力的笔试题有一定的期限,问:什么是结果,答案是4,为什么要挤? 我明白(不知道是不正确): &a这是一个数组指针,类型int[5],然后&a添加1其实a+sizeof(int)* ...
- UVA 847 - A Multiplication Game(游戏)
UVA 847 - A Multiplication Game 题目链接 题意:一个数一開始是1,每次轮流乘2-9,谁先大于n谁就赢,问谁胜 思路:博弈,找出必胜态.2-9为stan,10-18为ol ...
- UVa 10491 - Cows and Cars
題目:有m+n個們,每個門後面有牛或者車:有n仅仅牛,m輛車,你選擇当中1個: 然後打開当中的k你沒有選中的門後是牛的,問你改變選時得到車的概率. 說明:數學題,概率.全概率公式就可以: 說明:第10 ...
- How to pause the game in Uniy3D
static float timeScale; Description The scale at which the time is passing. This can be used for slo ...
- Unity3d 导入图像尺寸失真解决方案
导入到unity3d内的图像被默认长宽变换为满足2^n关系. 例如以下图,我有张图片名称为984plus598表示我尺寸为984*598.拷贝到unity3d中后的大小为1024*512 方法一: 在 ...
- POJ 2155 Matrix (D区段树)
http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 1 ...
- 推荐2一个在Java编码过程中得心应手的工具
推荐2在编码过程中的减小不仅编码的量,挺easy工具上手:可适用Java反思与单探头Assert. 1 Mirror:Java反思 简单介绍 官网:http://projetos.vidageek.n ...
- QlikView线图高亮选择尺寸
作为标题,如今,学生问我一个问题.尺寸Month.expression它是Count(Id). 这个图是一个折线图,不管你选择哪个月的其他下拉列表,销售量.由于Expression里面是这样写的 Co ...
- Android-往来:添加到联系人
//添加到联系人.使用事务 public void testAddContact() { String name[]={"周杰伦","谢霆锋","言承 ...