一.设计测试用例

1.分析我之前写的登录脚本:

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait url = 'http://www.maiziedu.com/'
login_path = '/html/body/div[2]/div/div/div/div/a[2]'
account = 'xxx' #这里需填实际的账号
pwd = 'maizi123456' #预先设定好网址和账号密码 def get_ele_times(driver,times,func):
return WebDriverWait(driver,times).until(func) #等待方法 def login_test():
driver = webdriver.Firefox() #open firefox
driver.get(url) #打开url
time.sleep(1)
driver.maximize_window() #最大化窗口 ele_login = get_ele_times(driver,10,\
lambda driver:driver.find_element_by_xpath(login_path))
ele_login.click() #找到登录,然后点击
#相当于:driver.find_element_by_link_text(login_text).click() account_ele = driver.find_element_by_xpath('//*[@id="id_account_l"]')
account_ele.send_keys('')
account_ele.send_keys(account) password_ele = driver.find_element_by_xpath('//*[@id="id_password_l"]')
password_ele.send_keys('')
password_ele.send_keys(pwd) driver.find_element_by_xpath('//*[@id="login_btn"]').click()
try:
driver.find_element_by_link_text('该账号不正确')
print ("ACCOUNT AND PWD ERROR!")
except:
print ("ACCOUNT AND PWD RIGHT!") if __name__ == '__main__':
login_test()

Selenium(八)测试用例的设计与模块化-LMLPHP

2.分析脚本功能是否可以模块化

Selenium(八)测试用例的设计与模块化-LMLPHP

3.模块化与数据隔离

Selenium(八)测试用例的设计与模块化-LMLPHP

4.修改后的模型

Selenium(八)测试用例的设计与模块化-LMLPHP

二.测试用例模块化

1.

Selenium(八)测试用例的设计与模块化-LMLPHP

2.模块化的代码:

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait url = 'http://www.maiziedu.com/'
login_xpath = '/html/body/div[2]/div/div/div/div/a[2]'
account = 'XXX'
pwd = 'maizi123456' #预先设定好网址和账号密码 def openBrower(): #配置浏览器
webdriver_handle = webdriver.Firefox()
return webdriver_handle def openUrl(handle,url): #打开url
handle.get(url) def get_ele_times(driver,times,func):
return WebDriverWait(driver,times).until(func) #等待方法 def findElement(driver,arg):
'''
arg must be dict
1.user_xpath:用户名
2.pwd_xpath:密码
3.login_xpath:登录按钮
return useEle,pwdEle,loginEle
''' useEle = driver.find_element_by_xpath(arg['user_xpath'])
pwdEle = driver.find_element_by_xpath(arg['pwd_xpath'])
loginEle = driver.find_element_by_xpath(arg['login_xpath'])
return useEle,pwdEle,loginEle def sendVals(eletuple,arg):
'''
ele tuple
account:uname,pwd
'''
listkey = ['uname','pwd']
i = 0
for key in listkey:
eletuple[i].send_keys('')
eletuple[i].clear()
eletuple[i].send_keys(arg[key])
i+=1
eletuple[2].click()
def checkResult(driver,text):
try:
driver.find_element_by_link_text(test)
print ("ACCOUNT AND PWD ERROR!")
except:
print ("ACCOUNT AND PWD RIGHT!") def login_test():
driver = openBrower()
openUrl(driver,url)
driver.maximize_window()
ele_login = get_ele_times(driver,10,lambda driver:driver.find_element_by_xpath(login_xpath))
ele_login.click()
#字典
ele_dict = {'user_xpath':'//*[@id="id_account_l"]',\
'pwd_xpath':'//*[@id="id_password_l"]','login_xpath':'//*[@id="login_btn"]'}
account_dict = {'uname':account,'pwd':pwd} ele_tuple = findElement(driver,ele_dict) #接收字典的值 sendVals(ele_tuple,account_dict)
checkResult(driver,'该账号不正确')
driver.find_element_by_link_text('退出').click() if __name__ == '__main__':
login_test()
05-19 11:12