1. 获取driver
appium通过生成driver来识别和操作app的UI元素。生成driver时,需要给出被测设备的相关信息。appium官方上的例子如下:
public static AndroidDriver<?> mobiledriver; @BeforeTest public void beforeTest( ) throws MalformedURLException { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(MobileCapabilityType.APPIUM_VERSION, "1.7.2"); capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4"); capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android"); capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,"Appium"); capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator"); capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Browser"); capabilities.setCapability("newCommandTimeout", 2000); mobiledriver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); }
Android 第三方被测app举例:
public static AndroidDriver<?> driver1; public static void main(String[] args) { //set up Appium DesiredCapabilities capabilities1 = new DesiredCapabilities(); capabilities1.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2"); capabilities1.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); capabilities1.setCapability(MobileCapabilityType.PLATFORM_VERSION, "9.0.0"); capabilities1.setCapability(MobileCapabilityType.DEVICE_NAME, "PCT-AL10"); capabilities1.setCapability(MobileCapabilityType.UDID, "xxxx"); capabilities1.setCapability("appPackage", "com.xxxx.xxx.demo"); capabilities1.setCapability("appActivity", ".MainActivity"); //for input Chinese capabilities1.setCapability("unicodeKeyboard", true); capabilities1.setCapability("resetKeyboard", true); try { driver1 = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities1); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
如何获取以上信息呢?
- MobileCapabilityType.APPIUM_VERSION: appium服务器的版本。可通过命令行appium -version 或 安装的appium server客户端查看。
- MobileCapabilityType.PLATFORM_VERSION: 操作系统版本。Android可通过命令行 adb -s xxxx(欲连接的deviceID) shell getprop ro.build.version.release来查看。
- MobileCapabilityType.PLATFORM_NAME: Android 或 iOS。
- MobileCapabilityType.DEVICE_NAME: 被测设备名。Android可通过命令 adb -s xxxx shell getprop ro.product.model 查看。
- appPackage: 被测app包名。Android可通过命令 adb shell pm list packages | grep xxxx来查看。
- appActivity: 被测app activity名。可通过sdk里带的uiautomatorviewer查看。
- MobileCapabilityType.UDID: 被测设备id。Android可通过命令 adb devices获得。