zoukankan      html  css  js  c++  java
  • linux的桌面应用、PC应用、软件自动化测试-pyautogui

    鄙人也是第一次:

      查了资料都是手机APP、Windows应用程序的自动化,为什么没有Linux应用程序的自动化测试?探索期间有个Airtest网易出的自动化测试功能强大,但是还不支持Linux,娃娃哈哈哈~

    然后了解到了python中的pyautogui跨平台自动化框架。自动输入、自动点击都自动了,但是如何断言啊,我还想使用unittest框架生成测试报告

      苦思:就是把pyautogui官方文档好好看看,绝对有你需要的方法~有了方法在哪里使用就看你自己了。

      断言:预先把结果截图,比如密码不能为空,点击提交时会有提示:密码不能为空。那我们就先把密码不能为空这个提示截图,然后再使用pyautogui中的Locate Functions 去断言

    附上我的源码:

      

    
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time : 2020/6/2 下午2:58
    # @Author : AiShuiShui-艾谁谁
    # @Site :
    # @File : test_login.py
    import os
    import sys
    sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) # 解决在服务器导包错误
    import unittest
    from assertpy import assert_that # 很好的断言工具
    from log import logging1 # 自己写的log对象
    import subprocess # 用来异步执行linux命令很好用
    import airtest # 一款基于图像识别和poco控件识别的一款UI自动化测试工具
    import time
    import pyautogui
    import os

    import importlib # 重新加载包
    importlib.reload(logging1)

    import configparser # 加载配置文件
    cf = configparser.ConfigParser()
    cf.read('./config.ini')
    client_app_name = cf.get("server", "app_name")
    deb_path = cf.get("client", "client_deb_file") # 你的包需要放的位置,按需修改

    pyautogui.FAILSAFE = True # 鼠标移动到左上角停止程序
    pyautogui.PAUSE = 1 # 执行每个函数后停止1s
    logger = logging1.get_logger()

    import HTMLTestRunner
    import warnings # 解决警告问题

    importlib.reload(logging1) # 更新log文件后重新加载

    pyautogui.FAILSAFE = True # 鼠标移动到左上角停止程序
    pyautogui.PAUSE = 1 # 执行每个函数后停止1
    logger = logging1.get_logger()

    start_cmd = 'client'
    end_cmd = 'pkill client'
    password = "××××"


    class TestLogIn(unittest.TestCase):
    def setUp(self) -> None:
    warnings.simplefilter('ignore', ResourceWarning) # 解决警告
    """
    先打开应用程序
    :return:
    """
    cmd = start_cmd
    subprocess.Popen(cmd)
    time.sleep(2)

    def tearDown(self) -> None:
    warnings.simplefilter('ignore', ResourceWarning)
    """
    每次测试完毕,都需要关闭应用程序
    :return:
    """
    cmd = end_cmd
    os.system(cmd)

    def test_1(self):
    """
    判断页面元素完整性
    :return:
    """
    login_ui =
    pyautogui.locateOnScreen('../screenshot/2020-06-08_16-27.png', confidence=0.9) # 如果没有找到则返回none,如果是none,那意味这有问题 confidence允许稍微的色素差
    pyautogui.screenshot("../result_screenshot/login_ui.png")
    assert_that(login_ui).is_not_equal_to(None) # 不为none才是正确的结果

    def test_2(self):
    """
    插入加密狗,输入空的密码,提示密码不能为空
    :return:
    """
    buttonkmslocation = pyautogui.locateOnScreen("../screenshot/请输入登录密码.png", confidence=0.9)

    # 获取中心点(结合上面)
    buttonkmspoint =
    pyautogui.center(buttonkmslocation)
    print(buttonkmspoint)

    # 获取到中心点后点击,
    pyautogui.click(buttonkmspoint)
    time.sleep(1)
    # 不输入密码点击登录
    pyautogui.moveRel(0, 50)
    pyautogui.click()
    # 当前页面是不是包含密码不能为空
    buttonkmslocation1 = pyautogui.locateOnScreen("../screenshot/密码不能为空.png", confidence=0.9)
    print(buttonkmslocation1) # 如果不为空就证明找到了
    pyautogui.screenshot("../result_screenshot/密码不能为空.png")
    assert_that(buttonkmslocation1).is_not_equal_to(None)
    time.sleep(1)

    def test_3(self):
    """
    插入加密狗,输入的错误的密码,提示密码错误
    :return:
    """
    buttonkmslocation = pyautogui.locateOnScreen("../screenshot/请输入登录密码.png", confidence=0.9)

    # 获取中心点(结合上面)
    buttonkmspoint = pyautogui.center(buttonkmslocation)
    print(buttonkmspoint)

    # 获取到中心点后点击,
    pyautogui.click(buttonkmspoint)
    time.sleep(1)
    # 输入密码点击登录
    pyautogui.typewrite("123", interval=0.5)
    pyautogui.moveRel(0, 50)
    pyautogui.click()
    # 当前页面包含密码错误提示
    buttonkmslocation1 = pyautogui.locateOnScreen("../screenshot/error_password.png", confidence=0.9)
    print("已经在屏幕上捕获到:", buttonkmslocation1) # 如果不为空就证明找到了
    pyautogui.screenshot("../result_screenshot/密码错误.png")
    assert_that(buttonkmslocation1).is_not_equal_to(None)
    time.sleep(1)

    def test_4(self):
    """
    插入加密狗,输入正确的密码,进入主页面
    :return:
    """
    buttonkmslocation = pyautogui.locateOnScreen("../screenshot/请输入登录密码.png", confidence=0.9)

    # 获取中心点(结合上面)
    buttonkmspoint = pyautogui.center(buttonkmslocation)
    print(buttonkmspoint)

    # 获取到中心点后点击,
    pyautogui.click(buttonkmspoint)
    time.sleep(1)
    # 输入密码点击登录
    pyautogui.typewrite(password, interval=0.5) # interval作用是每输入一个等待0.5s,模拟人输入
    pyautogui.moveRel(0, 50)
    pyautogui.click()
    # 当前页面已经进入主页面包含
    buttonkmslocation1 = pyautogui.locateOnScreen("../screenshot/sn_no.png", confidence=0.9)
    print("已经在屏幕上捕获到:", buttonkmslocation1)
    # 并且截屏一张,方便查看
    pyautogui.screenshot("../result_screenshot/进入登录页面.png")
    assert_that(buttonkmslocation1).is_not_equal_to(None)
    time.sleep(1)


    if __name__ == '__main__':
    suit = unittest.TestSuite()
    suit.addTest(TestLogIn('test_1'))
    suit.addTest(TestLogIn('test_2'))
    suit.addTest(TestLogIn('test_3'))
    suit.addTest(TestLogIn('test_4'))
    now = time.strftime('%m_%d_%H-%M-%S')
    with open("../result_report_html/%s.html" % now, "wb") as fp:
    runner = HTMLTestRunner.HTMLTestRunner(
    stream=fp,
    title="测试报告",
    description="登录报告"
    )
    runner.run(suit)
     

    注意:如果使用了unittest框架的话,一定要在终端执行你的python脚本,要不然你的报告永远都没有办法生成,网上的方法找了,没用哦,都是相互抄袭;

    还有,这个中方法我不能保证100%成功,但是如果有个别失败的,可以单独在pycharm中右击执行一下;

    interval
  • 相关阅读:
    DataTable Clone()方法和Copy()方法的区别
    element-ui的使用
    解决VS Code 软件PowerShell执行策略问题
    Vue-Router
    Vue的生命周期
    vue-cli脚手架和webpack
    Vue组件
    Vue基本用法和指令
    ES6常用语法
    前端-Bootstrap框架
  • 原文地址:https://www.cnblogs.com/tarzen213/p/13067851.html
Copyright © 2011-2022 走看看