通过python编码生成RobotFramework类

  • 在此记录下之前开发web版ride的过程中,遇到了python执行robot脚本的问题。

    直接创建TestSuit类

  • 通过python脚本生成Robot Framework 里面的TestSuite,并执行以下逻辑:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    from robot.api import TestSuite
    ts = TestSuite('test')
    ts.resource.imports.library('Selenium2Library')
    ts.resource.imports.resource('d:/temp/flows.robot')
    tc = ts.tests.create('case')
    tc.keywords.create('Open Browser', args=[u'url=http://www.baidu.com', u'browser=gc'])
    tc.keywords.create('Go to', args=[u'url=http://tieba.baidu.com'])
    tc.keywords.create(u'\u5546\u54c1\u8be6\u60c5\u9875\u968f\u673a\u9009\u62e9\u5546\u54c1', args=[])
    ts.run()

robot文件转TestSuite

  • 也可以从robot 文件生产TestSuite
    1
    2
    3
    from robot.api import TestSuiteBuilder
    suite = TestSuiteBuilder().build('path/to/activate_skynet.robot')

执行结果逻辑

  • run方法可以返回结果,也可以读取一些执行的用例,用例状态等结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    result = suite.run(critical='smoke', output='skynet.xml')
    assert result.return_code == 0
    assert result.suite.name == 'Activate Skynet'
    test = result.suite.tests[0]
    assert test.name == 'Should Activate Skynet'
    assert test.passed and test.critical
    stats = result.suite.statistics
    assert stats.critical.total == 1 and stats.critical.failed == 0
  • 当然这样结果里面的内容还是有限的,特别是没有关键字执行的message,那么就需要导入另外一个类ExecutionResult

    1
    2
    3
    4
    5
    6
    from robot.result import ExecutionResult
    result = ExecutionResult(outputxml)
    for case in result.suite.tests:
    for kw in case.keywords:
    print kw.name, kw.messages, kw.passed, kw.starttime)

生成HTML报告

1
2
3
4
5
6
from robot.api import ResultWriter
# Report and xUnit files can be generated based on the result object.
ResultWriter(result).write_results(report='skynet.html', log=None)
# Generating log files requires processing the earlier generated output XML.
ResultWriter('skynet.xml').write_results()