AndroidTest.java

博观而约取,厚积而薄发。这篇文章主要讲述AndroidTest.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.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 20private AppiumDriver< WebElement> driver; 21 22@Before 23public void setUp() throws Exception { 24//File classpathRoot = new File(System.getProperty("user.dir")); 25File appDir = new File("E:/package"); 26File app = new File(appDir, "ApiDemos-debug.apk"); 27DesiredCapabilities capabilities = new DesiredCapabilities(); 28capabilities.setCapability("deviceName","Android Emulator"); 29capabilities.setCapability("platformVersion", "4.4"); 30capabilities.setCapability("app", app.getAbsolutePath()); 31capabilities.setCapability("appPackage", "io.appium.android.apis"); 32capabilities.setCapability("appActivity", ".ApiDemos"); 33driver = new AndroidDriver< > (new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 34} 35 36@After 37public void tearDown() throws Exception { 38driver.quit(); 39} 40 41@Test 42public void apiDemo(){ 43WebElement el = driver.findElement(By.xpath(".//*[@text=‘Animation‘]")); 44assertEquals("Animation", el.getText()); 45el = driver.findElementByClassName("android.widget.TextView"); 46assertEquals("API Demos", el.getText()); 47el = driver.findElement(By.xpath(".//*[@text=‘App‘]")); 48el.click(); 49List< WebElement> els = driver.findElementsByClassName("android.widget.TextView"); 50assertEquals("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原文地址:https://discuss.appium.io/t/difference-between-webelement-mobileelement-androidelement-and-ioselement/5254/4
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
【AndroidTest.java】 


    推荐阅读