self.sel.select(element, selection)
def Check(self, element):
if self.isElementPresent(element):
self.sel.check(element)
def Type(self, element, text):
if self.isElementPresent(element):
self.sel.type(element, text)
測試數據分離, 這個做的非常簡單,就是把期望值和控件位置作為測試數據放入 xml 中。
GWEB
link
//span[text()="Web"]
Web
GIMAGES
link
//span[text()="Images"]
Images
name 表示控件名字,type 為控件的類型,locator 表示控制位置,expect_value 表示期望值。
讀取 xml 的內容。這段代碼屬于我現學現賣,直接看了一下 python 對 xml 的支持,然后自己搗鼓了一下就寫出來了,可以獲取自己需要的 xml 里面的數據。
from xml.dom import minidom
class PageData():
def __init__(self, page_name, file):
self.name = page_name
self.data = minidom.parse(file)
self.xpth_dict = self.XML_Dict()
def XML_Dict(self):
xpath_dict = {}
for i in self.data.getElementsByTagName(self.name):
xpath_dict[str(i.childNodes[1].firstChild.nodeValue)] = \
[str(i.childNodes[5].firstChild.nodeValue),\
str(i.childNodes[7].firstChild.nodeValue)]
return xpath_dict
def GetLocator(self, object_name):
return self.xpth_dict[object_name][0]
def GetValue(self, object_name):
return self.xpth_dict[object_name][1]
最后,主程序里面,我們就能用下面的方式,執行我們本來已經設計好的案例。
def testAutoCompleteFunctionMouseMove(self):
'''test the function of auto complete. case 2: when user move mouse to the suggestion, there will be a link '''
self.initTest("testAutoCompleteFunctionMouseMove")
self.open('http://www.google.com/ncr')
GoogleHomePage = data_parser.PageData("GoogleHomePage", self.data_file)
self.Type(GoogleHomePage.GetLocator('SEARCHTEXT'), 's')
GoogleAutoComplete = data_parser.PageData("AutoCompleteCase", self.data_file)
self.isElementPresent(GoogleAutoComplete.GetLocator("SUGGESTIONFIELD"))
time.sleep(10)
self.MouseMove(GoogleAutoComplete.GetLocator("SUGGESTIONONEFORS"))
expect_text = "I'm Feeling Lucky »"
self.assertLogTrue(self.isTextPresent(expect_text), "The text %s has been displayed" %expect_text)
self.MouseMove(GoogleAutoComplete.GetLocator("SUGGESTIONTWOFORS"))
time.sleep(10)
self.assertLogTrue(self.isTextPresent(expect_text), "The text %s has been displayed" %expect_text)
self.endTest()
這樣,一個相當簡易的自動化測試框架基本完工??梢酝晟频牡胤綄嵲谔嗔?,希望有志之士去完善吧,加入些新功能,譬如錯誤出現的時候截圖,然后統計測試用例總數以及通過的數量。
后記
作為一個菜鳥測試工程師,沒有任何的開發經驗,搞出這個東西還是有點小激動的,也激發了本人對許多事情的興趣。 以后希望能有更好的 test frame 去學習去創造。
原文轉自:http://www.anti-gravitydesign.com