读取 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 去学习去创造。