一.前言
1.项目是针对我们公司内部系统的测试,只能内部网络访问,外部网络无法访问
2.问:
2.1.外部网络无法访问,代码也无法运行,那还看这个项目有啥用。
2.2.如何学习本项目。
2.3.如何学习自动化测试(Python+Selenium)。
3.答:
3.1.其实代码并不重要,希望大家完完整整的看完这个项目后,自己会有思路有想法,学会这个项目的框架结构和设计思想,把这些能应用到自己的项目中,那么目的就达到了(项目中涉及到的一些公共方法是可以单独运行的,大家可以拿来执行用到自己的项目中)。
3.2.首先希望大家带着目标来学习这个项目:
a.项目的目录结构(每个目录中存放什么东西)。
b.项目如何使用框架(本项目使用的是unittest框架)。
c.设计模式是如何应用在本项目中的(本项目应用page object设计模式)。
3.个人而言
3.1.如果没有任何的编程基础,建议先学习一门编程语言,包括环境的搭建,自己动手写代码,遇到问题多想多琢磨,这样一定会加深自己的印象。如果有一定的编程基础那么直接看看Python的基础语法和selenium就ok。
3.2.自己动手搭个框架,手写一个实战的项目,这时候你会发现你还有好多东西不会,那么线路就来了,哪里不会就去学哪里,边学边写,直到你的项目完成,再次回味就会发现你会了好多,当然不会的东西更多了因为你的思路慢慢的扩宽了,你会想到无人值守,集成等等的想法。
3.3.可以参加培训机构的培训,说实话现在的培训机构越来越多,个人认为有些机构的老师确实是没什么水准的,因为他们教的是基础没有太多的拔高内容,但是有一点是好了,你可以很系统的学习一系列的自动化知识。
二.项目简介
1.项目名称:**公司电子零售会员系统
2.项目目的:实现电子零售会员系统项目自动化测试执行
3.项目版本:v1.0
三.项目目录
Retail_TestPro
docs
01测试计划
02测试大纲
03测试用例
04测试报告
05测试进度
06技术文档
07测试申请
package
HTMLTestRunner.py
retail
config
__init__.py
conf.py
config.ini
data
TestData
__init__.py
elementDate.xlsx
mail_receiver.txt
report
image
Fail
Pass
Log
TestReport
test_case
models
doconfini.py
doexcel.py
driver.py
log.py
myunittest.py
sendmail.py
strhandle.py
Tcinfo.py
Testreport.py
page_obj
Activerule_page.py
Base_page.py
Company_page.py
Createrule_page.py
Memberquery_page.py
Modifypw_page.py
Pointquery_page.py
ActiveRuleTc.py
CompanyQueryTc.py
CreateRuleTc.py
LoginTc.py
MemberQueryTc.py
ModifyPwTc.py
PointQueryTc.py
runTc.py
四.项目环境
1.python 36
2.pip insatll selenium
3.PyCharm 2017.2.4
4.Windows 10 10.0
5.HTMLTestRunner.py
五.项目框架
1.unittest单元测试框架
2.pageobject 设计模式
3.UI对象库思想
六.项目设计
1.一个模块(被测项目的页面)对应一个py文件及一个测试类(测试文件)
2.每一个测试页面(系统的页面)中存储页面元素及此页面中涉及到的功能
3.每一个用例组合在一个测试类里面生成一个py文件
七.项目目标
1.在写自动化测试项目的时候一定要想好脚本都要哪些功能,页面元素频繁改动的时候是否需要大批量的修改脚本,及测试不同数据时是否也要修改脚本,那么能想到这些我初始目标差不多就可以。
1.1.生成测试用例执行结果报告
1.2.生成测试用例执行日志
1.3.用例执行失败或者执行完成后自动发送邮件报告
1.4. 用例执行失败或者成功时截取图片
1.5.数据驱动(读取测试数据,减少脚本维护成本)
八.项目代码
config.ini
[project]
project_path = G:\Petrochina_Retail_Test_Project
conf.py
import os
import sys
from retail.test_case.models.doconfIni import DoConfIni
currPath= \
os.path.split(os.path.realpath(__file__))[0]
print(currPath)
readConfig = \
DoConfIni()
print(readConfig)
proPath = \
readConfig.getConfValue(os.path.join(currPath,'config.ini'),'project','project_path')
print(proPath)
logPath= \
os.path.join(proPath,'retail','report','Log')
print(logPath)
tcPath = \
os.path.join(proPath,'retail','test_case')
print(tcPath)
reportPath= \
os.path.join(proPath,'retail','report','TestReport')
print(reportPath)
dataPath= \
os.path.join(proPath,'retail','data','TestData')
print(dataPath)
failImagePath = os.path.join(proPath, 'retail', 'report', 'image','fail')
print(failImagePath)
passImagePath = os.path.join(proPath, 'retail', 'report', 'image','pass')
print(passImagePath)
funcName = sys._getframe().f_code.co_name
print(funcName)
funcNo = sys._getframe().f_back.f_lineno
print(funcNo)
funcFile= sys._getframe().f_code.co_filename
print(funcFile)
2.elementData.xlsx # 存放所有的测试数据及元素
3.mail_receiver.txt# 存放邮件接收者的账号 , 可以添加多个账号以‘,’号分割
**@qq.com
4.公共方法models下面的文件方法
doconfini.py
import logging
import configparser
from retail.config.conf import *
from retail.test_case.models.log import Logger
log = Logger(__name__, CmdLevel=logging.INFO, FileLevel=logging.INFO)
class DoConfIni(object):
def __init__(self):
"""
:param filename:
"""
self.cf = configparser.ConfigParser()
def getConfValue(self,filename,section,name):
"""
:param config:
:param name:
:return:
"""
try:
self.cf.read(filename)
value = self.cf.get(section,name)
except Exception as e:
log.logger.exception('read file [%s] for [%s] failed , did not get the value' %(filename,section))
raise e
else:
log.logger.info('read excel value [%s] successed! ' %value)
return value
def writeConfValue(self,filename, section, name, value):
"""
:param section: section
:param name: value name
:param value: value
:return: none
"""
try:
self.cf.add_section(section)
self.cf.set(section, name, value)
self.cf.write(open(filename, 'w'))
except Exception :
log.logger.exception('section %s has been exist!' %section)
raise configparser.DuplicateSectionError(section)
else:
log.logger.info('write section'+section+'with value '+value+' successed!')
if __name__ == '__main__':
file_path = currPath
print(file_path)
read_config = DoConfIni()
value = read_config.getConfValue(os.path.join(currPath,'config.ini'),'project','project_path')
print(value)
read_config.writeConfValue(os.path.join(currPath,'config.ini'),'tesesection', 'name', 'hello word')
doexcel.py
import xlrd
import os
import logging
from retail.config import conf
from retail.test_case.models.log import Logger
log = Logger(__name__, CmdLevel=logging.INFO, FileLevel=logging.INFO)
class ReadExcel(object):
def __init__(self,fileName='elementDate.xlsx',sheetName='elementsInfo'):
"""
:param fileName:
:param sheetName:
"""
try:
self.dataFile = os.path.join(conf.dataPath, fileName)
self.workBook = xlrd.open_workbook(self.dataFile)
self.sheetName = self.workBook.sheet_by_name(sheetName)
except Exception:
log.logger.exception('init class ReadExcel fail', exc_info=True)
raise
else:
log.logger.info('initing class ReadExcel')
def readExcel(self,rownum,colnum):
"""
:param rownum:
:param colnum:
:return:
"""
try:
value = self.sheetName.cell(rownum,colnum).value
except Exception:
log.logger.exception('read value from excel file fail', exc_info=True)
raise
else:
log.logger.info('reading value [%s] from excel file [%s] completed' %(value, self.dataFile))
return value
if __name__ == '__main__':
cellValue = ReadExcel().readExcel(1,3)
print((cellValue))
log.py
import logging
import time
class Logger(object):
def __init__(self,logger, CmdLevel=logging.INFO, FileLevel=logging.INFO):
"""
:param logger:
:param CmdLevel:
:param FileLevel:
:return:
"""
self.logger = logging.getLogger(logger)
self.logger.setLevel(logging.DEBUG)
fmt = logging.Formatter('%(asctime)s - %(filename)s:[%(lineno)s] - [%(levelname)s] - %(message)s')
currTime = time.strftime("%Y-%m-%d")
self.LogFileName = r'D:\Petrochina_Retail_Test_Project\retail\report\Log\log'+currTime+'.log'
fh = logging.FileHandler(self.LogFileName)
fh.setFormatter(fmt)
fh.setLevel(FileLevel)
self.logger.addHandler(fh)
def debug(self, message):
"""
:param message:
:return:
"""
self.logger.debug(message)
def info(self,message):
"""
:param message:
:return:
"""
self.logger.info(message)
def warn(self,message):
"""
:param message:
:return:
"""
self.logger.warning(message)
def error(self,message):
"""
:param message:
:return:
"""
self.logger.error(message)
def criti(self,message):
"""
:param message:
:return:
"""
self.logger.critical(message)
if __name__ == '__main__':
logger = Logger("fox",CmdLevel=logging.DEBUG, FileLevel=logging.DEBUG)
logger.logger.debug("debug")
logger.logger.log(logging.ERROR,'%(module)s %(info)s',{'module':'log日志','info':'error'})
sendmail.py
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import os
from retail.config import conf
from retail.test_case.models.log import Logger
log = Logger(__name__)
class SendMail(object):
'''邮件配置信息'''
def __init__(self,
receiver,
subject='Retail 系统测试报告',
server='smtp.qq.com',
fromuser='281754043@qq.com',
frompassword='gifhhsbgqyovbhhc',
sender='281754043@qq.com'):
"""
:param receiver:
:param subject:
:param server:
:param fromuser:
:param frompassword:
:param sender:
:return:
"""
self._server = server
self._fromuser = fromuser
self._frompassword = frompassword
self._sender = sender
self._receiver = receiver
self._subject = subject
def sendEmail(self, fileName):
"""
:param fileName:
:return:
"""
try:
f = open(os.path.join(conf.reportPath, fileName), 'rb')
fileMsg = f.read()
except Exception:
log.logger.exception('open or read file [%s] failed,No such file or directory: %s' %(fileName, conf.reportPath))
log.logger.info('open and read file [%s] successed!' %fileName)
else:
f.close()
subject = 'Python test report'
msg = MIMEText(fileMsg, 'html', 'utf-8')
msg['subject'] = Header(subject, 'utf-8')
msg['from'] = self._sender
try:
smtp = smtplib.SMTP()
smtp.connect(self._server)
smtp.login(self._fromuser, self._frompassword)
except Exception:
log.logger.exception('connect [%s] server failed or username and password incorrect!' %smtp)
else:
log.logger.info('email server [%s] login success!' %smtp)
try:
smtp.sendmail(self._sender, self._receiver, msg.as_string())
except Exception:
log.logger.exception('send email failed!')
else:
log.logger.info('send email successed!')
def getReceiverInfo(fileName):
'''
:param filename: 读取接收邮件人信息
:return: 接收邮件人信息
'''
try:
openFile = open(os.path.join(conf.dataPath, fileName))
except Exception:
log.logger.exception('open or read file [%s] failed,No such file or directory: %s' %(fileName, conf.dataPath))
else:
log.logger.info('open file [%s] successed!' %fileName)
for line in openFile:
msg = [i.strip() for i in line.split(',')]
log.logger.info('reading [%s] and got receiver value is [%s]' %(fileName, msg))
return msg
if __name__ == '__main__':
readMsg = getReceiverInfo('mail_receiver.txt')
sendmail = SendMail(readMsg)
sendmail.sendEmail('2018-09-21 17_44_04.html')
strhandle.py
import logging
from retail.test_case.models.log import Logger
log = Logger(__name__, CmdLevel=logging.INFO, FileLevel=logging.INFO)
def strhandle(str):
"""
:param str:
:return:
"""
try:
lowerCase = 0
upperCase = 0
number = 0
other = 0
for stritem in str:
if stritem.islower():
lowerCase += 1
elif stritem.isdigit():
number += 1
elif stritem.isupper():
upperCase +=1
else:
other += 1
return lowerCase, upperCase, number, other
except Exception as e:
log.logger.exception('string handle error , please check!', exc_info=True)
raise e
if __name__=='__main__':
list = ['qwert','erwer']
lowercase, uppercase, number, other = strhandle(list[0])
print (u"该字符串中的小写字母有:%d" %lowercase)
print (u"该字符串中的大写写字母有:%d" %uppercase)
print (u"该字符串中的数字有:%d" %number)
print (u"该字符串中的特殊字符有:%d" %other)
testreport.py
import time
import logging
import unittest
from BeautifulReport import BeautifulReport
import HTMLTestRunner
from retail.config import conf
from retail.test_case.models.log import Logger
log = Logger(__name__, CmdLevel=logging.INFO, FileLevel=logging.INFO)
def testreport():
currTime = time.strftime('%Y-%m-%d %H_%M_%S')
fileName = conf.reportPath + r'\report' + currTime + '.html'
try:
fp = open(fileName, 'wb')
except Exception :
log.logger.exception('[%s] open error cause Failed to generate test report' %fileName)
else:
runner = HTMLTestRunner.HTMLTestRunner\
(stream=fp, title=u'Retail sys测试报告',
description=u'处理器:Intel(R) Core(TM) ''i5-6200U CPU @ 2030GHz 2.40 GHz 'u'内存:8G 系统类型: 64位 版本: windows 10 家庭中文版')
log.logger.info('successed to generate test report [%s]' %fileName)
return runner, fp, fileName
def addTc(TCpath = conf.tcPath, rule = '*TC.py'):
"""
:param TCpath: 测试用例存放路径
:param rule: 匹配的测试用例文件
:return: 测试套件
"""
discover = unittest.defaultTestLoader.discover(TCpath, rule)
return discover
def runTc(discover):
"""
:param discover: 测试套件
:return:
"""
currTime = time.strftime('%Y-%m-%d %H_%M_%S')
fileName = currTime+'.html'
try:
result = BeautifulReport(discover)
result.report(filename=fileName, description='测试报告', log_path=conf.reportPath)
except Exception:
log.logger.exception('Failed to generate test report', exc_info=True)
else:
log.logger.info('successed to generate test report [%s]' % fileName)
return fileName
if __name__ == '__main__':
testreport()
suite = addTc(rule = '*TC.py')
runTc(suite)
driver.py
from selenium import webdriver
import logging
import sys
from retail.test_case.models.log import Logger
log = Logger(__name__, CmdLevel=logging.INFO, FileLevel=logging.INFO)
class WDriver(object):
def fireFoxDriver(self):
try:
self.driver = webdriver.Firefox()
except Exception as e:
log.logger.exception('FireFoxDriverServer.exe executable needs to be in PATH. Please download!', exc_info=True)
raise e
else:
log.logger.info('%s:found the Firefox driver [%s] successed !' %(sys._getframe().f_code.co_name,self.driver))
return self.driver
def chromeDriver(self):
try:
self.driver = webdriver.Chrome()
except Exception as e:
log.logger.exception('ChromeDriverServer.exe executable needs to be in PATH. Please download!',exc_info=True)
raise e
else:
log.logger.info('%s:found the chrome driver [%s] successed !' % (sys._getframe().f_code.co_name, self.driver))
return self.driver
def ieDriver(self):
try:
self.driver = webdriver.Ie()
except Exception as e:
log.logger.exception('IEDriverServer.exe executable needs to be in PATH. Please download!',exc_info=True)
raise e
else:
log.logger.info('%s:found the IE driver [%s] successed !' % (sys._getframe().f_code.co_name, self.driver))
return self.driver
if __name__ == '__main__':
WDrive=WDriver()
WDrive.fireFoxDriver()
myunittest.py
from retail.test_case.models.driver import WDriver
import logging
import unittest
from retail.test_case.page_obj.login_page import LoginPage
from retail.test_case.models.log import Logger
from selenium import webdriver
log = Logger(__name__, CmdLevel=logging.INFO, FileLevel=logging.INFO)
class MyunitTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = WDriver().chromeDriver()
cls.driver.maximize_window()
log.logger.info('opened the browser successed!')
def setUp(self):
self.login = LoginPage(self.driver)
self.login.open()
log.logger.info('************************starting run test cases************************')
def tearDown(self):
self.driver.refresh()
log.logger.info('************************test case run completed************************')
@classmethod
def tearDownClass(cls):
cls.driver.quit()
log.logger.info('quit the browser success!')
if __name__ == '__main__':
unittest.main()
九.需要的所有公共方法都编写完了, 后期再需要别的方法可以加。下面就开始编写测试用例,由于使用的是PageObject模式,那么需要设计一个basepage页面,所有的页面或者说模块全部继承这个basepage,basepage主要编写所有页面的公共方法。
base_page.py
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import os
import logging
import sys
from retail.test_case.models.log import Logger
from retail.config import conf
from retail.test_case.models.doexcel import ReadExcel
eleData = ReadExcel()
testLoginData = ReadExcel('elementDate.xlsx', 'userNamePw')
modifyPwData = ReadExcel('elementDate.xlsx', 'modifyPw')
queryData = ReadExcel('elementDate.xlsx', 'queryData')
log = Logger(__name__, CmdLevel=logging.INFO, FileLevel=logging.INFO)
class BasePage(object):
"""主菜单"""
menuList = \
[(By.LINK_TEXT, eleData.readExcel(7, 3)),
(By.LINK_TEXT, eleData.readExcel(8, 3)),
(By.LINK_TEXT, eleData.readExcel(9, 3)),
(By.LINK_TEXT, eleData.readExcel(10, 3)),
(By.LINK_TEXT, eleData.readExcel(11, 3)),
(By.LINK_TEXT, eleData.readExcel(12, 3)),
(By.LINK_TEXT, eleData.readExcel(13, 3)),
(By.LINK_TEXT, eleData.readExcel(14, 3))]
def __init__(self, driver,url='http://11.11.164.134:9081/rmms/modules/ep.rmms.portal/login/login.jsp'):
self.driver = driver
self.base_url = url
def _open(self,url):
try:
self.driver.get(url)
self.driver.implicitly_wait(10)
except Exception as e:
log.logger.exception(e, exc_info=True)
raise ValueError('%s address access error, please check!' %url)
else:
log.logger.info('%s is accessing address %s at line[46]' %(sys._getframe().f_code.co_name,url))
def open(self):
self._open(self.base_url)
log.logger.info('%s loading successed!' %self.base_url)
return self.base_url
def findElement(self, *loc):
"""查找单一元素"""
try:
WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc))
except Exception as e:
log.logger.exception('finding element timeout!, details' ,exc_info=True)
raise e
else:
log.logger.info('The page of %s had already find the element %s' % (self, loc))
return self.driver.find_element(*loc)
def findElements(self, *loc):
"""查找一组元素"""
try:
WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc))
except Exception as e:
log.logger.exception('finding element timeout!, details', exc_info=True)
raise e
else:
log.logger.info('The page of %s had already find the element %s' % (self, loc))
return self.driver.find_elements(*loc)
def inputValue(self, inputBox, value):
"""后期修改其他页面直接调用这个函数"""
inputB = self.findElement(*inputBox)
try:
inputB.clear()
inputB.send_keys(value)
except Exception as e:
log.logger.exception('typing value error!', exc_info=True)
raise e
else:
log.logger.info('inputValue:[%s] is receiveing value [%s]' % (inputBox, value))
def getValue(self, *loc):
element = self.findElement(*loc)
try:
value = element.text
except Exception:
value = element.get_attribute('value')
log.logger.info('reading the element [%s] value [%s]' % (loc, value))
return value
except:
log.logger.exception('read value failed', exc_info=True)
raise Exception
else:
log.logger.info('reading the element [%s] value [%s]' % (loc,value))
return value
def getValues(self, *loc):
value_list = []
try:
for element in self.findElements(*loc):
value = element.text
value_list.append(value)
except Exception as e:
log.logger.exception('read value failed', exc_info=True)
raise e
else:
log.logger.info('reading the element [%s] value [%s]'% (loc,value_list))
return value_list
def jScript(self,src):
try:
self.driver.excute_script(src)
except Exception as e:
log.logger.exception('execute js script [%s] failed ' %src)
raise e
else:
log.logger.info('execute js script [%s] successed ' %src)
def isElementExist(self, element):
try:
WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(element))
except:
return False
else:
return True
def saveScreenShot(self, filename):
list_value = []
list = filename.split('.')
for value in list:
list_value.append(value)
if list_value[1] == 'png' or list_value[1] == 'jpg' or list_value[1] == 'PNG' or list_value[1] == 'JPG':
if 'fail' in list_value[0].split('_'):
try:
self.driver.save_screenshot(os.path.join(conf.failImagePath, filename))
except Exception:
log.logger.exception('save screenshot failed !', exc_info=True)
else:
log.logger.info('the file [%s] save screenshot successed under [%s]' % (filename, conf.failImagePath))
elif 'pass' in list_value[0]:
try:
self.driver.save_screenshot(os.path.join(conf.passImagePath, filename))
except Exception:
log.logger.exception('save screenshot failed !', exc_info=True)
else:
log.logger.info('the file [%s] save screenshot successed under [%s]' % (filename, conf.passImagePath))
else:
log.logger.info('save screenshot failed due to [%s] format incorrect' %filename)
else:
log.logger.info('the file name of [%s] format incorrect cause save screenshot failed, please check!' % filename)
def accept(self, *loc):
self.findElement(*loc).click()
log.logger.info('closed the error information fram successed!')
if __name__ == '__main__':
pass
1.登录页面 login_page.py
from selenium.webdriver.common.by import By
import logging
import sys
from retail.test_case.page_obj.base_page import BasePage, eleData, testLoginData
from retail.test_case.models.log import Logger
log = Logger(__name__, CmdLevel=logging.INFO, FileLevel=logging.INFO)
class LoginPage(BasePage):
"""用户名,密码,登录按钮,保存信息,错误提示"""
userNameEle = (By.ID, eleData.readExcel(1, 3))
passWordEle = (By.ID, eleData.readExcel(2, 3))
loginBtnEle = (By.ID, eleData.readExcel(3, 3))
saveInfoEle = (By.NAME, eleData.readExcel(4, 3))
errorMessage = (By.ID, eleData.readExcel(5, 3))
quitBtn = (By.ID, eleData.readExcel(6, 3))
unpwData = \
[[testLoginData.readExcel(1, 0), testLoginData.readExcel(1, 1)],
[testLoginData.readExcel(2, 0), testLoginData.readExcel(2, 1)],
[testLoginData.readExcel(3, 0), testLoginData.readExcel(3, 1)],
[testLoginData.readExcel(4, 0), testLoginData.readExcel(4, 1)],
[testLoginData.readExcel(5, 0), testLoginData.readExcel(5, 1)],
[testLoginData.readExcel(6, 0), testLoginData.readExcel(6, 1)],
[testLoginData.readExcel(7, 0), testLoginData.readExcel(7, 1)]]
def clickLoginBtn(self):
element = self.findElement(*self.loginBtnEle)
element.click()
log.logger.info('%s ,logining....!' % sys._getframe().f_code.co_name)
def getFailedText(self):
info = self.findElement(*self.errorMessage).text
log.logger.info('login failed : %s' %info)
return info
def handleAlert(self):
try:
alert = self.driver.switch_to_alert()
text = alert.text
alert.accept()
except Exception:
log.logger.exception('handle alert failed, please check the details' ,exc_info=True)
raise
else:
log.logger.info('login failed ,%s handle alert successed alert info: %s!' %(sys._getframe().f_code.co_name, text))
return text
def loginFunc(self, username='rmln', password='qwert1234!@#'):
self.inputValue(self.userNameEle, username)
self.inputValue(self.passWordEle, password)
self.clickLoginBtn()
def clearValue(self, element):
empty = self.findElement(*element)
empty.clear()
log.logger.info('emptying value.......')
def quit(self):
self.findElement(*self.quitBtn).click()
log.logger.info('quit')
if __name__ == '__main__':
pass
2.登录测试用例 LoginTc.py
import unittest
import time
import logging
import sys
from retail.test_case.models.myunittest import MyunitTest
from retail.test_case.models.log import Logger
log = Logger(__name__, CmdLevel=logging.INFO, FileLevel=logging.INFO)
class Login_TC(MyunitTest):
"""登录模块测试用例"""
def test_login_success_correct_username_password(self):
"""用户名正确,密码正确,登录成功"""
self.login.loginFunc()
currUrl = self.driver.current_url
try:
self.assertIn('main', currUrl, 'main not in current url!')
except Exception:
self.login.saveScreenShot('correct_username_password_fail.png')
raise
else:
self.login.saveScreenShot('correct_username_password_pass.png')
log.logger.info('%s->run completed! please check the test report' % (sys._getframe().f_code.co_name))
def test_login_failed_incorrect_username(self):
"""用户名错误,密码正确,登录失败"""
self.login.loginFunc(self.login.unpwData[1][0], self.login.unpwData[1][1])
failText = self.login.getFailedText()
self.assertEqual(u'输入的用户名或密码错误,请重新输入!', failText, u'提示信息错误')
log.logger.info('%s->run completed! please check the test report' % (sys._getframe().f_code.co_name))
def test_login_failed_incorrect_password(self):
"""用户名正确,密码错误,登录失败"""
self.login.loginFunc(self.login.unpwData[5][0], self.login.unpwData[5][1])
failText = self.login.getFailedText()
self.assertEqual(u'输入的用户名或密码错误,请重新输入!', failText, u'提示信息错误')
log.logger.info('%s->run completed! please check the test report' % (sys._getframe().f_code.co_name))
def test_login_failed_username_password_blank(self):
"""用户名为空,密码为空,登录失败"""
self.login.loginFunc(self.login.unpwData[6][0], self.login.unpwData[6][1])
failText = self.login.handleAlert()
self.assertEqual(u'请填写用户名', failText, u'提示信息错误')
log.logger.info('%s->run completed! please check the test report' % (sys._getframe().f_code.co_name))
def test_login_failed_password_blank(self):
"""用户名正确,密码为空,登录失败"""
self.login.loginFunc(self.login.unpwData[4][0], self.login.unpwData[4][1])
failText = self.login.handleAlert()
self.assertEqual(u'请填写用户密码', failText, u'提示信息错误')
log.logger.info('%s->run completed! please check the test report' % (sys._getframe().f_code.co_name))
def test_login_failed_unpw_incorrect(self):
"""用户名错误,密码错误,登录失败"""
self.login.loginFunc(self.login.unpwData[3][0], self.login.unpwData[4][0])
failText = self.login.getFailedText()
self.assertEqual (u'输入的用户名或密码错误,请重新输入!', failText, 'failed')
log.logger.info('%s->run completed! please check the test report' % (sys._getframe().f_code.co_name))
def test_login(self):
"""循环测试登录功能"""
for listitem in self.login.unpwData:
self.login.inputValue(self.login.userNameEle,listitem[0])
time.sleep(2)
self.login.inputValue(self.login.passWordEle,listitem[1])
time.sleep(2)
self.login.clickLoginBtn()
time.sleep(2)
if listitem[0] =='rmln' and listitem[1] == 'qwert1234!@#':
currUrl = self.driver.current_url
self.assertIn ('main' , currUrl)
self.login.quit()
elif listitem[0] == 'rmln' and listitem[1] != 'qwert1234!@#':
if listitem[1] == '':
failText = self.login.handleAlert()
self.assertEqual(u'请填写用户密码', failText, u'提示信息错误')
else:
failText = self.login.getFailedText()
self.assertEqual(u'输入的用户名或密码错误,请重新输入!', failText, u'提示信息错误')
elif listitem[0] != 'rmln' and listitem[1] == 'qwert1234!@#':
if listitem[0]=='':
failText = self.login.handleAlert()
self.assertEqual(u'请填写用户名', failText, u'提示信息错误')
else:
failText = self.login.getFailedText()
self.assertEqual(u'输入的用户名或密码错误,请重新输入!', failText, u'提示信息错误')
elif listitem[0] == listitem[1] == '':
failText = self.login.handleAlert()
self.assertEqual(u'请填写用户名', failText, '提示信息错误')
else:
failText = self.login.getFailedText()
self.assertEqual(u'输入的用户名或密码错误,请重新输入!', failText, u'提示信息错误')
log.logger.info('%s->run completed! please check the test report' % (sys._getframe().f_code.co_name))
if __name__ == '__main__':
unittest.main()
3.修改密码页面 modifypw_page.py
import logging
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from retail.test_case.page_obj.base_page import BasePage, eleData, modifyPwData
from retail.test_case.models.log import Logger
log = Logger(__name__, CmdLevel=logging.INFO, FileLevel=logging.INFO)
class PrimaryMenu(BasePage):
"""密码数据"""
pwdList = \
[[modifyPwData.readExcel(1, 0), modifyPwData.readExcel(1, 1), modifyPwData.readExcel(1, 2)],
[modifyPwData.readExcel(2, 0), modifyPwData.readExcel(2, 1), modifyPwData.readExcel(2, 2)],
[modifyPwData.readExcel(3, 0), modifyPwData.readExcel(3, 1), modifyPwData.readExcel(3, 2)],
[modifyPwData.readExcel(4, 0), modifyPwData.readExcel(4, 1), modifyPwData.readExcel(4, 2)],
[modifyPwData.readExcel(5, 0), modifyPwData.readExcel(5, 1), modifyPwData.readExcel(5, 2)]]
"""权限管理下拉菜单"""
menuPersonal = (By.LINK_TEXT, eleData.readExcel(15, 3))
menuModifyPwd = (By.LINK_TEXT, eleData.readExcel(16, 3))
"""密码修改"""
oldPwd = (By.ID, eleData.readExcel(17, 3))
newPwd = (By.ID, eleData.readExcel(18, 3))
commitPwd = (By.ID, eleData.readExcel(19, 3))
"""错误提示框及确定"""
errMessage = (By.XPATH, eleData.readExcel(20, 3))
closeBtn = (By.CSS_SELECTOR, eleData.readExcel(21, 3))
"""密码说明"""
readMe = (By.ID, eleData.readExcel(22, 3))
"""保存"""
saveBtn = (By.XPATH, eleData.readExcel(23, 3))
def findMenu(self,*menuList):
return self.findElement(*menuList)
def inputOldPw(self, oldPwd=''):
try:
self.findElement(*self.oldPwd).clear()
self.findElement(*self.oldPwd).send_keys(oldPwd)
except Exception:
log.logger.exception('input Pw [%s] for oldPw [%s] fail' %(oldPwd, self.oldPwd))
raise
else:
log.logger.info('inputing Pw [%s] for oldPw [%s] ' % (oldPwd, self.oldPwd))
def inputNewPw(self, newPwd=''):
try:
self.findElement(*self.newPwd).clear()
self.findElement(*self.newPwd).send_keys(newPwd)
except Exception:
log.logger.exception('input Pw [%s] for newPw [%s] fail' % (newPwd, self.newPwd))
raise
else:
log.logger.info('inputing Pw [%s] for newPw [%s] ' % (newPwd, self.newPwd))
def inputConfirmPw(self, confirmPwd=''):
try:
self.findElement(*self.commitPwd).clear()
self.findElement(*self.commitPwd).send_keys(confirmPwd)
except Exception:
log.logger.exception('input Pw [%s] for commitPw [%s] fail' %(confirmPwd, self.commitPwd))
raise
else:
log.logger.info('inputing Pw [%s] for commitPw [%s] ' %(confirmPwd, self.commitPwd))
def saveButton(self):
try:
self.driver.implicitly_wait(5)
clickbutton = self.findElement(*self.saveBtn)
time.sleep(1)
clickbutton.click()
except Exception:
log.logger.exception('click save button fail')
raise
else:
log.logger.info('clciking the button')
def modifyPwMenu(self):
try:
self.findElement(*self.menuList[0]).click()
self.findElement(*self.menuPersonal).click()
self.findElement(*self.menuModifyPwd).click()
except Exception:
log.logger.exception('not found menu [%s]-[%s]-[%s]' %(self.menuList[0], self.menuPersonal, self.menuModifyPwd))
raise
else:
log.logger.info('finding menu [%s]-[%s]-[%s]' %(self.menuList[0], self.menuPersonal, self.menuModifyPwd))
self.driver.implicitly_wait(2)
def modifyPw(self, list):
try:
self.inputOldPw(list[0])
self.inputNewPw(list[1])
self.inputConfirmPw(list[2])
self.saveButton()
except Exception:
log.logger.exception('input oldpw/newpw/commitpw [%s]/[%s]/[%s] fail' %(list[0], list[1], list[2]))
raise
else:
log.logger.info('modifing pw [%s]/[%s]/[%s]' %(list[0], list[1], list[2]))
def errorDialog(self, commit_btn = (By.ID,'unieap_form_Button_1_unieap_input')):
"""
:type commit_btn: 元祖
"""
try:
messages_frame = self.findElement(*self.errMessage)
text = messages_frame.text
element = self.findElement(*commit_btn)
time.sleep(2)
action = ActionChains(self.driver)
action.move_to_element(element).perform()
time.sleep(2)
element.click()
action.reset_actions()
except Exception:
log.logger.exception('close errMsgFram [%s] or get text [%s]fail' %(self.errMessage))
raise
else:
log.logger.info('close errMsgFram [%s] and get text [%s] success' %(self.errMessage, text))
return text
def closeErrMsg(self, element):
try:
ele = self.findElement(*element)
action = ActionChains(self.driver)
action.move_to_element(ele).perform()
time.sleep(2)
ele.click()
action.reset_actions()
except Exception:
log.logger.exception('close the err msg ifram fail', exc_info=True)
raise
else:
log.logger.info('closing the err msg ifram success!')
if __name__ == '__main__':
pass
4.修改密码测试用例 ModifyPw.py
import time
from retail.test_case.models.myunittest import MyunitTest
from retail.test_case.page_obj.modifypw_page import PrimaryMenu
from retail.test_case.models.strhandle import strhandle
class ModifyPw_TC(MyunitTest):
"""权限管理/个人设置/密码修改模块测试用例"""
def test_menu_is_display(self):
"""主菜单校验"""
self.login.loginFunc()
menu = PrimaryMenu(self.driver)
time.sleep(4)
num = 0
for menu_item in menu.menuList:
self.assertEqual(menu.menuList[num][1],(menu.findMenu(*menu_item).text),u'菜单不存在')
num=num+1
def test_modify_password_len(self):
"""旧密码非空,新密码长度小于4位,确认密码非空,修改密码失败,弹窗提示"""
self.login.loginFunc()
menu = PrimaryMenu(self.driver)
menu.modifyPwMenu()
menu.modifyPw(menu.pwdList[0])
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'密码长度至少 4 位!', text, u'提示信息错误')
def test_modify_password_strebgth(self):
"""旧密码非空,新密码长度大于4且强度不够,确认密码非空,修改密码失败,弹窗提示"""
self.login.loginFunc()
menu = PrimaryMenu(self.driver)
menu.modifyPwMenu()
menu.modifyPw(menu.pwdList[1])
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'密码强度不够,请重新输入密码!', text, u' 密码强度不够,请重新输入密码!')
def test_modify_password_incorrect(self):
"""旧密码不正确非空,新密码等于确认密码且满足条件,修改密码失败,弹窗提示"""
self.login.loginFunc()
menu = PrimaryMenu(self.driver)
menu.modifyPwMenu()
menu.modifyPw(menu.pwdList[2])
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'旧密码输入错误!', text, u'旧密码输入错误!')
def test_modify_password_difference(self):
"""旧密码非空,新密码不等于确认密码且新密码满足条件,修改密码失败,弹窗提示"""
self.login.loginFunc()
menu = PrimaryMenu(self.driver)
menu.modifyPwMenu()
menu.modifyPw(menu.pwdList[3])
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'两次输入的新密码不同!', text, u'两次输入的新密码不同!')
def test_modify_password_all_blank(self):
"""旧密码,新密码,确认密码任意为空,修改密码失败,弹窗提示"""
self.login.loginFunc()
menu = PrimaryMenu(self.driver)
menu.modifyPwMenu()
menu.modifyPw(menu.pwdList[4])
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'该输入项的值不能为空!', text, u' 该输入项的值不能为空!')
def test_modify_password(self):
"""循环校验提示信息"""
self.login.loginFunc()
menu = PrimaryMenu(self.driver)
menu.modifyPwMenu()
error_list = []
for list in range(len(menu.pwdList)):
menu.modifyPw(menu.pwdList[list])
if menu.isElementExist(menu.errMessage):
text = menu.errorDialog(menu.closeBtn)
error_list.append(text)
else:
self.assertTrue(menu.isElementExist(*menu.errMessage), 'error fram not exist, please open bug')
self.assertEqual(u'密码长度至少 4 位!',error_list[0],'log infomation error!')
self.assertEqual(u'密码强度不够,请重新输入密码!', error_list[1], 'log infomation error!')
self.assertEqual(u'旧密码输入错误!', error_list[2], 'log infomation error!')
self.assertEqual(u'两次输入的新密码不同!', error_list[3], 'log infomation error!')
self.assertEqual(u'该输入项的值不能为空!', error_list[4], 'log infomation error!')
def test_modifypw(self):
"""循环测试修改密码功能"""
self.login.loginFunc()
menu = PrimaryMenu(self.driver)
menu.modifyPwMenu()
for item in menu.pwdList:
menu.modifyPw(item)
if menu.isElementExist(menu.errMessage):
if item[0] != '' and len(item[1]) < 4 and item[2] !='':
text = menu.errorDialog(menu.closeBtn)
try:
self.assertEqual(u'密码长度至少 4 位!',text,'the message incorrect!')
except Exception:
menu.saveScreenShot(u'fail_密码长度.png')
raise
elif item[0] != '' and len(item[1]) >= 4 and item[2] !='':
lowercase, uppercase, number, other=strhandle(item[1])
if lowercase > 0 and uppercase > 0 and number == 0 and other == 0:
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'密码强度不够,请重新输入密码!', text, u' 密码强度不够,请重新输入密码!')
elif uppercase > 0 and other > 0 and number == 0 and lowercase == 0:
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'密码强度不够,请重新输入密码!', text, u' 密码强度不够,请重新输入密码!')
elif lowercase >0 and other > 0 and number == 0 and uppercase == 0:
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'密码强度不够,请重新输入密码!', text, u' 密码强度不够,请重新输入密码!')
elif lowercase == 0 and other == 0 and number > 0 and uppercase > 0:
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'密码强度不够,请重新输入密码!', text, u'密码强度不够,请重新输入密码!')
elif lowercase > 0 and other == 0 and number > 0 and uppercase == 0:
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'密码强度不够,请重新输入密码!', text, u'密码强度不够,请重新输入密码!')
elif lowercase > 0 and other == 0 and number == 0 and uppercase == 0:
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'密码强度不够,请重新输入密码!', text, u'密码强度不够,请重新输入密码!')
elif lowercase == 0 and other > 0 and number == 0 and uppercase == 0:
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'密码强度不够,请重新输入密码!', text, u'密码强度不够,请重新输入密码!')
elif lowercase == 0 and other == 0 and number > 0 and uppercase == 0:
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'密码强度不够,请重新输入密码!', text, u'密码强度不够,请重新输入密码!')
elif lowercase == 0 and other == 0 and number == 0 and uppercase > 0:
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'密码强度不够,请重新输入密码!', text, u'密码强度不够,请重新输入密码!')
elif item[0] != 'qwert1234!@#' and item[1] == item[2]:
lowercase, uppercase, number, other = strhandle(item[1])
if (lowercase > 0 and uppercase > 0 and number > 0) or (
lowercase > 0 and uppercase > 0 and other > 0) or (
number > 0 and other > 0 and lowercase > 0) or (
number > 0 and other > 0 and uppercase > 0):
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'旧密码输入错误!', text, u'旧密码输入错误!')
elif item[0] == 'qwert1234!@#$' and item[1] != item[2]:
lowercase, uppercase, number, other = strhandle(item[1])
if (lowercase > 0 and uppercase > 0 and number > 0) or (
lowercase > 0 and uppercase > 0 and other > 0) or (
number > 0 and other > 0 and lowercase > 0) or (
number > 0 and other > 0 and uppercase > 0):
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'两次输入的新密码不同!', text, u'两次输入的新密码不同!')
else:
print('test value incorrect! please check it')
elif item[0] == '' or item[1] =='' or item[2] =='':
text = menu.errorDialog(menu.closeBtn)
self.assertIn(u'该输入项的值不能为空!', text, u'该输入项的值不能为空!')
else:
self.assertTrue(menu.isElementExist(menu.errMessage), 'error fram not exist, please check the test value or file bug')
if __name__=='__main__':
pass
5.会员档案查询页面 memberquery_page.py
from retail.test_case.page_obj.base_page import queryData
import time
from selenium.webdriver.common.by import By
import logging
import sys
from retail.test_case.page_obj.modifypw_page import PrimaryMenu, eleData
from retail.test_case.models.log import Logger
log = Logger(__name__, CmdLevel=logging.INFO, FileLevel=logging.INFO)
class MemberQuery(PrimaryMenu):
valuesList = [queryData.readExcel(1, 1), int(queryData.readExcel(2, 1)), queryData.readExcel(3, 1)]
memberMenu = (By.LINK_TEXT, eleData.readExcel(24, 3))
uiElements = (By.XPATH, eleData.readExcel(25, 3))
memberTypeBtn = (By.ID, eleData.readExcel(26, 3))
memberTypeNum = [(By.XPATH, eleData.readExcel(27, 3)),
(By.XPATH, eleData.readExcel(28, 3)),
(By.XPATH, eleData.readExcel(29, 3))]
memberLevelBtn = (By.ID, eleData.readExcel(30, 3))
memberLevelNum = [(By.XPATH, eleData.readExcel(31, 3)),
(By.XPATH, eleData.readExcel(32, 3)),
(By.XPATH, eleData.readExcel(33, 3)),
(By.XPATH, eleData.readExcel(34, 3))]
memberNumNamePhone = [(By.ID, eleData.readExcel(35, 3)),
(By.ID, eleData.readExcel(36, 3)),
(By.ID, eleData.readExcel(37, 3))]
qFailerr = (By.XPATH, eleData.readExcel(38, 3))
confirmBtn = (By.XPATH, eleData.readExcel(39, 3))
queryResetBtn = [(By.ID, eleData.readExcel(40, 3)),
(By.ID, eleData.readExcel(41, 3))]
def selectMemberType(self):
try:
self.findElement(*self.memberTypeBtn).click()
self.driver.implicitly_wait(2)
except Exception:
log.logger.exception('selecting member type fail ')
raise
else:
log.logger.info('---selecting member type ')
def selectMemberLevel(self):
try:
self.findElement(*self.memberLevelBtn).click()
self.driver.implicitly_wait(2)
except Exception:
log.logger.exception('selecting member level fail ')
raise
else:
log.logger.info('---selecting member level ')
def memberQueryMenu(self):
self.findElement(*self.menuList[1]).click()
self.findElement(*self.memberMenu).click()
time.sleep(4)
log.logger.info('page [%s] :found the menu [%s] and [%s]' % (
sys._getframe().f_code.co_name, self.menuList[1], self.memberMenu))
def memberTypeLevelOption(self, *xpathList):
try:
member_type_level = self.findElement(*xpathList)
text = member_type_level.text
except Exception:
log.logger.exception('get element member type/level item text fail', exc_info=True)
raise
else:
log.logger.info('get element [%s] member type/level item text [%s] fail' % (xpathList, text))
return text, member_type_level
def cQueryResetBtn(self, *queryResetBtn):
try:
self.findElement(*queryResetBtn).click()
except Exception:
log.logger.exception('query/reset button not click', exc_info=True)
raise
else:
log.logger.info('clicking query/reset button ')
def iQueryCondition(self, numNamePhone, value):
number_name_phone = self.findElement(*numNamePhone)
try:
number_name_phone.clear()
number_name_phone.send_keys(value)
except Exception:
log.logger.exception('input value error', exc_info=True)
raise
else:
log.logger.info('[%s] is typing value [%s] ' % (numNamePhone, value))
def getInputboxValue(self, *memberNumNamePhone):
try:
get_member_number_name_phone_text = self.findElement(*memberNumNamePhone)
text = get_member_number_name_phone_text.get_attribute('value')
except Exception:
log.logger.exception('get value of element fail', exc_info=True)
raise
else:
log.logger.info('get value [%s] of element [%s] success' % (memberNumNamePhone, text))
return text
def reset(self):
try:
self.findElement(*self.memberNumNamePhone[0]).clear()
self.findElement(*self.memberNumNamePhone[1]).clear()
self.findElement(*self.memberNumNamePhone[2]).clear()
except Exception:
log.logger.exception('reset fail', exc_info=True)
raise
else:
log.logger.info('reset [%s]-[%s]-[%s] success' % (
self.memberNumNamePhone[0], self.memberNumNamePhone[1], self.memberNumNamePhone[2]))
if __name__ == '__main__':
pass
6.会员档案查询用例 MemberQueryTc.py
import random
import time
from selenium.webdriver.common.action_chains import ActionChains
from retail.test_case.models.myunittest import MyunitTest
from retail.test_case.page_obj.memberquery_page import MemberQuery
class MemberQuery_TC(MyunitTest):
"""会员档案查询模块测试用例"""
def test_member_check_ui(self):
"""会员档案查询页面显示正确"""
menu = MemberQuery(self.driver)
self.login.loginFunc()
menu.memberQueryMenu()
elements = menu.findElements(*menu.uiElements)
ele_list = []
for eles in elements:
if eles.text !='':
ele_list.append(eles.text)
self.assertEqual(u'查询条件', ele_list[0])
self.assertEqual(u'会员信息明细', ele_list[1])
self.assertEqual(u'积分变化明细', ele_list[2])
def test_member_type(self):
"""会员类型下拉列表项正确"""
menu = MemberQuery(self.driver)
self.login.loginFunc()
menu.memberQueryMenu()
menu.selectMemberType()
list_type = []
for member_type in menu.memberTypeNum:
text, memeber_type_level = menu.memberTypeLevelOption(*member_type)
list_type.append(text)
self.assertEqual(u'个人会员', list_type[0])
self.assertEqual(u'企业会员', list_type[1])
self.assertEqual(u'其它', list_type[2])
def test_member_level(self):
"""会员级别下拉列表项正确"""
menu = MemberQuery(self.driver)
self.login.loginFunc()
menu.memberQueryMenu()
menu.selectMemberLevel()
list_level = []
for member_level in menu.memberLevelNum:
text, memeber_type_level = menu.memberTypeLevelOption(*member_level)
list_level.append(text)
self.assertEqual(u'标准会员', list_level[0])
self.assertEqual(u'黄金会员', list_level[1])
self.assertEqual(u'铂金会员', list_level[2])
self.assertEqual(u'钻石会员', list_level[3])
def test_member_query_failed(self):
"""默认条件查询成功"""
menu = MemberQuery(self.driver)
self.login.loginFunc()
menu.memberQueryMenu()
menu.cQueryResetBtn(*menu.queryResetBtn[0])
flag = menu.isElementExist(menu.qFailerr)
self.assertFalse(flag, msg=u'查询失败')
def test_alone_query_1(self):
"""按会员编号,会员姓名,手机号码单一条件查询"""
menu = MemberQuery(self.driver)
self.login.loginFunc()
menu.memberQueryMenu()
for num_name_phone in menu.memberNumNamePhone:
menu.reset()
for value in menu.valuesList:
menu.iQueryCondition(num_name_phone,value)
menu.cQueryResetBtn(*menu.queryResetBtn[0])
time.sleep(3)
flag = menu.isElementExist(menu.qFailerr)
if flag:
self.assertTrue(flag, u'提示框不存在,查询成功')
menu.accept(*menu.confirmBtn)
else:
self.assertFalse(flag, u'提示框存在,查询失败')
def test_alone_query_2(self):
"""按会员类型单一查询"""
menu = MemberQuery(self.driver)
self.login.loginFunc()
menu.memberQueryMenu()
for me_type in menu.memberTypeNum:
menu.selectMemberType()
text, member_type_level = menu.memberTypeLevelOption(*me_type)
ActionChains(self.driver).move_to_element(member_type_level).perform()
member_type_level.click()
menu.cQueryResetBtn(*menu.queryResetBtn[0])
time.sleep(3)
flag = menu.isElementExist(menu.qFailerr)
self.assertFalse(flag, u'提示框存在,查询失败')
def test_alone_query_3(self):
"""按会员级别单一查询"""
menu = MemberQuery(self.driver)
self.login.loginFunc()
menu.memberQueryMenu()
for me_level in menu.memberLevelNum:
menu.selectMemberLevel()
text, member_level = menu.memberTypeLevelOption(*me_level)
ActionChains(self.driver).move_to_element(member_level).perform()
member_level.click()
menu.cQueryResetBtn(*menu.queryResetBtn[0])
time.sleep(3)
flag = menu.isElementExist(menu.qFailerr)
self.assertFalse(flag, u'提示框存在,查询成功')
def test_reset(self):
"""重置功能校验"""
menu = MemberQuery(self.driver)
self.login.loginFunc()
menu.memberQueryMenu()
for inputBox in menu.memberNumNamePhone:
menu.iQueryCondition(inputBox,random.choice(menu.valuesList))
menu.selectMemberType()
text_type, member_type = menu.memberTypeLevelOption(*(random.choice(menu.memberTypeNum)))
ActionChains(self.driver).move_to_element(member_type).perform()
member_type.click()
menu.cQueryResetBtn(*menu.queryResetBtn[1])
text_list = []
for input_box in menu.memberNumNamePhone:
text = menu.getInputboxValue(*input_box)
text_list.append(text)
type_type_text = menu.getInputboxValue(*menu.memberTypeBtn)
text_list.append(type_type_text)
for get_attr in text_list:
self.assertEqual('',get_attr)
if __name__ == '__main__':
pass
7.执行测试用例
7.1.from BeautifulReport import BeautifulReport 这个报告需要网上找一下(很多类似的测试报告源码,不一定非使用本案例中的报告模板)。
RunTc.py
import unittest
import time
from BeautifulReport import BeautifulReport
from retail.config.conf import *
from retail.test_case.models.testreport import testreport
if __name__ == '__main__':
runner, fp, fileName = testreport()
test_suite = unittest.defaultTestLoader.discover(tcPath, pattern='LoginTc.py')
runner.run(test_suite)
fp.close()
十.报告展示
1.截图:创建规则失败时截图。 2.截图:登录成功截图。
3.用例执行日志:一部分日志记录。
4.测试报告
十一.总结
1.看到结果还是挺有成就感的,邮件的截图我没发,因为我是内网不知道什么原因邮件服务器连接不上,但是使用外网单独测试邮件发送是没什么问题的!
2.就写这么多吧,其他页面的用例设计思路都是一样的,因为所有的用例彼此都是独立的,所以多与少都不影响! 要源码的同学可以访问我的github自己下载吧!
|