基于Selenium的Web自动化框架增强篇
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类库相关的操作做二次封装,放在BasePage中,其他子类页面自动继承相应的操作方法(如findelement,click等等)
· 封装了findelement方法,可以根据页面元素的(类型,值)进行查找,只需要调用一个方法findelement(s),而不需要针对不同的类型调用不同的find方法(fine_element_by_xxxx())
· 子类页面不需要引用selenium的类库,书写更加简单易读
· 测试用例中也不需要引用selenium的任何类库,简单易读