在使用selenium做web自動化測試的過程中,我們經常會遇到各種各樣的問題,比如:
1.當頁面加載緩慢時,selenium lookup元素會拋出壹個異常,導致腳本停止運行。
2.寫完腳本後發現代碼邏輯不夠清晰,維護困難,降低了測試效率。
所以有必要對selenium進行封裝,使框架更適合公司的需求。下面是基類的封裝,主要使用singleton模式,可以將業務邏輯代碼存放在不同的Python文件中。
註意:BASE_URL是博客公園的登錄地址。
1 # -*-編碼:utf-8 -*-?2從selenium導入webdriver?3導入時間?4 ?5 OVER_TIME = 5?6 BASE _ URL = " nb logs . com/user/sign in?ReturnUrl=hFwww.cnblogs.com%2F "?7 ?8 ?9類驅動(對象):10?11def _ _ new _ _ (CLS,* args,* * kW): 12 ""13使用singleton模式將類設置為在運行時只有壹個實例。在其他Python類中使用基類時,14可以創建多個對象。確保所有對象都基於瀏覽器15 ""16如果沒有hasattr (CLS,' _ instance'): 17 orig = super(驅動程序,cls) 18 cls。_instance = orig.__new__(cls,*args,**kw) 19返回cls。_instance 20?21 def start(self,url=BASE_URL,driver_name="Chrome") : 22 ""23啟動瀏覽器24 :param url:測試地址25 :param driver_name:設置啟動時的瀏覽器類型26:return:27 " " 28 if driver _ name = = " Firefox ":29 self . driver = web driver。Firefox()30 elif driver _ name = = " Ie ":31 self . driver = web driver。ie()32 else:33 self . driver = web driver。chrome()34 self . driver . implicitly _ wait(OVER _ TIME)35 self . driver . get(URL)36 self . driver . maximize _ window()37?38 def get_url(self): 39 " " "返回瀏覽器的地址" " 40 return BASE_URL 41?42 def find _ element (self,by,value): 43 """44這裏增加了壹個OVER_TIME作為查找元素的超時。為範圍內的I設置OVER_TIME的大小45 " " 46(OVER _ TIME):47 try:48返回自我。司機。find _ element (by = by,value = value)49 Exception,e: 50 print e 51?52 def find_elements(self,by,Value): 53 " "與find_element " "壹致54 for I in range(over _ time):55 try:56 return self。司機。find _ elements (by = by,value = value)57 Exception,e: 58 print e 59?60 def Find _ display _ elements(self,by,value): 61 """62在顯示狀態下查找壹組元素。在尋找壹類元素時,63往往有些元素是看不見的。這個函數屏蔽那些不可見的元素64 " " 65 for I in range(over _ time):66 try:67 elements = self。driver.find _ elements (by = by,value=value) 68 num =元素。__len__() 69除了例外,e:70 print e 71 time . sleep(1)72 if num & gt;= 1:73 break 74 display _ element =[]75 #將可見元素放入列表中。並為範圍(num)中的j返回76:77 element = elements。_ _ getitem _ _ (j) 78 if元素。is _ displayed()。:79 display _ element . append(element)80返回display_element 81?82 def is _ element _ present (self,by,value): 83 " "判斷壹個元素是否存在" " 84 try: 85 self。司機。Find _ element (by = by,value=Value) 86返回True 87除異常,e: 88打印e 89返回False 90?91 def close(self):92 self . driver . close()93?94 def quit(self): 95 u " " "退出瀏覽器" " 96 self.driver.quit() 97?98 if _ _ name _ _ = = " _ _ main _ _ ":99 page = Driver()100 page . start()
其次,使用封裝的框架和pageobject模式設計登錄腳本。
1 # -*-編碼:utf-8 -*- 2從webdriver導入Driver 3從selenium.webdriver.common.by導入By 4?5 ?6類登錄(對象):7?8 def __init__(self,user_name="xxx ",pwd = " XXX "):9 self . page = Driver()10 page = self . page 11 page . start(URL = page . get _ URL())12 self . user _ name = page . find _ element(By。ID," input 1 ")13 self . pwd = page . find _ element(By。ID," input 2 ")14 self . log in _ BTN = page . find _ element(By。ID," signin")15 self.login(用戶名,密碼)16 17 def login(用戶名,密碼):18 self .用戶名. clear()19 self .用戶名. send_keys(用戶名)20 self . pwd . clear()21 self . pwd . send _ keys(密碼)22 self . log in _ Bt
第三,打開博客公園的新聞、博客問題等子頁面的腳本設計。
1 # -*-編碼:utf-8 -*- 2從webdriver導入Driver 3從login導入Login 4從selenium.webdriver.common.by導入By 5?6 ?7類子頁面(對象):8?9 def _ _ init _ _(self):10 self . page = Driver()11 page = self . page 12 self . home _ page = page . find _ element(By。LINK_TEXT,U“Garden”)13 self . news _ page = page . find _ element(by . LINK _ TEXT,U“news”)14 self . q _ page = page . find _ element(by . LINK _ TEXT,U“溫柏”)16 17 def open _ home _ page(self):15 selfhome _ page . click()19 20 def open _ news _ page(self)。:21 self . news _ page . click()22 23 def open _ q _ page(self):24 self . q _ page . click()25 26 def open _ ing _ page(self):27 self . ing _ page . click()28 29 30 if _ _ name _ _ = = ' _ _ main _ _ ':31 log in(" XXX "," XXX ")32 sub = SubPage()33 sub . open _ q _ page()
第四,其他
根據該模型,博主可以根據公司的具體業務實現自動化測試腳本。這篇文章是壹個討論的話題。如果有其他更好的想法,可以壹起討論。