【转】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(来自 ) 题目描写叙述 给定一个数组和滑动窗体的大小.找出全部滑动窗体里 ...
随机推荐
- 【Android进阶】Android调用WebService的实现
最近想自己搞搞服务器,就从最简单的webservice开始吧 先上效果图 项目结构 开始贴代码,注释都有,有问题的请留言 MainActivity.java package com.example.w ...
- Handler消息源代码分析
public static final Looper myLooper() { return (Looper)sThreadLocal.get(); } 首先到Handler运行过程的总结: 1. L ...
- 怎样解决No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386).
怎样解决No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386). 错误 ...
- String构造函数originalValue.length>size 它发生
最近观看Jdk6于String源代码被发现String这种施工方法有.源内容如下面: public String(String original) { int size = original.coun ...
- 对SA权限的再突破 (对付xplog70.dll被删)转载
原文:对SA权限的再突破 (对付xplog70.dll被删)转载 对SA权限的再突破 (对付xplog70.dll被删)转载 转载自:http://www.bitscn.com/plus/view.p ...
- android应用框架构建------AppManager
体验一些Android应用程序开发的个人总结 当我们开发应用程序.经常有非常多非常多activity,在这一刻,我们需要一个activity堆栈,以帮助管理activity的finish和start. ...
- [创意标题] spoj 11354 Amusing numbers
意甲冠军: 给k(1<=k<=10^15),先询问k 大只包含数字5和6的数目是多少 实例 1那是,5 ,3那是,55 .4那是,56 思考: 首先,我们可以找到.有许多2这是头号,有两个 ...
- JMS ActiveMQ研究文档
1. 背景 当前,CORBA.DCOM.RMI等RPC中间件技术已广泛应用于各个领域.但是面对规模和复杂度都越来越高的分布式系统,这些技术也显示出其局限性:(1)同步通信:客户发出调用后,必须等待服务 ...
- EJB(一)认识ejb
什么是ejb? 相同仍旧在这个系列博客之处,谈谈对ejb的认识和理解. sun微公司对于ejb的定义大体是这种,ejb是一套用于开发和部署分布式组件的的架构.採用ejb的架构应用能够是 ...
- JSP简明教程(四):EL表达式语言、JavaBean、Cookie、Session
EL表达式语言 EL这是Expression Language.的目的是为了简化JSP句法.来看几个例子来清除. ${test} 它会被翻译成<%=test%> ${test.name} ...