AndroidSlideTest.java
以下代码使用ApiDemos-debug.apk进行测试
1 package com.saucelabs.appium; 2 3 import io.appium.java_client.AppiumDriver; 4 import io.appium.java_client.android.AndroidDriver; 5 import org.junit.After; 6 import org.junit.Before; 7 import org.junit.Test; 8 import org.openqa.selenium.Dimension; 9 import org.openqa.selenium.Point; 10 import org.openqa.selenium.WebElement; 11 import org.openqa.selenium.remote.DesiredCapabilities; 12 13 import java.io.File; 14 import java.net.URL; 15 16 /** 17 * Created by saikrisv on 26/04/16. 18 */ 19 public class AndroidSlideTest { 20 private AppiumDriver<WebElement> driver; 21 22 @Before 23 public void setUp() throws Exception { 24 //File classpathRoot = new File(System.getProperty("user.dir")); 25 File appDir = new File("E:/package"); 26 File app = new File(appDir, "ApiDemos-debug.apk"); 27 DesiredCapabilities capabilities = new DesiredCapabilities(); 28 capabilities.setCapability("deviceName","Android Emulator"); 29 capabilities.setCapability("platformVersion", "4.4"); 30 capabilities.setCapability("app", app.getAbsolutePath()); 31 capabilities.setCapability("appPackage", "io.appium.android.apis"); 32 capabilities.setCapability("appActivity", ".ApiDemos"); 33 driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 34 } 35 36 @After 37 public void tearDown() throws Exception { 38 driver.quit(); 39 } 40 41 @Test 42 public void testSlider(){ 43 driver.findElementByXPath(".//*[@text='Views']").click(); 44 driver.scrollTo("Seek Bar").click(); 45 46 WebElement slider = driver.findElementById("io.appium.android.apis:id/seek"); 47 Point sliderLocation = getCenter(slider); 48 driver.swipe(sliderLocation.getX(), sliderLocation.getY(), sliderLocation.getX()-100, sliderLocation.getY(), 7000); 49 50 } 51 52 private Point getCenter(WebElement element) { 53 54 Point upperLeft = element.getLocation(); 55 Dimension dimensions = element.getSize(); 56 return new Point(upperLeft.getX() + dimensions.getWidth()/2, upperLeft.getY() + dimensions.getHeight()/2); 57 } 58 }
用到的定位方式:
Xpath: WebElement slider = driver.findElementById("io.appium.android.apis:id/seek");
ID: WebElement slider = driver.findElementById("io.appium.android.apis:id/seek");
用到的操作:
scrollTo,click: driver.scrollTo("Seek Bar").click(); //自动滑动屏幕,将不在当前屏幕的内容移到当前屏幕中,然后点击。
getLocation: Point upperLeft = element.getLocation(); //获取元素的位置,实际上是元素矩形框左上角的位置。
getSize: Dimension dimensions = element.getSize(); //获取元素矩形框的长度和宽度。
swipe: driver.swipe(sliderLocation.getX(), sliderLocation.getY(), sliderLocation.getX()-100, sliderLocation.getY(), 7000); //在指定时间内,从屏幕一点滑动到另一点。