appium自动化 - android

Posted 我的小花园

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了appium自动化 - android相关的知识,希望对你有一定的参考价值。

1. 获取driver

appium通过生成driver来识别和操作app的UI元素。生成driver时,需要给出被测设备的相关信息。appium官方上的例子如下:

https://github.com/appium-boneyard/sample-code/blob/master/sample-code/examples/java/appium-generic-test/src/test/java/AndoridBrowserLocalTest.java

    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获得。

以上是关于appium自动化 - android的主要内容,如果未能解决你的问题,请参考以下文章

appium+python自动化60-windows上同时启动多个appium服务,让多个android机器并行运行

使用appium同时支持iOS和android的case结构及jenkins job配置

Android + Appium 自动化测试完整的环境配置及代码详解

教你用Appium搭建Android自动化测试框架(详细教程)

Appium自动化时,如何快速获得Android app的包名和启动页

彻底解决appium 自动化测试时总是自动安装appium android input manager for unicode