一.設計測試用例html
1.分析我以前寫的登陸腳本:web
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()
2.分析腳本功能是否能夠模塊化瀏覽器
3.模塊化與數據隔離模塊化
4.修改後的模型測試
二.測試用例模塊化ui
1.url
2.模塊化的代碼:spa
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()