1. 启动安卓虚拟机。
2. 启动appium:cmd中直接输入appium启动
3. cmd中安装测试apk,adb install apk路径
4. Eclipse创建Java项目,构建路径加入Selenium、appium、Junit
5. 敲入源码:
ContactManager.java
package appTest; import io.appium.java_client.AppiumDriver; import io.appium.java_client.android.AndroidDriver; import java.io.File; import java.net.URL; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; public class ContactManager { private static AppiumDriver<WebElement> driver; @Before public static void setUp() throws Exception { // set up appium File classpathRoot = new File(System.getProperty("user.dir")); File appDir = new File(classpathRoot, "apps"); File app = new File(appDir, "ContactManager-selendroid.apk"); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("deviceName","Android Emulator"); capabilities.setCapability("platformVersion", "4.2.2"); capabilities.setCapability("app", app.getAbsolutePath()); capabilities.setCapability("appPackage", "com.example.android.contactmanager"); capabilities.setCapability("appActivity", ".ContactManager"); URL url = new URL("http://127.0.0.1:4723/wd/hub"); driver = new AndroidDriver<WebElement>(url, capabilities); } @After public static void tearDown() throws Exception { driver.quit(); } @Test public static void addContact() throws InterruptedException{ WebElement el = driver.findElement(By.name("Add Contact")); el.click(); List<WebElement> textFieldsList = driver.findElementsByClassName( "android.widget.EditText"); Thread.sleep(3000); textFieldsList.get(0).sendKeys("tuanfuyao"); Thread.sleep(3000); List<WebElement> spinner = driver.findElementsByClassName( "android.widget.Spinner"); spinner.get(1).click(); Thread.sleep(3000); driver.findElementByName("Work").click(); System.out.println("textFieldsList.size: "+textFieldsList.size()); textFieldsList.get(1).sendKeys("15858589999"); spinner.get(2).click(); Thread.sleep(3000); driver.findElementByName("Mobile").click(); Thread.sleep(3000); textFieldsList.get(2).sendKeys("tuanfuyao@gmail.com"); //Thread.sleep(10000); driver.findElementByName("Save").click(); } public static void main(String args[]) throws Exception{ setUp(); addContact(); tearDown(); } }
6. 执行程序。
7. 使用uiautomatorviewer进行元素定位
8. 获取App包名:aapt dump badging apk路径