您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
學(xué)習(xí)Selenium Python版初的一個小想法
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2015/3/13 15:15:26 ] 推薦標(biāo)簽:Selenium 單元測試 測試工具 Python 代碼 XML

  第二步, 根據(jù)每個testcase建立TestCase類的實(shí)例:
class TestCase:
def __init__(self, _testcase):
self.name=_testcase.attrib['name']
_list=[]
self.actions=[]
try:
_list=_testcase.getchildren()
except Exception,msg:
print msg
self._executor=_testcase.find('Executable').text
self.address=_testcase.find('Address').text
for act in _testcase.findall('Action'):
if act.tag=='Action':
_action=Action(act)
self.actions.append(_action)
def setUp(self):
if 'Chrome'==self._executor:
self.executor=webdriver.Chrome()
elif 'Firefox'==self._executor:
self.executor=webdriver.Firefox()
else:
self.executor=webdriver.Ie()
self.executor.get(self.address)
def tearDown(self):
self.executor.quit()
def execute(self):
logging.debug("Start to execute the testcase:%s" % self.name)
print "TestCaseName:%s" % self.name
try:
self.setUp()
for action in self.actions:
action.execute(self.executor);
self.tearDown()
Assert.AssertPass("TestCase:%s " % self.name)
except Exception as error:
print error
self.tearDown()
Assert.AssertFail("TestCase:%s " % self.name)
  第三步,根據(jù)xml里定義的type來觸發(fā)動作,我簡單的列了下:
def execute(self,executor):
_type=self._actions['Type']
self.getBy(self._actions)
try:
if self.by!=None:
ele=self.findElement(executor)
Assert.AssertIsNotNull(ele,"   ".join("Find element by %s:%s" % (str(self.by),self.by_value)))
if _type=='Input':
ele.send_keys(self._actions['Content'])
time.sleep(3)
elif _type=='Click':
ele.click()
elif _type=='Scroll':
webOperate.page_scroll(executor)
elif _type=='Back':
webOperate.goBack(executor)
else:
print "   ",
Assert.AssertFail(self._name)
print "No such action:%s" % _type
if self._expected is not None:
Assert.AssertIsTrue(self.isExpected(executor),"   ".join("Find element by %s:%s" % (str(self.by),self.by_value)))
print "   ",
Assert.AssertPass(self._name)
except AssertionError:
print "   ",
Assert.AssertFail(self._name)
raise AssertionError(self._name +" execute failed")
  看一個運(yùn)行的截圖

  代碼比較簡單,原理也很清晰,是解析XML文件,然后調(diào)用相應(yīng)的selenium代碼,但是如果我們再深入的想想,在解析這些XML的時候,是不是可以以更友好的方式來展示生成的代碼呢,甚或者以UI的方式,一條條展示。然后再提供可編輯功能?甚或者再提供錄制的方式來自動生成這些XML文件,再解析成代碼,這不是一個強(qiáng)大的自動化測試工具??

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd