html网站怎么转换为编程,如何以编程方式将Selenese(html)转换为Python?

如何将Selenium IDE生成的测试用例转换为Python而不手动导出每个测试用例?有没有命令行转换器来完成这个任务?在

最后,我想使用seleniumrc和Pythons内置的unittest来测试我的网站。在

非常感谢。在

更新:

我开始写一个转换器,但它太多的工作来实现所有的命令。有更好的办法吗?在from xml.dom.minidom import parse

class SeleneseParser:

def __init__(self,selFile):

self.dom = parse(selFile)

def getTestName(self):

return self.dom.getElementsByTagName('title')[0].firstChild.data

def getBaseUrl(self):

return self.dom.getElementsByTagName('link')[0].getAttribute('href')

def getNodes(self):

cmds = []

nodes = self.dom.getElementsByTagName('tbody')[0].childNodes

for node in nodes:

if node.nodeType == node.TEXT_NODE and "\n" in node.data:

continue

if node.nodeType == node.COMMENT_NODE:

cmds.append(node.data)

if node.nodeType == node.ELEMENT_NODE:

cmd = []

for c in node.childNodes:

if c.nodeType == node.ELEMENT_NODE:

if len(c.childNodes) == 1:

cmd.append(c.childNodes[0].data)

else:

cmd.append("")

cmds.append(cmd)

return cmds

class PythonConverter:

def __init__(self,sourceFile):

self.parser = SeleneseParser(sourceFile)

self.dest = u'# -*- coding: utf-8 -*-\n\nfrom selenium import selenium\nimport unittest, time, re\n'

def getHeader(self):

self.dest += u'\nclass %s(unittest.TestCase):\n' % self.parser.getTestName()

self.dest += u'\tdef setUp(self):\n\t\tself.verificationErrors = []\n'

self.dest += u'\t\tself.selenium = selenium("localhost", 4444, "*chrome", "%s")\n' % self.parser.getBaseUrl()

self.dest += u'\t\tself.selenium.start()\n'

def getContent(self):

self.dest += u'\n\tdef test_%s(self):\n\t\tsel = self.selenium\n' % self.parser.getTestName()

nodes = self.parser.getNodes()

for node in nodes:

if type(node) is list:

cmd,target,value = node[0],node[1],node[2]

if cmd == 'store':

self.dest += u'\t\t%s = "%s"\n' % (value,target)

elif cmd == 'clickAndWait':

self.dest += u'\t\tsel.click(u"%s")\n\t\tsel.wait_for_page_to_load("30000")\n' % (target)

elif cmd == 'type':

self.dest += u'\t\tsel.%s(u"%s", u"%s")\n' % (cmd,target,value)

elif cmd == 'select':

self.dest += u'\t\tsel.select(u"%s", u"%s")\n' % (target,value)

elif cmd == 'verifyTextPresent':

self.dest += u'\t\ttry: self.failUnless(sel.is_text_present(u"%s"))\n\t\texcept AssertionError, e: self.verificationErrors.append(str(e))\n' % target

elif cmd == 'verifySelectedLabel':

self.dest += u'\t\ttry: self.assertEqual(u"%s", sel.get_selected_label(u"%s"))\n\t\texcept AssertionError, e: self.verificationErrors.append(str(e))\n' % (value,target)

elif cmd == 'verifyValue':

self.dest += u'\t\ttry: self.assertEqual(u"%s", sel.get_value(u"%s"))\n\t\texcept AssertionError, e: self.verificationErrors.append(str(e))\n' % (value,target)

elif cmd == 'verifyText':

self.dest += u'\t\ttry: self.assertEqual(u"%s", sel.get_text(u"%s"))\n\t\texcept AssertionError, e: self.verificationErrors.append(str(e))\n' % (value,target)

elif cmd == 'verifyElementPresent':

self.dest += u'\t\ttry: self.failUnless(sel.is_element_present(u"%s"))\n\t\texcept AssertionError, e: self.verificationErrors.append(str(e))\n' % (target)

else:

self.dest += u'\t\tsel.%s(u"%s")\n' % (cmd,target)

#print cmd,target,value

else:

self.dest += u'\t\t#%s\n' % node

def getFooter(self):

self.dest += u'\n\tdef tearDown(self):\n\t\tself.selenium.stop()\n\t\tself.assertEqual([], self.verificationErrors)\n'

self.dest += u'\nif __name__ == "__main__":\n\tunittest.main()'

def convert(self):

self.getHeader()

self.getContent()

self.getFooter()

return self.dest

p = PythonConverter('test_case.html')

print p.convert()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值