Flutter自动化GUI测试插件dart_autogui的使用

Flutter自动化GUI测试插件dart_autogui的使用

Dart Autogui 是一个基于 Python 的 autogui 模块的包装器。要使此包正常工作,您的计算机上需要安装 Python、pyautoguipyscreeze

安装

Python 包安装

pip3 install pyautogui pyscreeze

Dart 包安装

pubspec.yaml 文件中添加以下依赖:

dependencies:
  dart_autogui: 0.0.1

验证 dart_autogui 是否安装成功:

print(await Validate.installed());
// 如果所有依赖项都已安装,返回 true;否则返回 false

功能

鼠标功能

getPosition() 
moveTo() 
moveRel() 
dragTo() 
dragRel() 
click() 
rightClick() 
middleClick() 
doubleClick() 
tripleClick() 
scroll()
mouseDown()
mouseUp()
onLeftClick()
onRightClick()

键盘功能

keyboardKeys() 
typeWord() 
hotKey() 

示例

以下是一个完整的示例代码,展示了如何使用 dart_autogui 进行自动化 GUI 测试。

import 'package:dart_autogui/dart_autogui.dart';

// 获取鼠标当前位置
Future<void> pos() async {
  final position = await Mouse.pos();
  print("X: ${position.x}, Y: ${position.y}");
}

// 将鼠标移动到指定位置
Future<void> moveTo() async {
  await Mouse.moveTo(
    x: 100,
    y: 100,
    duration: Duration(seconds: 5), // 移动时间为 5 秒
    tween: MouseTween.easeInElastic, // 使用弹性动画效果
  );
}

// 相对移动鼠标位置
Future<void> moveRel() async {
  await Mouse.moveRel(
    x: 100,
    y: 100,
    duration: Duration(seconds: 5),
  );
}

// 拖动鼠标到指定位置
Future<void> dragTo() async {
  await Mouse.dragTo(
    x: 100,
    y: 100,
    duration: Duration(seconds: 5),
    tween: MouseTween.easeInElastic,
    button: MouseButton.right, // 使用右键拖动
  );
}

// 相对拖动鼠标
Future<void> dragRel() async {
  await Mouse.dragRel(
    x: 100,
    y: 100,
    duration: Duration(seconds: 2),
    tween: MouseTween.easeInElastic,
  );
}

// 右键点击
Future<void> rightClick() async {
  await Mouse.rightClick(
    x: 100,
    y: 100,
    clicks: 5, // 点击次数为 5 次
    tween: MouseTween.easeInQuad, // 使用二次方动画效果
  );
}

// 获取键盘按键信息
Future<void> keyboardKeys() async {
  final data = await Keyboard.keyboardKeys();
  data.forEach((element) {
    print(element);
  });
}

// 输入文字
Future<void> typeWord() async {
  await Keyboard.typeWord('Adib Mohsin', interval: 0); // 每个字符间隔时间为 0 秒
}

// 执行快捷键组合
Future<void> hotkey() async {
  await Keyboard.hotKey(['ctrl', 'v']); // 执行 Ctrl + V 快捷键
}

// 验证安装是否成功
Future<void> validateInstall() async {
  print(await Validate.installed());
}

void main() async {
  // 验证安装
  validateInstall();

  // 调用其他函数进行测试
  pos();
  moveTo();
  moveRel();
  dragTo();
  dragRel();
  rightClick();
  keyboardKeys();
  typeWord();
  hotkey();
}
1 回复

更多关于Flutter自动化GUI测试插件dart_autogui的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


dart_autogui 是一个用于 Flutter 的自动化 GUI 测试插件,它允许你通过 Dart 代码模拟用户的鼠标和键盘操作。这个插件非常适合用于自动化测试、UI 自动化脚本以及其他需要模拟用户交互的场景。

以下是如何使用 dart_autogui 的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 dart_autogui 依赖:

dependencies:
  flutter:
    sdk: flutter
  dart_autogui: ^0.0.1  # 请查看最新版本

然后运行 flutter pub get 来获取依赖。

2. 导入包

在你的 Dart 文件中导入 dart_autogui

import 'package:dart_autogui/dart_autogui.dart';

3. 初始化 AutoGUI

在使用 dart_autogui 之前,你需要初始化 AutoGUI 对象:

final autogui = AutoGUI();

4. 模拟鼠标操作

dart_autogui 提供了多种鼠标操作,例如移动鼠标、点击、拖动等。

移动鼠标

autogui.mouse.moveTo(100, 100);  // 将鼠标移动到屏幕坐标 (100, 100)

点击鼠标

autogui.mouse.click();  // 在当前位置点击鼠标左键

右键点击

autogui.mouse.rightClick();  // 在当前位置点击鼠标右键

拖动鼠标

autogui.mouse.dragTo(200, 200);  // 从当前位置拖动鼠标到 (200, 200)

5. 模拟键盘操作

dart_autogui 也支持模拟键盘操作,例如按键、输入文本等。

按键

autogui.keyboard.pressKey(KeyCode.enter);  // 按下 Enter 键

输入文本

autogui.keyboard.type('Hello, World!');  // 输入文本 "Hello, World!"

6. 其他功能

dart_autogui 还提供了一些其他功能,例如获取屏幕分辨率、截屏等。

获取屏幕分辨率

final size = autogui.screen.size;
print('Screen size: ${size.width}x${size.height}');

截屏

final image = autogui.screen.capture();
// 你可以将 image 保存为文件或进行其他处理

7. 示例代码

以下是一个完整的示例,展示如何使用 dart_autogui 进行简单的鼠标和键盘操作:

import 'package:dart_autogui/dart_autogui.dart';

void main() async {
  final autogui = AutoGUI();

  // 移动鼠标到 (100, 100) 并点击
  autogui.mouse.moveTo(100, 100);
  autogui.mouse.click();

  // 输入文本
  autogui.keyboard.type('Hello, World!');

  // 按下 Enter 键
  autogui.keyboard.pressKey(KeyCode.enter);

  // 获取屏幕分辨率
  final size = autogui.screen.size;
  print('Screen size: ${size.width}x${size.height}');
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!