How to convert Selenese (html) to Python programmatically?
在不手动导出每个测试用例的情况下,如何将SeleniumIDE生成的测试用例转换为Python?那个作业有命令行转换器吗?
最后,我想使用SeleniumRC和pythons内置的unittest来测试我的网站。
谢谢。
更新:
我开始编写一个转换器,但是它的工作太多,无法实现所有的命令。有更好的方法吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | 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" " 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 -*- from selenium import selenium import unittest, time, re ' def getHeader(self): self.dest += u' class %s(unittest.TestCase): ' % self.parser.getTestName() self.dest += u'\tdef setUp(self): \t\tself.verificationErrors = [] ' self.dest += u'\t\tself.selenium = selenium("localhost", 4444,"*chrome","%s") ' % self.parser.getBaseUrl() self.dest += u'\t\tself.selenium.start() ' def getContent(self): self.dest += u' \tdef test_%s(self): \t\tsel = self.selenium ' % 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" ' % (value,target) elif cmd == 'clickAndWait': self.dest += u'\t\tsel.click(u"%s") \t\tsel.wait_for_page_to_load("30000") ' % (target) elif cmd == 'type': self.dest += u'\t\tsel.%s(u"%s", u"%s") ' % (cmd,target,value) elif cmd == 'select': self.dest += u'\t\tsel.select(u"%s", u"%s") ' % (target,value) elif cmd == 'verifyTextPresent': self.dest += u'\t\ttry: self.failUnless(sel.is_text_present(u"%s")) \t\texcept AssertionError, e: self.verificationErrors.append(str(e)) ' % target elif cmd == 'verifySelectedLabel': self.dest += u'\t\ttry: self.assertEqual(u"%s", sel.get_selected_label(u"%s")) \t\texcept AssertionError, e: self.verificationErrors.append(str(e)) ' % (value,target) elif cmd == 'verifyValue': self.dest += u'\t\ttry: self.assertEqual(u"%s", sel.get_value(u"%s")) \t\texcept AssertionError, e: self.verificationErrors.append(str(e)) ' % (value,target) elif cmd == 'verifyText': self.dest += u'\t\ttry: self.assertEqual(u"%s", sel.get_text(u"%s")) \t\texcept AssertionError, e: self.verificationErrors.append(str(e)) ' % (value,target) elif cmd == 'verifyElementPresent': self.dest += u'\t\ttry: self.failUnless(sel.is_element_present(u"%s")) \t\texcept AssertionError, e: self.verificationErrors.append(str(e)) ' % (target) else: self.dest += u'\t\tsel.%s(u"%s") ' % (cmd,target) #print cmd,target,value else: self.dest += u'\t\t#%s ' % node def getFooter(self): self.dest += u' \tdef tearDown(self): \t\tself.selenium.stop() \t\tself.assertEqual([], self.verificationErrors) ' self.dest += u' if __name__ =="__main__": \tunittest.main()' def convert(self): self.getHeader() self.getContent() self.getFooter() return self.dest p = PythonConverter('test_case.html') print p.convert() |
我已经开始构建一个SelenesTopython解析器,Pyselenese,这是我在Github上发布的。您可以自由地分叉/克隆存储库,并尝试一下:http://github.com/jpstacey/pyselenese。
不,没有一种方法,但是理论上它不应该太难做,因为您所需要做的就是使用python-rc.js来转换文件。