Appium測試ContactManager.apk

21ca發表於2017-05-04

啟動 虛擬裝置參考:Appium筆記


下載 ContactManager.apk

https://raw.githubusercontent.com/appium/sample-code/master/sample-code/apps/ContactManager/ContactManager.apk

安裝ContactManager.apk
C:\Program Files (x86)\Android\android-sdk\platform-tools>adb install D:\xxx\ContactManager.apk

Java程式碼:
  1. import java.net.MalformedURLException;
  2. import java.net.URL;
  3. import java.util.List;

  4. import org.openqa.selenium.WebElement;
  5. import org.openqa.selenium.remote.CapabilityType;
  6. import org.openqa.selenium.remote.DesiredCapabilities;

  7. import io.appium.java_client.MobileElement;
  8. import io.appium.java_client.android.AndroidDriver;

  9. public class ContactManagerAppTest {

  10.     public static void main(String[] args) throws MalformedURLException {
  11.         DesiredCapabilities capabilities = new DesiredCapabilities();
  12.         capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
  13.         capabilities.setCapability("platformName", "Android");
  14.         capabilities.setCapability("deviceName", "Android Emulator");
  15.         capabilities.setCapability("platformVersion", "5.1.1");
  16.         capabilities.setCapability("appPackage", "com.example.android.contactmanager");
  17.         capabilities.setCapability("appActivity", ".ContactManager");

  18.         AndroidDriver<MobileElement> driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);


  19.         WebElement el = driver.findElementByAndroidUIAutomator("text(\"Add Contact\")");
  20.         el.click();
  21.         List<MobileElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");
  22.         textFieldsList.get(0).sendKeys("Some Name");
  23.         textFieldsList.get(2).sendKeys("Some@example.com");
  24.         driver.swipe(100, 500, 100, 100, 2);
  25.         driver.findElementByAndroidUIAutomator("text(\"Save\")").click();
  26.     
  27.         driver.quit();

  28.     }

  29. }

一些adb命令
傳檔案到裝置:adb push D:\xxx\ContactManager.apk /data
啟動shell: adb shell
adb remount

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10742815/viewspace-2138392/,如需轉載,請註明出處,否則將追究法律責任。

相關文章