python单机版自动化测试框架源代码(selenium+Appium+requests+unittest+Excel用例+HTMLTestRunner报告)

一、自动化测试框架:

1、框架和项目源代码下载

https://gitee.com/rmtic/autoTest

说明:框架可以支持web界面UI、安卓Android,ios苹果、接口API等自动化测试。文档和代码持续维护更新,有问题可以交流。

2、依赖包安装

2.1、jdk

jdk安装

2.2、python安装

下载地址:

http://python.org/getit/ 

http://npm.taobao.org/mirrors/python/

2.3、python依赖包

执行autoTest目录下的install.bat,即autoTest\install.bat

 

2.3、chrome driver

2.3.1、chrome driver的下载地址:

http://npm.taobao.org/mirrors/chromedriver/

2.3.2、安装

下载解压放到chrome的安装目录下...\Google\Chrome\Application\

设置path环境变量,把chrome的安装目录(我的:C:\ProgramFiles\Google\Chrome\Application)

注:

1、因信息安全原因,删除了真实项目配置文件中密码和http网页真实地址

2、以业务物流追踪(ipadWuLiuZhuiZong.py)中国港口功能为样例,做了界面检查、数据库与界面数据对比、字体颜色(红绿)检查等功能,仅供参考

3、建议windows绽放配置为100%,否则可能会影响部分功能稳定性

 

 

 

二、简介:

  1. 外部工具:上面自动化包目录“autoTest\basic\browsermob-proxy”中已经包含Browsermob-Proxy
  2. autoTest\conf\config.ini可以配置日志级别
  3. autoTest\caseexcel\ipadWebCase.xls是ipad网页项目Excel用例,“ipadApiCase.xls"是API接口用例,"大屏WebCase.xls"是大屏项目用例,"#url.xls"是生产和测试等网页地址配置文件
  4. autoTest\basic是基础脚本,基本所有项目通用,mySysCommon.py为系统常用函数功能class,webTestCase.py为UI自动化测试常用函数功能class
  5. autoTest\report是用于存放测试报告和过程图片
  6. autoTest\log是用于存放过程日志
  7. autoTest\cases\Zd为某项目自动化脚本,allData.json为公共数据变量文件,publicOperation.py为当前项目共用函数,comm.py中编写unittest的test开头用例,ipadDanJi.py和ipadWuLiuZhuiZong.py是各测试模块。commApi.py为API接口入口用例,ipadApi.json为API接口配置文件。
  8. main.py为主执行文件(可以通过main.bat执行),默认处理main.py处理autoTest\caseexcel目录中所有*.xls用例(但不包括子目录),完成后生成测试报告如下。如果有错误用例,编号记录在autoTest\log\retryCase.txt文件中

     

     

  9. retry.py可单独执行main.py中错误用例(可以通过retry.bat执行),即读取autoTest\log\retryCase.txt文件
  10. selenium_debug.py用于单独调试comm.py中对应test开头的自动化测试用例,但不读Excel用例数据
  11. 定时任务task.py(可以通过task.bat执行)自动定时先执行main.bat,再执行retry.bat,如果有错误用例发送通知

 

三、用例编写入门(详细使用说明)

 1、新建模块用例文件

 

2、在comm.py中import上面模块用例的class

并用CLASS Comm继承IpadWuLiuZhuiZong

 

3、模块用例中写具体的点击菜单和其它操作代码

 

 

 

4、在comm.py中添加模块入口用例

 

 

self.mySysParameterLoginEx用于IpadUserAdminID

self.params_in和self.checkPoint分别为输入参数和检查参数,可以分别获取excel用例中“输入参数”和“检查点”

 

 

ipadPbLoginOK为登录函数,输入用户、密码、和url

ipadWuLiuZhuiZongOK即调用模块用例入口

 

 

5、在Excel用例中添加与comm.py中相同名称的模块名

即模块名testIpadWuLiuZhuiZongOK,就可调用对应的函数。

用例执行时会从上向下按行逐个执行。

如果有多个文件也是按名称逐个执行。

状态为“无效”时,则不执行。为空值或其它时则执行。

 

 

6、在Excel用例中配置config页和在"#url.xls"的config页中配置url

