基于Selenium的Web自動(dòng)化框架增強(qiáng)篇
作者:
網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:
[ 2016/12/19 11:10:32 ] 推薦標(biāo)簽:
Selenium 功能測(cè)試
self.driver.quit()
def getAttribute(self, element, attribute):
'''
Get element attribute
'''
return element.get_attribute(attribute)
def getText(self, element):
'''
Get text of a web element
'''
return element.text
def getTitle(self):
'''
Get window title
'''
return self.driver.title
def getCurrentUrl(self):
'''
Get current url
'''
return self.driver.current_url
def getScreenshot(self,targetpath):
'''
Get current screenshot and save it to target path
'''
self.driver.get_screenshot_as_file(targetpath)
def maximizeWindow(self):
'''
Maximize current browser window
'''
self.driver.maximize_window()
def back(self):
'''
Goes one step backward in the browser history.
'''
self.driver.back()
def forward(self):
"""
Goes one step forward in the browser history.
"""
self.driver.forward()
def getWindowSize(self):
"""
Gets the width and height of the current window.
"""
return self.driver.get_window_size()
def refresh(self):
'''
Refresh current page
'''
self.driver.refresh()
self.driver.switch_to()
GoogleMainPage:
from BasePage import BasePage
class GoogleMainPage(BasePage):
"""description of class"""
searchbox = ('ID','lst-ib')
def __init__(self, browser = 'chrome'):
super().__init__(browser)
def inputSearchContent(self,searchContent):
searchBox = self.findElement(self.searchbox)
self.type(searchBox,searchContent)
self.enter(searchBox)
Test
所做的改變:
· 將與Selenium類庫(kù)相關(guān)的操作做二次封裝,放在BasePage中,其他子類頁(yè)面自動(dòng)繼承相應(yīng)的操作方法(如findelement,click等等)
· 封裝了findelement方法,可以根據(jù)頁(yè)面元素的(類型,值)進(jìn)行查找,只需要調(diào)用一個(gè)方法findelement(s),而不需要針對(duì)不同的類型調(diào)用不同的find方法(fine_element_by_xxxx())
· 子類頁(yè)面不需要引用selenium的類庫(kù),書(shū)寫(xiě)更加簡(jiǎn)單易讀
· 測(cè)試用例中也不需要引用selenium的任何類庫(kù),簡(jiǎn)單易讀