UI自动化框架搭建之Python3
阅读原文时间:2023年07月11日阅读:3

UI自动化框架搭建--unittest

使用的代码是Python3版本,与时俱进哈哈

解释一下我的框架目录接口(每个人框架的目录接口不一样,根据实际要求)

common目录:公共模块,这个地方可以存放一下公共调用的方法或者文件之类的

log:日志目录,存放执行自动化脚本产生的日志 ,方便定位问题所在,这个是必须有的

report:测试报告目录,每一次执行自动化测试都需要产生一份测试报告,我这边把执行自动化的脚本也放在里面了,以及自动化添加用例套件的方法也放在这里了,主要是嫌麻烦

testcase:测试用例目录,封装好的测试用例

一个很简单的框架目录

本次自动化框架需要的模块主要有:HTMLTestRunner、unittest、logging、pymysq、selenium,其他的就涉及自动化的项目页面需要了

log日志的方法:

# coding:utf-8
import logging,time,os
# 这个是日志保存本地的路径
log_path = "C:\\Users\Administrator\PycharmProjects\\test"
class Log:
def __init__(self):
# 文件的命名
self.logname = os.path.join(log_path, '%s.log'%time.strftime('%Y_%m_%d'))
self.logger = logging.getLogger()
self.logger.setLevel(logging.DEBUG)
# 日志输出格式
self.formatter = logging.Formatter('[%(asctime)s] - %(filename)s[line:%(lineno)d] - fuc:%(funcName)s- %(levelname)s: %(message)s')
def __console(self, level, message):
# 创建一个FileHandler,用于写到本地
fh = logging.FileHandler(self.logname, 'a',encoding="utf-8") # 追加模式
fh.setLevel(logging.DEBUG)
fh.setFormatter(self.formatter)
self.logger.addHandler(fh)

    # 创建一个StreamHandler,用于输出到控制台  
    ch = logging.StreamHandler()  
    ch.setLevel(logging.DEBUG)  
    ch.setFormatter(self.formatter)  
    self.logger.addHandler(ch)  

    if level == 'info':  
        self.logger.info(message)  
    elif level == 'debug':  
        self.logger.debug(message)  
    elif level == 'warning':  
        self.logger.warning(message)  
    elif level == 'error':  
        self.logger.error(message)  
    # 这两行代码是为了避免日志输出重复问题  
    self.logger.removeHandler(ch)  
    self.logger.removeHandler(fh)  
    # 关闭打开的文件  
    fh.close()  

def debug(self, message):  
    self.\_\_console('debug', message)  

def info(self, message):  
    self.\_\_console('info', message)  

def warning(self, message):  
    self.\_\_console('warning', message)  

def error(self, message):  
    self.\_\_console('error', message)  

if __name__ == "__main__":
log = Log()
log.info("---测试开始----")
log.info("输入密码")
log.warning("----测试结束----")

添加测试用例的套件:
文件一:

import unittest

class ParametrizedTestCase(unittest.TestCase):
def __init__(self, methodName='runTest', param1=None, param2=None, param3=None):
super(ParametrizedTestCase, self).__init__(methodName)
self.param1 = param1
self.param2 = param2
self.param3 = param3

@staticmethod  
def parametrize\_case(classname, casename, param1=None, param2=None, param3=None):  
    suite = unittest.TestSuite()  
    suite.addTest(classname(casename, param1=param1, param2=param2, param3=param3))  
    return suite  

@staticmethod  
def parametrize\_class(testcase\_klass, param1=None, param2=None, param3=None):  
    testloader = unittest.TestLoader()  
    testnames = testloader.getTestCaseNames(testcase\_klass)  
    suite = unittest.TestSuite()  
    for name in testnames:  
        suite.addTest(testcase\_klass(name, param1=param1, param2=param2, param3=param3))  
    return suite  

文件二:

# -*-_ coding: utf-8 -*-

import unittest
import time
from selenium import webdriver
import HTMLTestRunner

from common.test_excel import *
from report.Parametrized import *
from testcase.test_login import Login
from testcase.test_webqz import Webqz
from testcase.test_qzvideo import Videpqz
from testcase.test_qzshaixuan import QzShaiXuan
from testcase.test_gerenzx import Gerenzx
from testcase.test_cunzhengqq import CunZhengQQ

