appium-flutter-driver 測試flutter項目


1.安裝Appium:

安裝就按照官網的就可以了:

http://appium.io/docs/en/about-appium/getting-started/?lang=zh

2.安裝appium-flutter-driver:

npm i -g appium-flutter-driver

如果后面會提示找不到  appium-flutter-finder  ,那就在運行:

npm i -g appium-flutter-finder

git地址:https://github.com/truongsinh/appium-flutter-driver 

3.測試

環境搭建完畢,就可以開始測試了:

注意:被測試的flutter的app需要是debug或者是profiling包,不可以是realease包。

step1:在pubspec.yaml文件中,新增

dev_dependencies:
  flutter_test:
    sdk: flutter

  test: any
  flutter_driver:
    sdk: flutter

step2:在runApp()前追加  enableFlutterDriverExtension(); 

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

step3:編寫最簡單的測試腳本:

// import * as wdio from 'webdriverio';
// import * as assert from 'assert';
const wdio = require('webdriverio');
const assert = require('assert');
const find = require('appium-flutter-finder');


const opts = {
  port: 4723,
  capabilities: {
    platformName: "Android",
    platformVersion: "10",
    deviceName: "one plus",
    app: "D:/Tests/Appium/flutter_test_demo.apk",
    automationName: "Flutter"
  }
};

(async () => {

  // byValueKey是flutter driver的api,用於通過key來查找Widgets,key只能是String或者是int
  const buttonFinder = find.byValueKey('FloatingActionButton');

  // 加載測試的配置
  const driver = await wdio.remote(opts);
  
  // 點擊按鈕
  await driver.elementClick(buttonFinder);
})();

測試結果很明顯:自動加1了唄。

注意: automationName: "Flutter" 這代碼一定要加上,不然會報各種 not implement.

更多的測試案例,可以參考:

https://github.com/truongsinh/appium-flutter-driver/blob/master/example/nodejs/src/index.js

 

附上flutter代碼:

import 'package:flutter/material.dart';
import 'package:flutter_driver/driver_extension.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        key: Key('FloatingActionButton'),
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

 

原理:

Appium Flutter driver is a cross-platform test automation tool for flutter applications. It is a part of the automation tool for the Appium mobile test. But how does the driver for Appium Flutter work? To instantiate the flutter engine, it uses WebSocket communication to hold the WDIO script, and the request will be sent to the particular application. The AUT will then submit the script response via the Appium Flutter driver.

pasted image 0 (2)

 來自:https://www.fleekitsolutions.com/automating-flutter-app-appium-flutter-driver/

簡單的說,其實真正在測試的是flutter driver,就是flutter的測試工具,appium-flutter-driver類似於轉發器,把腳本命令發給flutter driver去測試。

 

That is all.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM