您的位置:软件测试 > 开源软件测试 > 开源功能测试工具 > Selenium
基于Selenium的Web自动化框架增强篇
作者:网络转载 发布时间:[ 2016/12/19 11:10:32 ] 推荐标签:Selenium 功能测试

  在写完上一篇“基于Selenium的Web自动化框架”之后一直没有时间重新审视该框架,正好趁着给同事分享的机会,重新分析了一下框架,发现了很多不足之处,所以才有了这篇增强版。
  那么问题来了,到底在框架的哪一部分做了增强呢?这次主要从设计模式的角度来简单介绍一下。
  首先我们来看一下之前是如何书写页面模式中的类的:
BasePage:
class BasePage(object):
"""description of class"""
#webdriver instance
def __init__(self, driver):
self.driver = driver
GoogleMainPage:
from BasePage import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
class GoogleMainPage(BasePage):
"""description of class"""
searchbox = (By.ID,'lst-ib')
def inputSearchContent(self,searchContent):
searchBox = self.driver.find_element(*self.searchbox)
searchBox.send_keys(searchContent+Keys.RETURN)
  重新审视之前的实现,我们可以发现在各个子类页面中,均需要引用相当的selenium类库(比如webdriver),并且需要用webdriver来定位页面元素,这会造成各个子类页面与selenium类库有较多的集成,并且也是书写上的浪费。
  现在来看一下做了结构调整的部分呈现:
BasePage:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
class BasePage(object):
"""description of class"""
#webdriver instance
def __init__(self, browser='chrome'):
'''
initialize selenium webdriver, use chrome as default webdriver
'''
if browser == "firefox" or browser == "ff":
driver = webdriver.Firefox()
elif browser == "chrome":
driver = webdriver.Chrome()
elif browser == "internet explorer" or browser == "ie":
driver = webdriver.Ie()
elif browser == "opera":
driver = webdriver.Opera()
elif browser == "phantomjs":
driver = webdriver.PhantomJS()
try:
self.driver = driver
except Exception:
raise NameError("Not found %s browser,You can enter 'ie', 'ff' or 'chrome'." % browser)
def findElement(self,element):
'''
Find element
element is a set with format (identifier type, value), e.g. ('id','username')
Usage:
self.findElement(element)
'''
try:
type = element[0]
value = element[1]
if type == "id" or type == "ID" or type=="Id":
elem = self.driver.find_element_by_id(value)
elif type == "name" or type == "NAME" or type=="Name":
elem = self.driver.find_element_by_name(value)
elif type == "class" or type == "CLASS" or type=="Class":
elem = self.driver.find_element_by_class_name(value)
elif type == "link_text" or type == "LINK_TEXT" or type=="Link_text":
elem = self.driver.find_element_by_link_text(value)
elif type == "xpath" or type == "XPATH" or type=="Xpath":
elem = self.driver.find_element_by_xpath(value)
elif type == "css" or type == "CSS" or type=="Css":
elem = self.driver.find_element_by_css_selector(value)
else:
raise NameError("Please correct the type in function parameter")
except Exception:
raise ValueError("No such element found"+ str(element))
return elem
def findElements(self,element):
'''
Find elements
element is a set with format (identifier type, value), e.g. ('id','username')
Usage:
self.findElements(element)
'''
try:
type = element[0]
value = element[1]
if type == "id" or type == "ID" or type=="Id":
elem = self.driver.find_elements_by_id(value)
elif type == "name" or type == "NAME" or type=="Name":
elem = self.driver.find_elements_by_name(value)
elif type == "class" or type == "CLASS" or type=="Class":
elem = self.driver.find_elements_by_class_name(value)
elif type == "link_text" or type == "LINK_TEXT" or type=="Link_text":
elem = self.driver.find_elements_by_link_text(value)
elif type == "xpath" or type == "XPATH" or type=="Xpath":
elem = self.driver.find_elements_by_xpath(value)
elif type == "css" or type == "CSS" or type=="Css":
elem = self.driver.find_elements_by_css_selector(value)
else:
raise NameError("Please correct the type in function parameter")
except Exception:
raise ValueError("No such element found"+ str(element))
return elem
def open(self,url):
'''
Open web url
Usage:
self.open(url)
'''
if url != "":
self.driver.get(url)
else:
raise ValueError("please provide a base url")
def type(self,element,text):
'''
Operation input box.
Usage:
self.type(element,text)
'''
element.send_keys(text)
def enter(self,element):
'''
Keyboard: hit return
Usage:
self.enter(element)
'''
element.send_keys(Keys.RETURN)
def click(self,element):
'''
Click page element, like button, image, link, etc.
'''
element.click()
def quit(self):
'''
Quit webdriver
'''

上一页12下一页
软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd