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

发布于 2024-08-29 00:19:52 字数 4408 浏览 7 评论 0 原文

如何将 Selenium IDE 制作的测试用例转换为 Python,而不需要手动导出每个测试用例?有没有适合这项工作的命令行转换器?

最后,我想使用 Selenium RC 和 Python 构建在单元测试中来测试我的网站。

多谢。

更新:

我开始编写一个转换器,但实现所有命令的工作量太大。还有更好的办法吗?

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()

How would I convert test cases made by Selenium IDE to Python without exporting every test case by hand? Is there any command line converter for that job?

In the end I want to use Selenium RC and Pythons build in unittest to test my websites.

Thanks a lot.

Update:

I started to write a converter but its too much work to implement all the commands. Is there any better way?

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留一抹残留的笑 2024-09-05 00:19:52

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

I've started building a Selenese-to-Python parser, PySelenese, which I've posted on Github. Feel free to fork/clone the repository and give it a try: http://github.com/jpstacey/PySelenese .

青朷 2024-09-05 00:19:52

不,没有办法,但从理论上讲,这应该不会太难,因为您需要做的就是拥有使用 python-rc.js 来转换文件的东西。

No there isn't a way but in theory it shouldn't be too difficult to do as all you need to do is have something that uses the python-rc.js to convert the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文