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


免責聲明!

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



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