Flutter 自动化测试 -appium-flutter-driver

本贴最后更新于 1197 天前,其中的信息可能已经时移世改

上篇文章有讲述如何通过FlutterDriver实现集成测试Flutter 应用自动化测试-集成测试篇 不熟悉的小伙伴可以先去看看。

什么是Appium Flutter Driver?

作为Flutter开发,FlutterDriver是足够帮助他们进行测试的,而作为自动化测试工程师最大的困难是对dart语言不了解,不知道如何在FlutterDriver驱动下用Dart语言编写TestCase,所以需要一款更加简单的、不需要学习Dart语言的工具来帮助我们去实现自动化测试,而Appium Flutter Driver满足这一点。

Appium Flutter Driver是一款可以跨平台测试Flutter应用的自动化测试工具,并且是Appium移动端测试工具的一部分。

其项目的Github地址:https://github.com/truongsinh/appium-flutter-driver

FlutterDriver VS Appium Flutter Driver

Appium Flutter Driver对比FlutterDriver对QA来说有巨大的优势:

image.png

安装Appium Flutter Driver

Appium V1.16.0版本或以上才能支持appium-flutter-driver

image.png

使用Appium-Desktop的同学

安装Appium-desktop V1.16.0以上,这里以Appium-Desktop V1.17.0为例,可以看到自带了appium-flutter-driver

image.png

使用npm方式安装Appium的同学

  1. 安装node.js https://nodejs.org/en/

  2. 安装配置Appium环境(版本V1.16.0或以上)

    #设置npm淘宝镜像
    npm config set registry https://registry.npm.taobao.org
    #全局安装appium,设置chromeDriver镜像下载地址
    npm install appium@1.17 -g --chromedriver_cdnurl=http://cdn.npm.taobao.org/dist/chromedriver
    #安装appium-doctor
    npm install appium-doctor -g
    #appium doctor检测环境
    appium-doctor
    

同样可以在Appium目录中找到appium-flutter-driver

打包测试App

由于FlutterDriver不支持release模式的App,所以被测应用需要打包成debug或者profile模式

Step1:开启FlutterDriver扩展模式

在runApp()之前添加enableFlutterDriverExtension()

void main() {
  enableFlutterDriverExtension();
  runApp(MyApp());
}

Step2:构建debug模式App包

在工程根目录下输入flutter build apk --debug

image.png

编写Java测试脚本

Step1:创建Maven工程

pom文件依赖配置如下:

<dependencies>
    <dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>7.0.0</version>
    </dependency>

    <dependency>
        <groupId>org.jetbrains.kotlinx</groupId>
        <artifactId>kotlinx-serialization-runtime</artifactId>
        <version>0.20.0</version>
    </dependency>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.0.0</version>
    </dependency>
</dependencies>

Step2:导入finder源码

由于appium-flutter-finder客户端依赖没有放到Maven中央仓库,我们需要自己导入相关源码(选择kotlin)

image.png

image.png

Step3:编写TestCase

public class FlutterAppTest {
    public static String appPath = "src\\test\\resources\\app-debug.apk";
    protected FlutterFinder find;
    protected AndroidDriver<MobileElement> driver;

    @BeforeTest
    public void setUp() throws MalformedURLException {
        String appAbsolutePath = System.getProperty("user.dir") + File.separator + appPath;
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "08e7c5997d2a");
        capabilities.setCapability("app", appAbsolutePath);
        capabilities.setCapability("platformVersion", "5.1.1");
        capabilities.setCapability("automationName", "Flutter");
        driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        find = new FlutterFinder(driver);
    }

    @Test
    public void countTest() throws InterruptedException {
        Thread.sleep(2000);
        MobileElement counterTextFinder = find.byValueKey("counter");
        MobileElement buttonFinder = find.byValueKey("increment");
        buttonFinder.click();
        buttonFinder.click();
        Assert.assertEquals("2", counterTextFinder.getText());
    }

    @AfterTest
    public void tearDown() throws InterruptedException {
        Thread.sleep(2000);
        driver.quit();
    }
}

执行效果:
image.png

编写Python测试脚本

Step1:下载Appium-Flutter-Finder for Python客户端库,官方提供了三种方式:

Step2:编写TestCase

import unittest
from appium import webdriver
from appium_flutter_finder.flutter_finder import FlutterElement, FlutterFinder

class FlutterTest(unittest.TestCase):
    def setUp(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '5.1.1'
        desired_caps['deviceName'] = '08e7c5997d2a'
        desired_caps['app'] = r'D:\flutter_demo\build\app\outputs\apk\debug\app-debug.apk'
        desired_caps['automationName'] = 'flutter'
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        self.finder = FlutterFinder()

    def test_flutter(self):
        text_finder = self.finder.by_value_key("counter")
        button_finder = self.finder.by_value_key("increment")
        text_element = FlutterElement(self.driver, text_finder)
        button_element = FlutterElement(self.driver, button_finder)
        button_element.click()
        button_element.click()
        self.assertEqual('2',text_element.text)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
     unittest.main()

执行过程和效果同上述一致,这里就不再展示了。

2 操作
shakebabe 在 2021-07-30 13:54:14 更新了该帖
shakebabe 在 2020-08-06 17:20:08 更新了该帖
2 回帖
请输入回帖内容 ...
  • liuxuesheng

    appium-flutter-driver 相关的API基础知识哪里有呀?

  • cj186

    flutter框架开发的APP,如何定位元素?元素总是定位不到!