App自动化测试 —— Appium的使用

简介

Appium是一个开源测试自动化框架,用于原生、 混合和移动 Web 应用程序。

安装

Appium安装方式有两种,一种是通过npm命令行安装,另一种是通过安装可视化工具。

  • npm安装

    npm安装方式稍微麻烦一点,需要先安装npm才能进行操作,想了解的可参考:安装Appium

  • 可视化工具安装

    可视化工具的下载可前往Github下载

本文使用的是 可视化工具安装 方式。

配置 & Run

安装完成后启动Appium.exe,点击编辑配置

在这里插入图片描述
在弹出的配置界面中填写已提前配置好的环境变量路径并保存重新启动

在这里插入图片描述
重启后点击启动服务器

在这里插入图片描述
点击右上角的启动检查器会话

在这里插入图片描述
在这里插入图片描述
点击中间偏下方的 “ + ” 号添加参数

Capability Description
platformName 设备操作系统
platformVersion 设备操作系统版本
deviceName 设备名称(可通过执行adb的adb devies获取当前连接上的设备)
appPackage 应用程序包名
appActivity 从appPackage 中启动的 Android 活动的活动名称
noReset true or false,执行程序的同时是否清空app数据

更多Capability可参考:Appium Desired Capabilities

填写部分参数如下:

在这里插入图片描述
点击启动会话后弹出如下窗口:

在这里插入图片描述
Appium可以通过录制操作,生成JS、Java、Python、Ruby等程序可运行的语言。具体操作如:开始操作 → 点击app界面的某一个按钮或输入框 → 点击后会在右侧的 选定元素 中显示相关信息 → 选择 点击 或 发送密钥 的操作 → 刷新屏幕截图。

在这里插入图片描述
部分选择元素是无法选中的,需要使用坐标定位所点击位置。录制完相关操作,将录制生成的代码复制到剪贴板,粘贴至Android Studio 开发工具 @Test注解的方法里。

在这里插入图片描述
run 一下,自动化UI测试的操作步骤就完成了

在这里插入图片描述
Android Studio里面执行代码,应提前引入commons-lang3java-clientselenium-server-standalone三个jar包,下载地址在文章尾部。部分Java代码如下:

	@Before
    public void initAppium() {
    
    
        File classpathRoot = new File(System.getProperty("user.dir"));
        // 获取apps文件,前面测试应用所存放的目录
        File appDir = new File(classpathRoot, "/test_app/");
        // 获取apk文件
        File app = new File(appDir, "AppiumAutoTest.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "测试的设备系统");
        capabilities.setCapability("platformVersion", "系统平台版本号");
        capabilities.setCapability("deviceName", "测试的设备名称");
        capabilities.setCapability("app", app.getAbsolutePath());
        capabilities.setCapability("appPackage", "被测应用包名");
        capabilities.setCapability("appActivity", "app启动的第一个运行界面");
        // 连接appium启动相应app
        try {
    
    
            driver = new AndroidDriver<>(new URL("http://192.168.31.98:4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
    
    
            e.printStackTrace();
        }
        System.out.println("App is launched!");
    }

    @Test
    public void startTest() throws InterruptedException {
    
    
        MobileElement el3 = (MobileElement) driver.findElementByAccessibilityId("Reflow");
        el3.click();
        sleep(1000);
        MobileElement el4 = (MobileElement) driver.findElementByAccessibilityId("Slideshow");
        el4.click();
        sleep(1000);
        MobileElement el5 = (MobileElement) driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/androidx.drawerlayout.widget.DrawerLayout/android.view.ViewGroup/android.widget.LinearLayout[2]/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.view.ViewGroup");
        el5.click();
        sleep(1000);
        execRootCmdSilent("adb shell input tap 958 1588");
        sleep(1000);
        MobileElement el6 = (MobileElement) driver.findElementByAccessibilityId("更多选项");
        el6.click();
        sleep(1000);
        MobileElement el7 = (MobileElement) driver.findElementById("com.miyue.appiumautotest:id/title");
        el7.click();
        sleep(1000);
        MobileElement el8 = (MobileElement) driver.findElementByAccessibilityId("转到上一层级");
        el8.click();
    }
    
	private void sleep(int second) throws InterruptedException {
    
    
        Thread.sleep(second);
    }

    /**
     * 使用adb命令执行点击的坐标点
     * @param paramString
     */
    public void execRootCmdSilent(String paramString) {
    
    
        String content = "";
        BufferedReader reader = null;
        InputStream is = null;
        try {
    
    
            java.lang.Process process = Runtime.getRuntime().exec(paramString);
            is = process.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is));
            StringBuffer output = new StringBuffer();
            int read;
            char[] buffer = new char[4096];
            while ((read = reader.read(buffer)) > 0) {
    
    
                output.append(buffer, 0, read);
            }
            content = output.toString();
            System.out.println("App execution adb done!" + content);
        } catch (Exception e) {
    
    
            e.printStackTrace();
            System.out.println("App issue!" + e.getMessage());
        } finally {
    
    
            if (null != is) {
    
    
                try {
    
    
                    is.close();
                } catch (Exception e) {
    
    
                    e.printStackTrace();
                }
            }
            if (reader != null) {
    
    
                try {
    
    
                    reader.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 运行完成
     * @throws InterruptedException
     */
    @After
    public void end() throws InterruptedException {
    
    
        // 退出应用
        driver.quit();
    }

问题 & 解决方案

  • no tasks available.

    原因:没有配置Run/Debug Configurations

  • Connection refused: connect.

    原因Appium服务未启动。

  • An unknown server-side error occurred while processing the command. Original error: Unable to find an active device or emulator with OS 11. The following are available: 6HJ4C19A29009173 (10).

    原因platformVersion 输入的操作系统与连接到的操作系统版本不对。

  • An unknown server-side error occurred while processing the command. Original error: Could not find a connected Android device in 20088ms.

    原因:未连接到测试设备,需要使用adb或数据线连接上设备,执行adb命令adb devices查看目前所连接到的设备,将设备名称拷贝粘贴至deviceName

  • 重新编写测试步骤的代码,应该提前clean运行缓存。

优点 & 缺点

优点

  • 支持第三方应用程序。
  • 支持同时测试多台设备。
  • 支持生成多种语言的测试代码。
  • 不需要把第三方代码编译进app中。
  • 解放了双手,避免了不必要的重复操作。
  • 支持IOS、Android、Windows的应用程序。
  • 支持Native apps、 Hybrid apps、Web apps。

缺点

  • 暂不支持可用于编写Android程序的Kotlin代码。
  • 相对于人工测试而言,Appium自动测试不太适合小规模的测试。

总结

噢,相信你体验之后肯定会刷新你对Appium的看法。

若测试的应用不需要进行多次重复性的UI测试,似乎没必要使用Appium自动化测试,与手动点击UI测试相比,速度慢的似乎不是一丁半点。

最近了解到一个叫AccessibilityService(无障碍服务)的东西,这玩意可以用来开发Application脚本,脚本执行速度那叫一个快。只可惜它不是一个自动化测试的框架,不然用来搞自动化测试应该很牛掰的吧。

本文Android Studio代码下载地址:Appium自动化测试的使用代码下载地址

参考资料

1、Appium官网
2、Appium下载地址
3、java-client.jar包下载地址
4、commons-lang3.jar包下载地址
5、Appium搭建Android自动化测试框架
6、selenium-server-standalone.jar包下载地址

猜你喜欢

转载自blog.csdn.net/baidu_41616022/article/details/126983066