如何将Selenese(html)以编程方式转换为Python?

如何将由Selenium IDE制作的测试用例转换为Python,而无需手动导出每个测试用例? 那个工作有没有命令行转换器?

最后,我想使用Selenium RC和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 -*-nnfrom selenium import seleniumnimport unittest, time, ren'

    def getHeader(self):
        self.dest += u'nclass %s(unittest.TestCase):n' % self.parser.getTestName()
        self.dest += u'tdef setUp(self):nttself.verificationErrors = []n'
        self.dest += u'ttself.selenium = selenium("localhost", 4444, "*chrome", "%s")n' % self.parser.getBaseUrl()
        self.dest += u'ttself.selenium.start()n'

    def getContent(self):
        self.dest += u'ntdef test_%s(self):nttsel = self.seleniumn' % 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'tt%s = "%s"n' % (value,target)
                elif cmd == 'clickAndWait':
                    self.dest += u'ttsel.click(u"%s")nttsel.wait_for_page_to_load("30000")n' % (target)   
                elif cmd == 'type':
                    self.dest += u'ttsel.%s(u"%s", u"%s")n' % (cmd,target,value)
                elif cmd == 'select':
                    self.dest += u'ttsel.select(u"%s", u"%s")n' % (target,value)
                elif cmd == 'verifyTextPresent':
                    self.dest += u'tttry: self.failUnless(sel.is_text_present(u"%s"))nttexcept AssertionError, e: self.verificationErrors.append(str(e))n' % target
                elif cmd == 'verifySelectedLabel':
                    self.dest += u'tttry: self.assertEqual(u"%s", sel.get_selected_label(u"%s"))nttexcept AssertionError, e: self.verificationErrors.append(str(e))n' % (value,target)
                elif cmd == 'verifyValue':
                    self.dest += u'tttry: self.assertEqual(u"%s", sel.get_value(u"%s"))nttexcept AssertionError, e: self.verificationErrors.append(str(e))n' % (value,target)
                elif cmd == 'verifyText':
                    self.dest += u'tttry: self.assertEqual(u"%s", sel.get_text(u"%s"))nttexcept AssertionError, e: self.verificationErrors.append(str(e))n' % (value,target)
                elif cmd == 'verifyElementPresent':
                    self.dest += u'tttry: self.failUnless(sel.is_element_present(u"%s"))nttexcept AssertionError, e: self.verificationErrors.append(str(e))n' % (target)
                else:
                    self.dest += u'ttsel.%s(u"%s")n' % (cmd,target)

                #print cmd,target,value
            else:
                self.dest += u'tt#%sn' % node

    def getFooter(self):
        self.dest += u'ntdef tearDown(self):nttself.selenium.stop()nttself.assertEqual([], self.verificationErrors)n'
        self.dest += u'nif __name__ == "__main__":ntunittest.main()'

    def convert(self):
        self.getHeader()
        self.getContent()
        self.getFooter()
        return self.dest

p = PythonConverter('test_case.html')
print p.convert()

我已经开始构建一个Selenese-to-Python解析器PySelenese,我在Github上发布了这个解析器。 随意分叉/克隆存储库,并试一试:http://github.com/jpstacey/PySelenese。


不,没有办法,但理论上它不应该太难做,因为你需要做的只是使用python-rc.js来转换文件。

链接地址: http://www.djcxy.com/p/50787.html

上一篇: How to convert Selenese (html) to Python programmatically?

下一篇: Is there a way to keep Hudson / Jenkins configuration files in source control?