项目名填写Zd,即路径“autoTest\cases\Zd”中的Zd

文件名,即comm.py中的comm

模块名,即文件comm.py中class名Comm

类型填写web

浏览器填写chrome或api,chrome即谷歌浏览器,api即接口测试

url即web测试地址,如下图

 

在"#url.xls"的config页中配置url,文件名和模块名与用例中config中的对应。类型为“无效”则不执行。如果上面用例excel中配置了url,则执行用例中的,不执行这里。用例excel中没有配置,则执行这里。如下图:

 

 

如果某个用例只在部分地址执行,则配置用例状态为“3”或“1;3”,如下图:

  

 其中,“3”即不执行对应url类型的如下图第3行地址,“1;3”即不执行对应类型的如下图第1和3行地址,如下图

 

 

 

 

7、函数注释说明

在函数开始注释中一般会有各函数使用说明,PyCharm等工具调用时浮窗也可以看到

 

 

8、UI自动化常用函数(webTestCase.py)

选择单个控件

def myWtFindElement(self, element, myby, myCtrlIdent, ifPrintErr=1, ifDisEna=1, maxWaittime=-1):

例:self.myWtFindElement(driver, By.CLASS_NAME, 'info')

选择多个控件

def myWtFindElements(self, element, myby, myCtrlIdent, ifPrintErr=1, ifFindElt=0):

例:self.myWtFindElements(driver, By.CLASS_NAME, 'itemBox')

WEB网页,输入框输入对应text值

def myWtSendKeysWebEx(self, element, myby, myCtrlIdent, text, ifPrintErr=1, ifDisEna=1, clear=0):

例:self.myWtSendKeysWebEx(driver, By.CLASS_NAME, "input-text", '123456')

控件点击

def myWtClickEx(self, element, myby, myCtrlIdent, ifPrintErr=1, ifDisEna=1, maxWaittime=-1):

例:self.myWtClickEx(driver, By.XPATH, "//span[text()='登录']")

获取控制台js log日志信息,如果控制台日志报错则打印error日志

def myWtGetJsLog(self, driver, jsLogExclude=[]):

例:self.myWtGetJsLog(driver, self.jsLogExclude)

获取Google Chrome网页devtools的Network接口请求和返回信息

def myWtPerformanceNetworkLog(self, driver, beginWaitTime=1, endWaitTime=10):

例:self.myWtPerformanceNetworkLog(driver)

 

9、系统常用函数(mySysCommon.py)

断言

self.mySysAssert("没有对应的类型")
例:self.mySysAssert('没有找到数据:{}'.format(fontfamily))

处理输入的文件名,返回处理后的文件名

def mySysProcessFileName(self, fileName)

函数说明:字符串清理,去除\r和\n

def mySysStringCleanup(self, textStr):

网络下载文件

def mySysDownloadFile(self, urlStr, saveFilename)

ping ip

def mySysPing(self, ip):

发邮件

def mySysSendEmail(self, smtpserver="smtp.sohu.com", sender="ocean@sohu.com", username="ocean@sohu.com", password="123456", receivers=["jianghy003@qianhai.com.cn", "52@qq.com"], textSubject=None, textBody=None, attachmentList=None)

生成带logo的二维码图片

def mySysMakeQRCode(self, fileName, data, logo=None)

判断字符串是否全数字

def mySysIsAllNumber(self, word):

读Excel,返回所有sheet name

def mySysReadExcelSheetName(self, filename):

读Excel,返回某行所有内容

def mySysReadExcelRow(self, filename, sheetname, rowNum)

读Excel,返回某列所有内容

def mySysReadExcelCol(self, filename, sheetname, colNum)

接口API

def mySysApi(self, url, requestMethod, header, datas, jsons)

oracle操作

def mySysOracleExecute(self, oracleIP, oraclePort, oracleUsername, oraclePassword, serviceName, sql, sshIP=None, sshPort=None, sshUsername=None, sshPassword=None)

 

10、项目常用函数(publicOperation.py)

ipad登录成功

ipadPbLoginOK(self, driver, userName, password, url):

选择主菜单

ipadPbSelectMainMenu(self, driver, menuStr1='', menuStr2='', menuStr3='')

接口

def myApi(self, url, requestMethod, datas, jsons):

oracle操作执行SQL

def myPbOracleExecute(self, sql):

生成SQL

def myPbMybatis(self, fileName, sql_id, params):

检查数据,数据库SQL查出的数值与传入的值对比

def myPbOracleValueListCheck(self, actualValue, fileName, sql_id, params):

def myPbOracleValueDictCheck(self, actualValue, fileName, sql_id, params):

ipad检查控件颜色

def ipadPbCheckTextColor(self, elt, color='red'):
例:self.ipadPbCheckTextColor(elt, 'green')

取和检查控件中文本大小(检查样例和报告如下图),且可以在截图中自动用红框标注位置

def ipadPbGetCheckTextSize(self, driver, paramsIn, checkPoint, uiPath, inputList=None, trby=None, trCtrlIdent=None, tdby=None, tdCtrlIdent=None, excludeTextList=[], tableWidth='N', tagList=['div', 'span', 'li', 'th', 'td', 'p']):

例如可以发现如下换行错误

 

 

 且在截图中自动用红框标注位置,如下图

注:使用这个功能时建议windows缩放为100%,如下图。如果用125%或以上可能会造成不准确

 

 

 

 

 

四、mySysCommon.py函数列表

def mySysAssert(self, text):
def mySysDialogOpenFile(self, filePathName):
def mySysGetDatetime(self):
def mySysGetDate(self, year=0, day=0, hour=0, minute=0, second=0):
def mySysGetLocalTime(self):
def mySysGetLocalMillisecond(self):
def mySysParameterLogin(self, paramsIn):
def mySysParameterLoginEx(self, paramsIn, ParName, ParPassword, iFForce=0):
def mySysParameterAssignment(self, paramsIn, ParNameA, ParNameB):
def mySysParameterValueReplaceJson(self, paramsIn, paramsKey):
def mySysParameterDefault(self, paramsIn, paramsKey, defaultValue):
def mySysParameterSplit(self, paramsIn, type):
def mySysParameterGetValue(self, paramsIn, paramsKey):
def mySysParameterIfNew(self, paramsIn):
def mySysGetRandChineseName(self):
def mySysGetPhoneNum(self):
def mySysGetPhoneRandomNum(self):
def mySysGetIdCardNum(self):
def mySysGetIdCardRandomNum(self):
def mySysCopyFile(self, oldFilename, newFilename):
def mySysMoveFile(self, oldFilename, newFilename):
def mySysRemoveFile(self, filename):
def mySysRemoveFileInDir(self, targetDir):
def mySysWriteFile(self, textStr, FileName='D:/order.txt'):
def mySysProcessFileName(self, fileName):
def mySysJsonLoad(self, FileName):
def mySysJsonWrite(self, data, FileName):
def mySysStringCleanup(self, textStr):
def mySysCloseProcess(self, ProcessName):
def mySysGetPathEachFile(self, filePath):
def mySysReadFileLines(self, fileName):
def mySysDateDifference(self, startDate, endDate):
def mySysListAddLimit(self, list, newMember, maxNum):
def mySysTimeGapSec(self, d1):
def mySysDownloadFile(self, urlStr, saveFilename):
def mySysPing(self, ip):
def mySysSendEmail(self, smtpserver="smtp.sohu.com", sender="ocean@sohu.com", username="ocean@sohu.com", password="123456", receivers=["jianghy003@qianhai.com.cn", "52@qq.com"], textSubject=None, textBody=None, attachmentList=None):
def mySysImagePaste(self, toImage, fileName, loc):
def mySysMakeQRCode(self, fileName, data, logo=None):
def mySysReadPdf(self, pdf):
def mySysDatePmtAgt(self):
def mySysIsChinese(self, ch):
def mySysIsExistChinese(self, word):
def mySysIsAllChinese(self, word):
def mySysGetChineseNum(self, word):
def mySysIsNumber(self, uchar):
def mySysIsExistNumber(self, word):
def mySysIsAllNumber(self, word):
def mySysIsAllNumberExclude(self, word):
def mySysGetNumberNum(self, word):
def mySysIsAlphabet(self, uchar):
def mySysIsExistAlphabet(self, word):
def mySysIsAllAlphabet(self, word):
def mySysGetAlphabetNum(self, word):
def mySysIsother(self, uchar):
def mySysSetClipboardText(self, clipboard):
def mySysReadExcelSheetName(self, filename):
def mySysReadExcelRow(self, filename, sheetname, rowNum):
def mySysReadExcelCol(self, filename, sheetname, colNum):
def mySysApi(self, url, requestMethod, header, datas, jsons):
def mySysOracleExecute(self, oracleIP, oraclePort, oracleUsername, oraclePassword, serviceName, sql, sshIP=None, sshPort=None, sshUsername=None, sshPassword=None):
def mySysTextCheck(self, actualText, expectText, ifFuzzy=0):
def mySysHsv2Rgb(self, h, s, v):
def mySysRgb2Hsv(self, r, g, b):

 

五、webTestCase.py函数列表

 

def setUp(self):
def tearDown(self):
def __upLoadImg(self, driver, testName, timeStr, type):
def myWtGet_screenshot_as_fileEx(self, driver, picName, printlog=0):
def myWtScreenshotAsFile(self, driver):
def myWtScreenshotByElementAsFile(self, driver, element):
def myWtScreenshotByElement(self, driver, element):
def myWtScreenshotByXyAsFile(self, driver, x=0, y=210 , width=300, height=150):
def myWtImageCompare(self, oldImage, newImage, percent=10):
def myWtScreenRecordAsFileArd(self):
def myWtExportFromArdToPC(self, fileNameList):
def myWtGetProxyResult(self):
def myWtGetProxyNewResult(self):
def myWtGetProxyNewAloneResult(self, localList, url):
def myWtCheckProxyResult(self):
def myWtGetJsLog(self, driver, jsLogExclude=[]):
def __getAppPathConf(self):
def myWtFindElement(self, element, myby, myCtrlIdent, ifPrintErr=1, ifDisEna=1, maxWaittime=-1):
def myWtFindElements(self, element, myby, myCtrlIdent, ifPrintErr=1, ifFindElt=0):
def myWtElementEx(self, element, ifPrintErr=1, ifDisEna=1, maxWaittime=-1):
def myWtSendKeys(self, element, text, clear=0):
def myWtSendKeysElements(self, element, myby, myCtrlIdent, text, clear=0):
def myWtSendKeysEx(self, element, myby, myCtrlIdent, text, ifPrintErr=1, ifDisEna=1, clear=0):
def myWtSendKeysWebEx(self, element, myby, myCtrlIdent, text, ifPrintErr=1, ifDisEna=1, clear=0):
def myWtClick(self, element, ifPrintErr=1, maxWaittime=-1):
def myWtClickEx(self, element, myby, myCtrlIdent, ifPrintErr=1, ifDisEna=1, maxWaittime=-1):
def myWtCheckboxRadioSelect(self, element, myby, myCtrlIdent, selected=1, ifPrintErr=1, ifDisEna=1):
def myWtSelectByText(self, driver, text):
def myWtEltNonexiContinue(self, element, myby, myCtrlIdent, ifPrintErr=0, maxWaittime=2):
def myWtFindElementsCpnText(self, driver, myby, myCtrlIdent, text, maxWaittime=-1):
def myWtElementsClickNumber(self, element, myby, myCtrlIdent, serialNumber=1, ifFindElt=0):
def myWtElementsChooseTextClick(self, eltList, text, ifBreak=1, serialNumber=0, maxWaittime=-1):
def myWtElementsChooseTextClickEx(self, driver, myby, myCtrlIdent, text, ifBreak=1, serialNumber=0, maxWaittime=-1):
def myWtElementsSimilarTextClick(self, eltList, text, ifBreak=1, serialNumber=0, maxWaittime=-1):
def myWtElementsChooseClickEx(self, element, myby, myCtrlIdent, elementEnd, mybyEnd, myCtrlIdentEnd):
def myWtElementsChooseAttributeClick(self, eltList, attributeName, text):
def myWtTablesChooseTextClickElt(self, eltList, text, myby, myCtrlIdent):
def myWtTablesChooseTextClickEltEx(self, eltList, text, myby, myCtrlIdent, myText):
def myWtElementsChooseTextSendKeys(self, eltList, text, inputStr):
def myWtElementsChooseAttributeSendKeys(self, eltList, attributeName, text, inputStr):
def myWtActionKeydown(self, driver, key):
def myWtTouchActionTap(self, driver, elt):
def myWtTouchActionLongpress(self, driver, elt):
def myWtCursorMoveFatherClickElt(self, driver, elt, myby, myCtrlIdent):
def myWtSelectCheck(self, driver, expectText):
def myWtInputCheck(self, driver, expectText):
def myWtTextCheck(self, driver, expectText, ifFuzzy=0):
def myWtAttributeCheck(self, driver, expectText, attributeName, ifFuzzy=0):
def myWtH5FlickUp(self, driver, elt, frequency=1, sleepTime=0, y1Temp=200):
def myWtH5FlickDown(self, driver, elt, frequency=1, sleepTime=0, y1Temp=-200):
def myWtGetWinSize(self, driver):
def myWtPhoneTap(self, driver, lx, ly):
def myWtPhoneSwipeAuto(self, driver, element, sleepTime=0):
def myWtPhoneSwipe(self, driver, x1, y1, x2, y2, frequency=1, sleepTime=0):
def myWtPhoneSwipeUp(self, driver, frequency=1, sleepTime=0, y1Temp=0.75, y2Temp=0.25):
def myWtPhoneSwipeDown(self, driver, frequency=1, sleepTime=0, y1Temp=0.25, y2Temp=0.75):
def myWtPhoneSwipeLeft(self, driver, frequency=1, sleepTime=0, x1Temp=0.75, x2Temp=0.25):
def myWtPhoneSwipeRight(self, driver, frequency=1, sleepTime=0, x1Temp=0.25, x2Temp=0.75):
def myWtPhonePasteText(self, driver, elt, text, x=0, y=0):
def myWtPhoneSetTypewriting(self):

 

