输入项 | 允许输入 | 可为空 | 格式规范 | 合法性 | 依赖项 |
---|---|---|---|---|---|
原管理员密码 | 是 | 否 | 字符串 | 长度限制:5-63; 字符集:英文字符集; | 需要与管理员密码相同 |
新管理员密码 | 是 | 否 | 字符串 | 长度限制:5-63; 字符集:英文字符集; | |
确认管理员密码 | 是 | 否 | 字符串 | 需要与新管理员密码相同 |
根据防呆规则可以列出:1.可能出现的错误 2.出现错误时页面应有的提示语
#可能出现的错误 errcode = ['oldPwdErr', 'lenErr', 'charErr', 'matchErr', 'pwdSameErr',\ 'oldPwdBlankErr', 'newPwdBlankErr'] #出现错误时页面应有的提示语 errTips = { 'oldPwdErr' :'原密码错误', 'lenErr' : '新密码长度应为5~63位', 'charErr' : "新密码包含非法字符", 'matchErr' : '两次密码输入不一致', 'pwdSameErr' : '新密码与原密码相同,请重新输入', 'oldPwdBlankErr' : '请输入原密码', 'newPwdBlankErr' : '请输入新密码' }
需要输入的数据为要检查的data和登陆密码
def checkData(data, loginPwd):#检查顺序跟页面顺序相同 pwd = loginPwd #'oldPwdBlankErr' if data['pwdOld'] == "": return errcode[5] #newPwdBlankErr if data['pwdNew'] == "": return errcode[6] #charErr strTmp = data['pwdNew'] for x in xrange(0,len(data['pwdNew'])): if ord(strTmp[x]) 33 or ord(strTmp[x]) > 127:#ASCII表示范围:32-127 return errcode[2] #lenErr if len(data['pwdNew']) > 63 or len(data['pwdNew']) 5: return errcode[1] #oldPwdErr if pwd != loginData.login_data['login_pwd']: return errcode[0] #pwdSameErr if data['pwdNew'] == pwd: return errcode[4] #no error return None
def checkResponse(error): if error == None: return webText = getText('//*[@id="PwdTip"]') if webText == False:#没有提示 print('###Error: no tips on web!') else: webText = webText.decode('UTF-8') waitandClick('//*[@id="ModifyPwd"]/i') time.sleep(1) return webText
data = [ {"pwdNew" : "12345678","pwdOld" : '8dadla'},#"oldPwdErr" {"pwdNew" : "admi","pwdOld" : 'admin'},#lenErr {'pwdNew' : '1 2 3','pwdOld' : 'admin'},#charErr {'pwdNew' : 'admin','pwdOld' : 'admin'},#pwdSameErr {'pwdNew' : "",'pwdOld' : ""},#oldPwdBlank {'pwdNew' : "",'pwdOld' : "admin"}#newPwdBlank ]
单元测试中,不同的部分应该是数据,所以可以定义一个通用的操作。
其中self.assertEqual(checkResponse(error), errTips[error])是判定测试是否通过的条件:页面提示语是否正确。
def commonAction(self, arg): error = checkData(arg) changeUserPwd.main(arg) self.assertEqual(checkResponse(error), errTips[error])
测试类中主要包括了测试用例6个,和对应的以“test”开头的测试函数。
这里继承了python的unittest。
关于unittest的语法请参考://www.jb51.net/article/65856.htm
class TestCase(unittest.TestCase): data = [ {"pwdNew" : "12345678","pwdOld" : '8dadla'},#"oldPwdErr" {"pwdNew" : "admi","pwdOld" : '*'},#lenErr {'pwdNew' : '1 2 3','pwdOld' : '*'},#charErr {'pwdNew' : 'admin','pwdOld' : '*'},#pwdSameErr {'pwdNew' : "",'pwdOld' : ""},#oldPwdBlank {'pwdNew' : "",'pwdOld' : "*"}#newPwdBlank ] def commonAction(self, arg): error = checkData(arg) changeUserPwd.main(arg) self.assertEqual(checkResponse(error), errTips[error]) def test_oldPwdErr(self): self.commonAction(self.data[0]) def test_lenErr(self): self.commonAction(self.data[1]) def test_charErr(self): self.commonAction(self.data[2]) def test_pwdSameErr(self): self.commonAction(self.data[3]) def test_oldPwdBlank(self): self.commonAction(self.data[4]) def test_newPwdBlank(self): self.commonAction(self.data[5])
这里利用了HTMLTestRunner来生成测试报告。
HTMLTestRunner语法请参看:https://testerhome.com/topics/7576
生成的测试报告将会存放在reports/test_report文件夹下,按照时间命名。测试报告的title叫做“修改管理员密码试报告”
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='test_report',report_title='修改管理员密码试报告'))
def closeDriver(): time.sleep(3) driver.quit() os.system('killall chromedriver') os.system('killall geckodriver')
到这里,我们可以完成修改用户名密码模块的单元测试了,为了增加代码的健壮性,下面介绍异常处理。
点击按钮的时候可能出现的异常情况是:可能页面元素还没有加载出来的时候,点击的动作就发生了。这样就会引发找不到元素异常。
解决的方法是通过显示等待,每10ms检查一次页面元素是否加载完成,完成后就点击,否则就等到超时时间之后结束动作。
def waitandClick(xpath): try: WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, xpath))) except TimeoutException as e: print('Error:waitandClick, TimeoutException, xpath = %s\n' % xpath) else: driver.find_element_by_xpath(xpath).click()
在填写表单时,除了页面元素还没有加载完成的异常外,还可能原有表单中有文本,而我们的输入则是以追加模式填写的。这就会导致填写的文本不准确。
def waitandSendkeys(xpath, keys): try: WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath))) except TimeoutException as e: print('Error:waitandSendkeys, TimeoutException, xpath = %s\n' % xpath) else: driver.find_element_by_xpath(xpath).clear() driver.find_element_by_xpath(xpath).send_keys(keys)
在元素加载中可能出现: 1. 在超时时间内元素没有加载完成 2. 查询的元素根本不存在
针对这两种情况进行异常处理:
def waitforDisplay(xpath): try: WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath))) except TimeoutException as e: print('Error:waitforDisplay, TimeoutException, xpath = %s\n' % xpath) else: try: process = driver.find_element_by_xpath(xpath) WebDriverWait(driver, 10).until(lambda driver: process.is_displayed()) except NoSuchElementException as e: print('Error:waitforDisplay, NoSuchElementException, xpath = %s\n' % xpath)
# -*- coding: UTF-8 -*- #!/usr/bin/env python from selenium import webdriver import time, os from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import TimeoutException import unittest import HtmlTestRunner import sys reload(sys) sys.setdefaultencoding('utf-8') # 1. 打开浏览器,访问p.to driver = webdriver.Chrome() def openDriver(): driver.get("http://p.to") driver.maximize_window() # 2. 登陆 class loginClass(object): """docstring for login""" def __init__(self, arg): self.login_pwd = arg def login(self): waitandSendkeys('//*[@id="Pwd"]', self.login_pwd) waitandClick('//*[@id="Save"]') def login(data): openDriver() test1 = loginClass(data) test1.login() # 3.修改管理员密码 class changePwdClass(object): """docstring for changePwdClass""" def __init__(self, arg): self.pwdNew = arg.get('pwdNew', '') self.pwdOld = arg.get('pwdOld', '') def changeUserPwd(self): waitandClick('//*[@id="Con"]/div[1]/ul[2]/li[1]') waitandClick('//*[@id="Con"]/div[1]/ul[2]/li[1]/ul/li[3]') waitforDisplay('//*[@id="_Widget"]') waitandSendkeys('//*[@id="PwdOld"]', self.pwdOld) waitandSendkeys('//*[@id="PwdNew"]', self.pwdNew) waitandSendkeys('//*[@id="PwdCfm"]', self.pwdNew) waitandClick('//*[@id="SavePwd"]') def changeUserPwd_main(data): changePwdObj = changePwdClass(data) changePwdObj.changeUserPwd() # 4. 单元测试数据 errcode = ['oldPwdErr', 'lenErr', 'charErr', 'matchErr', 'pwdSameErr',\ 'oldPwdBlankErr', 'newPwdBlankErr'] errTips = { 'oldPwdErr' :'原密码错误', 'lenErr' : '新密码长度应为5~63位', 'charErr' : "新密码包含非法字符", 'matchErr' : '两次密码输入不一致', 'pwdSameErr' : '新密码与原密码相同,请重新输入', 'oldPwdBlankErr' : '请输入原密码', 'newPwdBlankErr' : '请输入新密码' } # 5. 检查输入的数据合法性 def checkData(data):#检查顺序跟页面顺序相同 #pwd = loginPwd pwd='admin' #'oldPwdBlankErr' if data['pwdOld'] == "": return errcode[5] #newPwdBlankErr if data['pwdNew'] == "": return errcode[6] #charErr strTmp = data['pwdNew'] for x in xrange(0,len(data['pwdNew'])): if ord(strTmp[x]) 33 or ord(strTmp[x]) > 127:#ASCII表示范围:32-127 return errcode[2] #lenErr if len(data['pwdNew']) > 63 or len(data['pwdNew']) 5: return errcode[1] #oldPwdErr if pwd != data['pwdOld']: return errcode[0] #pwdSameErr if data['pwdNew'] == data['pwdOld']: return errcode[4] #no error return None # 6. 获取输入错误数据之后的页面提示语 def checkResponse(error): if error == None: return # webText = driver.find_element_by_xpath('//*[@id="PwdTip"]').text webText = getText('//*[@id="PwdTip"]') if webText == False:#没有提示 print('###Error: no tips on web!') else: webText = webText.decode('UTF-8') waitandClick('//*[@id="ModifyPwd"]/i') return webText # 8.单元测试类 class TestCase(unittest.TestCase): # 7. 编写测试用例 data = [ {"pwdNew" : "12345678","pwdOld" : '8dadla'},#"oldPwdErr" {"pwdNew" : "admi","pwdOld" : 'admin'},#lenErr {'pwdNew' : '1 2 3','pwdOld' : 'admin'},#charErr {'pwdNew' : 'admin','pwdOld' : 'admin'},#pwdSameErr {'pwdNew' : "",'pwdOld' : ""},#oldPwdBlank {'pwdNew' : "",'pwdOld' : "admin"}#newPwdBlank ] def commonAction(self, arg): error = checkData(arg) changeUserPwd_main(arg) self.assertEqual(checkResponse(error), errTips[error]) time.sleep(1) def test_oldPwdErr(self): self.commonAction(self.data[0]) def test_lenErr(self): self.commonAction(self.data[1]) def test_charErr(self): self.commonAction(self.data[2]) def test_pwdSameErr(self): self.commonAction(self.data[3]) def test_oldPwdBlank(self): self.commonAction(self.data[4]) def test_newPwdBlank(self): self.commonAction(self.data[5]) # 10. 关闭浏览器 def closeDriver(): time.sleep(3) driver.quit() os.system('killall chromedriver') os.system('killall geckodriver') # 11. 异常处理 ## 11.1 点击函数 def waitandClick(xpath): try: WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, xpath))) except TimeoutException as e: print('Error:waitandClick, TimeoutException, xpath = %s\n' % xpath) else: driver.find_element_by_xpath(xpath).click() ## 11.2 填写表单 def waitandSendkeys(xpath, keys): try: WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath))) except TimeoutException as e: print('Error:waitandSendkeys, TimeoutException, xpath = %s\n' % xpath) else: driver.find_element_by_xpath(xpath).clear() driver.find_element_by_xpath(xpath).send_keys(keys) ## 11.3 元素加载 def waitforDisplay(xpath): try: WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath))) except TimeoutException as e: print('Error:waitforDisplay, TimeoutException, xpath = %s\n' % xpath) else: try: process = driver.find_element_by_xpath(xpath) WebDriverWait(driver, 10).until(lambda driver: process.is_displayed()) except NoSuchElementException as e: print('Error:waitforDisplay, NoSuchElementException, xpath = %s\n' % xpath) def elementIsDisplayed(xpath): try: driver.find_element_by_xpath(xpath) except NoSuchElementException as e: return False def getText(xpath): time.sleep(1) return driver.find_element_by_xpath(xpath).text if __name__ == '__main__': openDriver() login('admin') #data = {'pwdNew'='admin', 'pwdOld'='12345678'} #changeUserPwd_main(data) #9. 进行单元测试并生成测试报告 unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='test_report',report_title='修改管理员密码试报告')) closeDriver()
完整demo请参看:https://github.com/niununu/k2p_web_test
到此这篇关于python实现自动化脚本编写的文章就介绍到这了,更多相关python 自动化脚本 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!