原文地址: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 编码实现
- 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;
-
- public class LauncherTest {
-
- private AndroidDriver driver;
-
- @Before
- public void setUp() throws Exception {
-
-
-
-
- DesiredCapabilities capabilities = new DesiredCapabilities();
- capabilities.setCapability("deviceName","Android");
-
-
-
- capabilities.setCapability("appPackage", "com.miui.home");
- capabilities.setCapability("appActivity", "com.miui.home.launcher.Launcher");
-
-
-
- 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;
-
- int xStart = -1;
- int yStart = -1;
- int xEnd = -1;
- int yEnd = -1;
-
-
- 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);
-
-
-
-
-
- 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以滑动窗体获得目标应用
原文:http://www.cnblogs.com/xiaoluosun/p/4519229.html