六、publicOperation.py函数列表

 

def __getJsonConf(self):
def ipadPbLoginOK(self, driver, userName, password, url):
def ipadPbLoginPasswordError(self, driver, userName, password, url):
def ipadPbwebLoginComm(self, driver, userName='', password='', url=''):
def ipadPbSelectMainMenu(self, driver, menuStr1='', menuStr2='', menuStr3=''):
def ipadPbRefreshWaiting(self, driver):
def ipadPbLeftBack(self, driver, maxWaittime=None):
def ipadPbCloseTopRight(self, driver, maxWaittime=None):
def ipadPbSelectShowDate(self, driver, datetype, date, maxWaittime=-1):
def ipadPbShowDate(self, driver, dType, year, month, day=None, maxWaittime=-1):
def ipadPbGetCheckTextSize(self, driver, paramsIn, checkPoint, uiPath, inputList=None, trby=None, trCtrlIdent=None, tdby=None, tdCtrlIdent=None, excludeTextList=[], tableWidth='N', tagList=['div', 'span', 'li', 'th', 'td', 'p']):
def __ipadPbGetCheckTextSize(self, driver, paramsIn, checkPoint, data, eltList, trby=None, trCtrlIdent=None, tdby=None, tdCtrlIdent=None, excludeTextList=[], tableWidth='N'):
def ipadPbGetStrStdWidth(self, string, fontfamily='Bebas'):
def ipadPbCheckTextColor(self, elt, color='red'):
def pbJudgeRGBColor(self, rgb):
def myApi(self, url, requestMethod, datas, jsons):
def myGetWebToken(self):
def dapingPbLoginOK(self, driver, userName, password, url):
def dapingPbLoginPasswordError(self, driver, userName, password, url):
def dapingPbwebLoginComm(self, driver, userName='', password='', url=''):
def dapingPbRefreshWaiting(self, driver):
def dapingPbCheckTextColor(self, elt, color='red'):
def myPbOracleExecute(self, sql):
def myPbMybatis(self, fileName, sql_id, params):
def myPbOracleValueCheck(self, actualValue, fileName, sql_id, params):

 

posted @ 2022-03-12 11:18  rmticocean  阅读(1112)  评论(0编辑  收藏  举报