Python完成RF测试用例

Python完成RF测试用例

Robot Framework 框架是基于 Python 语言开发的,所以,它本质上是 Python 的一个库。

?

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

from robot.api import TestSuite

from robot.api import ResultWriter

 

#百度搜索测试

class BaiduSearchTest:

    def __init__(self, name, libraries=["SeleniumLibrary"]):

        # 创建测试套件

        self.suite = TestSuite(name)

         

        # 导入SeleniumLibrary

        for lib in libraries:

            self.suite.resource.imports.library(lib)

     

    # 定义变量

    def create_variables(self):

        variables = {

            "${baidu}" : "http://www.baidu.com",

            "${browser}" : "Chrome",

            "${search_input}" : "id=kw",

            "${search_btn}" : "id=su",

        }

        for k, v in variables.items():

            self.suite.resource.variables.create(k, v)

     

    # 测试用例: 启动浏览器

    def open_browsers(self):       

        test_01 = self.suite.tests.create("启动浏览器")

        test_01.keywords.create("Open Browser",

            args=["${baidu}", "${browser}"])

        test_01.keywords.create("Title Should Be",

            args=["百度一下,你就知道"])  

 

    # 测试用例:百度搜索测试

    def search_word(self):

        test_02 = self.suite.tests.create("百度搜索测试")

        test_02.keywords.create("Input Text",           

            args=["${search_input}", "测试教程网"])

        test_02.keywords.create("Click Button",                 

            args=["${search_btn}"])

        test_02.keywords.create("Sleep", args=["5s"])   

 

    # 测试用例:断言验证搜索结果标题

    def assert_title(self):

        test_03 = self.suite.tests.create("断言验证搜索结果标题")

        test_03.keywords.create("Title Should Be",     

            args=["测试教程网_百度搜索"])   

 

    # 测试用例:关闭测试用例

    def close_browsers(self):

        test_04 = self.suite.tests.create("关闭浏览器")

        test_04.keywords.create("Close All Browsers")   

 

    # 运行

    def run(self):

        self.create_variables()

        self.open_browsers()

        self.search_word()

        self.assert_title()

        self.close_browsers()       

 

        # 运行套件

        result = self.suite.run(critical="百度搜索",

            output="output.xml")       

 

        # 生成日志、报告文件

        ResultWriter(result).write_results(

           report="report.html", log="log.html")

 

if __name__ == "__main__":

    print("用Python写Robot Framework测试")

    suite = BaiduSearchTest("百度搜索测试套件")

    suite.run()

  

运行结果:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

用Python写Robot Framework测试

==============================================================================

百度搜索测试套件                                                             

==============================================================================

启动浏览器                                                            | PASS |

------------------------------------------------------------------------------

百度搜索测试                                                          | PASS |

------------------------------------------------------------------------------

断言验证搜索结果标题                                                  | PASS |

------------------------------------------------------------------------------

关闭浏览器                                                            | PASS |

------------------------------------------------------------------------------

百度搜索测试套件                                                      | PASS |

0 critical tests, 0 passed, 0 failed

4 tests total, 4 passed, 0 failed

==============================================================================

Output:  C:\WorkSpace\SimpleTest\selenium_learn\output.xml

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值