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


    所谓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 +"
    startY:" + yStart + "
    endX:" + xEnd + "
    endY:" + 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




  • 相关阅读:
    解决Eclipse中“诡异”的错误:找不到或无法加载主类
    eclipse 中的注释 快捷键 多行注释快捷键 单行注释快捷键
    将Eclipse设置为黑色主题
    Javac提示不是内部或外部命令
    js粒子旋涡
    canvas黑客帝国代码雨特效 | jQuery特效|手机微信网站特效| 网页特效库
    python笔记--冒泡排序升级版
    MySQL5.7(二)数据库的基本操作
    Linux命令应用大词典-第1章 登录、退出、关机和重启
    第1章 Linux命令行简介
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6710059.html
Copyright © 2020-2023  润新知