searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

云桌面UI自动化工具

2023-08-29 07:40:48
23
0

0 前言

    PyAutoGUI支持Windows、macOS以及Linux三种系统,它的主要特点是可通过图像识别实现UI控件的识别和点击,这点和dogtail是不同的。

1 安装

1.1 Linux

sudo apt-get install -y python3-pip scrot python3-tk python3-dev # [1] scrot用于截屏
sudo pip3 install pyautogui opencv-python # [1] opencv用于支持图像识别的confidence参数

:图像识别若指定confidence参数,需要安装OpenCV,安装过程容易失败,需自行根据提示错误来解决。

 

1.2 Windows

pip install pyautogui

2 基础

(1)SSH远程运行[10]

    SSH远程登录执行需要添加环境变量DISPLAY,可在Python脚本中添加[10]

import os

if os.getenv('DISPLAY') == None: # SSH登录读取该环境变量的结果为None
    os.environ["DISPLAY"] = ":0" # 注意这行代码需要写在"import pyautogui"之前

import pyautogui

    也可以在在命令行中指定:

export DISPLAY=:0

    或启动Python脚本时指定

DISPLAY=:0 test.py

(2)暂停和自动放故障[10]

pyautogui.PAUSE = 1       # 每个执行动作之后,都会等待1s
pyautogui.FAILSAFE = True # 启动自动防故障功能(鼠标移到屏幕的做上角,将导致pyautogui产生pyautogui.FailSafeException异常)在自动化测试项目中,为了不自动跳出用例脚本,通常设置为False

3 鼠标

(1)屏幕大小

pyautogui.size() # [4]

(2)光标坐标

pyautogui.position() # [4]

Tips:可通过上述方式确认需要点击位置的坐标,然后添加到测试脚本中!

 

(3)右键单击

pyautogui.click(button='right')  # right-click the mouse [4]

(4)双击

pyautogui.doubleClick()  # perform a left-button double click [4]

(5)拖拽[4]

pyautogui.dragTo(100, 200, button='left')     # drag mouse to X of 100, Y of 200 while holding down left mouse button
pyautogui.dragTo(300, 400, 2, button='left')  # drag mouse to X of 300, Y of 400 over 2 seconds while holding down left mouse button
pyautogui.drag(30, 0, 2, button='right')   # drag the mouse left 30 pixels over 2 seconds while holding down the right mouse button

4 键盘

(1)快捷键

pyautogui.hotkey('ctrl', 'shift', 'esc')

    等价于

pyautogui.keyDown('ctrl')
pyautogui.keyDown('shift')
pyautogui.keyDown('esc')
pyautogui.keyUp('esc')
pyautogui.keyUp('shift')
pyautogui.keyUp('ctrl')

4 图像识别[10]

# 组合版本
pyautogui.locateCenterOnScreen('calc7key.png') # [7][10]
# 相当于
pyautogui.center(pyautogui.locateOnScreen('calc7key.png')) # [7][10]

# 多个识别目标[7]
for pos in pyautogui.locateAllOnScreen('someButton.png') # [7][10]
    print(pos)

(1)grayscale

    可设置为以灰度图来识别,这回提升识别的速度,但会降低识别准确率[7]

button7location = pyautogui.locateOnScreen('calc7key.png', grayscale=True)

(2)confidence

    可设置识别的准确度[7]

pyautogui.locateOnScreen('calc7key.png', confidence=0.9)

:上述参数依赖OpenCV,没有安装会直接发出异常。[7]

 

案例:银河麒麟做背景切换压测时发现,浅色主题必须配置该参数才能识别背景图标,否则基本都识别失败;深色主题不用该参数也没有太大影响。

 

(2)案例

    银河麒麟压测背景切换时发现下面两种格式都可以点击:

# 方法1
pyautogui.click(pyautogui.locateOnScreen("xxx.png"))

# 方法2
pyautogui.click(pyautogui.center(pyautogui.locateOnScreen("xxx.png")))

# 方法3
pyautogui.click(pyautogui.locateCenterOnScreen("xxx.png"))

参考资料

[1]PyAutoGUI > Docs > Installation
[2]PyAutoGUI > Docs > Cheat Sheet
[3]PyAutoGUI > Docs > Welcome to PyAutoGUI’s documentation!
[4]PyAutoGUI > Docs > Mouse Control Functions
[5]PyAutoGUI > Docs > Keyboard Control Functions
[6]PyAutoGUI > Docs > Message Box Functions
[7]PyAutoGUI > Docs > Screenshot Functions
[8]Python自动操作 GUI 神器——PyAutoGUI
[9]Python基础之pyautogui模块
[10]Python三方库PyAutoGUI的使用方法
[11]桌面GUI自动化测试工具-----pyautogui
[12]linux的桌面应用、PC应用、软件自动化测试-pyautogui
[13]自动化手工操作工具PyAutoGUI 极简使用教程

 

0条评论
0 / 1000
李****海
14文章数
0粉丝数
李****海
14 文章 | 0 粉丝