zoukankan      html  css  js  c++  java
  • 【转】Appium测试安卓Launcher以滑动窗体获得目标应用

    原文地址: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 编码实现

    [java] view plaincopy
     
    1. package majcit.com.AppiumDemo;  
    2.   
    3. import io.appium.java_client.android.AndroidDriver;  
    4.   
    5. import java.net.URL;  
    6. import org.junit.Test;  
    7. import org.junit.After;  
    8. import org.junit.Before;  
    9. import org.openqa.selenium.Dimension;  
    10. import org.openqa.selenium.NoSuchElementException;  
    11. import org.openqa.selenium.Point;  
    12. import org.openqa.selenium.WebElement;  
    13. import org.openqa.selenium.remote.DesiredCapabilities;  
    14.   
    15. import static org.hamcrest.Matchers.*;  
    16. import static org.hamcrest.MatcherAssert.assertThat;  
    17.   
    18. /** 
    19.  * Unit test for simple App. 
    20.  */  
    21. public class LauncherTest {  
    22.     /** 
    23.      * Create the test case 
    24.      * 
    25.      * @param testName name of the test case 
    26.      */  
    27.     private AndroidDriver driver;  
    28.   
    29.     @Before  
    30.     public void setUp() throws Exception {  
    31.         // set up appium  
    32.         //File classpathRoot = new File(System.getProperty("user.dir"));  
    33.         //File appDir = new File(classpathRoot, "apps");  
    34.         //File app = new File(appDir, "NotePad.apk");  
    35.         DesiredCapabilities capabilities = new DesiredCapabilities();  
    36.         capabilities.setCapability("deviceName","Android");  
    37.         //capabilities.setCapability("platformVersion", "4.2");  
    38.         //capabilities.setCapability("platformName", "Android");  
    39.         //capabilities.setCapability("app", app.getAbsolutePath());  
    40.         capabilities.setCapability("appPackage", "com.miui.home");  
    41.         capabilities.setCapability("appActivity", "com.miui.home.launcher.Launcher");  
    42.         //capabilities.setCapability("appActivity", ".NotesList");  
    43.         //capabilities.setCapability("autoLaunch", "false");  
    44.         //capabilities.setCapability("noReset", true);  
    45.         driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);  
    46.     }   
    47.   
    48.     @After  
    49.     public void tearDown() throws Exception {  
    50.         driver.quit();  
    51.     }  
    52.       
    53.     @Test  
    54.     public void launchNotePad() throws InterruptedException{  
    55.           
    56.         WebElement el = null;  
    57.         WebElement screen = null;  
    58.         Point point = null;  
    59.         Dimension size = null;  
    60.           
    61.         boolean found = false;  
    62.         int pageCount = 3; //Assume that there are totally 3 screens to be swiped.  
    63.           
    64.         int xStart = -1;  
    65.         int yStart = -1;  
    66.         int xEnd = -1;  
    67.         int yEnd = -1;  
    68.           
    69.         //Get the start and end coordinates for swipe  
    70.         Thread.sleep(3000);  
    71.         screen = driver.findElementById("com.miui.home:id/cell_layout");  
    72.         point = screen.getLocation();  
    73.         size = screen.getSize();  
    74.         xEnd = point.getX();  
    75.         yEnd = point.getY() + size.getHeight()/2;  
    76.         xStart = point.getX() + size.getWidth() - 5;  
    77.         yStart = yEnd;  
    78.           
    79.         System.out.println("starX:" + xStart +" startY:" + yStart + " endX:" + xEnd + " endY:" + yEnd);  
    80.           
    81.         //本来想通过判断屏幕上的几个小圆点来判断究竟有多少个屏幕的,但发觉xPath根本不起效,父目录感觉根本起不了定位作用,只有最后的//android.widget.ImageView起效,所以一下找出75个元素。  
    82.         /* 
    83.         List<WebElement> pageImages = driver.findElementsByXPath("//android.view.View/android.widget.LinearLayout/android.widget.ImageView"); 
    84.         assertThat(pageImages.size(),is(3)); 
    85.         for (WebElement e: pageImages) { 
    86.             e.click(); 
    87.         } 
    88.         */  
    89.           
    90.         //Swipe all screens till get the expected control  
    91.         int currentPage = 0;      
    92.         while (found == false && currentPage < pageCount) {  
    93.             found = true;  
    94.               
    95.             currentPage += 1;  
    96.               
    97.             try {  
    98.                 el = driver.findElementByName("Notes");  
    99.             }catch (NoSuchElementException e) {  
    100.                 found = false;  
    101.                 System.out.println(e);  
    102.             }  
    103.               
    104.             if (found == true)  
    105.                 break;  
    106.               
    107.             driver.swipe(xStart, yStart, xEnd, yEnd, 100);  
    108.             Thread.sleep(1000);  
    109.         }  
    110.           
    111.         assertThat(found,is(true));  
    112.         assertThat(el,notNullValue());  
    113.           
    114.         el.click();  
    115.           
    116.           
    117.           
    118.     }  
    119.       
    120. }  
    步骤说明大概如下:
    • 准备好必须的capabilities传送给appium服务器端,注意指定app,因为我们不需要重新安装Launcher
    • 找到代表整个屏幕的控件,然后通过获取它的location和size属性来计算出滑动开始和结束的坐标。注意开始的坐标如果是屏幕的边界,需要调整下像素(例子中是减去5个像素)以防出错。
    • 通过滑动遍历每个页面直到找到目标控件为止。
  • 相关阅读:
    javascript运行机制
    ios-scroll 和系统设置overflowscroll后卡顿
    input属性autocomplate背景颜色
    img 的onload事件和complate事件区别
    image图片之间缝隙bug解决方法
    gulp使用指南
    getQueryString
    decodeURI()和decodeURIComponent()函数
    css样式实现水平方向滚动
    I2C
  • 原文地址:https://www.cnblogs.com/xiaoluosun/p/4519229.html
Copyright © 2011-2022 走看看