您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
基于Selenium的Web自動化框架增強(qiáng)篇
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2016/12/19 11:10:32 ] 推薦標(biāo)簽:Selenium 功能測試

  在寫完上一篇“基于Selenium的Web自動化框架”之后一直沒有時(shí)間重新審視該框架,正好趁著給同事分享的機(jī)會,重新分析了一下框架,發(fā)現(xiàn)了很多不足之處,所以才有了這篇增強(qiáng)版。
  那么問題來了,到底在框架的哪一部分做了增強(qiáng)呢?這次主要從設(shè)計(jì)模式的角度來簡單介紹一下。
  首先我們來看一下之前是如何書寫頁面模式中的類的:
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)
  重新審視之前的實(shí)現(xiàn),我們可以發(fā)現(xiàn)在各個子類頁面中,均需要引用相當(dāng)?shù)膕elenium類庫(比如webdriver),并且需要用webdriver來定位頁面元素,這會造成各個子類頁面與selenium類庫有較多的集成,并且也是書寫上的浪費(fèi)。
  現(xiàn)在來看一下做了結(jié)構(gòu)調(diào)整的部分呈現(xiàn):
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下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd