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

上篇文章有讲述如何通过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来说有巨大的优势:

  • 不需要通过Dart编写TestCase,可以支持Java、Python、nodeJs等语言
  • 支持Flutter嵌套webview页面或者原生页面,或者是原生嵌套Flutter页面的自动化测试
  • 支持并发执行
  • 支持云测试平台,比如Sauce Labs

安装Appium Flutter Driver

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

使用Appium-Desktop的同学

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

使用npm方式安装Appium的同学
  1. 安装node.js https://nodejs.org/en/

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

    python 复制代码
    #设置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()

python 复制代码
void main() {
  enableFlutterDriverExtension();
  runApp(MyApp());
}
Step2:构建debug模式App包

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

编写Java测试脚本

Step1:创建Maven工程

pom文件依赖配置如下:

javascript 复制代码
<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)

Step3:编写TestCase
java 复制代码
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();
    }
}

执行效果:

编写Python测试脚本

Step1:下载Appium-Flutter-Finder for Python客户端库,官方提供了三种方式:
  • 从PyPi官方仓库安装

    python 复制代码
    pip install Appium-Flutter-Finder
  • https://pypi.org/project/Appium-Flutter-Finder/下载解压安装

    bash 复制代码
    tar -xvf Appium-Flutter-Finder-X.X.tar.gz
    cd Appium-Flutter-Finder-X.X
    python setup.py install
  • 从Github下载安装

    bash 复制代码
    git clone git@github.com:appium/python-client.git
    cd python-client
    python setup.py install
Step2:编写TestCase
python 复制代码
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()

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

作者:shakebabe

链接:http://testingpai.com/article/1596527703036