以下代码使用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.By; 9 import org.openqa.selenium.WebElement; 10 import org.openqa.selenium.remote.DesiredCapabilities; 11 12 import java.io.File; 13 import java.net.URL; 14 import java.util.List; 15 16 import static org.junit.Assert.assertEquals; 17 18 public class AndroidTest { 19 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 apiDemo(){ 43 WebElement el = driver.findElement(By.xpath(".//*[@text='Animation']")); 44 assertEquals("Animation", el.getText()); 45 el = driver.findElementByClassName("android.widget.TextView"); 46 assertEquals("API Demos", el.getText()); 47 el = driver.findElement(By.xpath(".//*[@text='App']")); 48 el.click(); 49 List<WebElement> els = driver.findElementsByClassName("android.widget.TextView"); 50 assertEquals("Activity", els.get(2).getText()); 51 } 52 53 }
用到的定位方式:
Xpath: WebElement el = driver.findElement(By.xpath(".//*[@text='Animation']"));
ClassName: List<WebElement> els = driver.findElementsByClassName("android.widget.TextView");
用到的操作:
getText: assertEquals("API Demos", el.getText());
click: el.click();
区分:WebElement, MobileElement, AndroidElement, and iosElement
Basically, using WebElement will let you use all the normal Selenium commands. MobileElement is appium's element which subclasses WebElement and adds appium-specific features (like being able to perform Touch Gestures).
AndroidElement and IOSElement implement MobileElement and add OS-specific features. Like on Android you can use findByUIAutomator
and on iOS you can use findByUIAutomation