driver = webdriver.Chrome()
ceshi_url = "http://192.168.1.1:81/sign"
ceshi2_url = 'http://192.168.1.2:81/attestationsh'
login_name = '18999999999'
login_passwd = 'a123456'

webqz_num = 10
webqz_name = time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time())) +"升级测试"
guochengqz_name = "自动化测试"
guochengqz_biaoqian = "测试"
shaixuanqz_name = "自动化"
baoquanbi_num = 520
gerenzx_qzurl = 'https://www.baidu.com'
cunzheng_biaoqian = '自动化测试'
cunzheng_file_name = '199'

suite = unittest.TestSuite()
suite.addTest(ParametrizedTestCase.parametrize_class(Login, param1=driver,param2=baoquanceshi_url,param3=login_name))
suite.addTest(ParametrizedTestCase.parametrize_case(Webqz,"test_webqz_search", param1=driver,param2=webqz_name,param3=webqz_num))
suite.addTest(ParametrizedTestCase.parametrize_case(Videpqz,"test_video_search", param1=driver,param2=guochengqz_name,param3=guochengqz_biaoqian))
suite.addTest(ParametrizedTestCase.parametrize_class(Gerenzx, param1=driver,param2=baoquanbi_num,param3=webqz_name))
suite.addTest(ParametrizedTestCase.parametrize_class(CunZhengQQ, param1=driver,param2=cunzheng_biaoqian,param3=cunzheng_file_name))
suite.addTest(ParametrizedTestCase.parametrize_class(QzShaiXuan, param1=driver,param2=shaixuanqz_name,param3=login_name))

now = time.strftime("%Y-%m-%d %H_%M_%S")
filename = ".\\" + now + "result.html"
fp = open(filename, "wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title="自动化测试报告")
runner.run(suite)

文件一是一个添加测试用例套件的方法
文件二是调用文件一的方法将测试封装好的测试用例进行添加,从而执行所有测试用例产生测试报告

举一个模拟登录的测试用例脚本

#-*- coding = utf - 8 -*-
from report.Parametrized import ParametrizedTestCase
import time , sys
from log.log_test import Log
import pytesseract
from PIL import Image,ImageEnhance
from selenium import webdriver
log = Log()
screenImg = "D:/checkcode.png"
i = 0

class Login(ParametrizedTestCase):
def test_login_search(self):
self.param1.get(self.param2)
self.param1.maximize_window()
try:
self.param1.find_element_by_name('phoneNumber').send_keys(self.param3)
log.info("输入用户账号:%s" % self.param3)
self.param1.find_element_by_name('password').send_keys('a123456')
log.info("输入用户密码:a123456")
for i in range(100):
self.param1.save_screenshot(screenImg) # 截取当前网页,该网页有我们需要的验证码
location = self.param1.find_element_by_xpath("//img[@class='check-code']").location
size = self.param1.find_element_by_xpath("//img[@class='check-code']").size
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
img = Image.open(screenImg).crop((left, top, right, bottom))
img = img.convert('L') # 转换模式:L | RGB
img = ImageEnhance.Contrast(img) # 增强对比度
img = img.enhance(2.0) # 增加饱和度
img.save(screenImg)
# 读取截图
img = Image.open(screenImg)
# 获取验证码
code = pytesseract.image_to_string(img)
# 打印验证码
print(list(code))
# 清空验证码输入框
self.param1.find_element_by_name('checkCode').clear()
# 输入验证码
self.param1.find_element_by_name('checkCode').send_keys(code)
# 提交登录
self.param1.find_element_by_class_name("login").click()
# time.sleep(2)
# if循环,当生成的code列表有4个元素时,跳出循环,否则刷新验证码,循环操作
if len(list(code)) == 4:
log.info("登陆成功")
break
else:
self.param1.find_element_by_xpath("//img[@class='check-code']").click()
time.sleep(1)
log.warning("登陆失败")
except Exception as err:
log.info("执行失败")

这是一个简单识别验证码登录的用例,对每一步进行日志打印,以方便后面定位问题

对一个简单的UI自动化框架的认知,大神可以指导一波
萌新入门,不喜勿喷,哈哈哈