RF之变量的共享使用与python测试库-5

RF申明变量:

       首先我们要创建Variables表

       *** Settings ***

       ${LoginUrl}      http://cloud.innovpowerf.com/Account/Login

       ${StudentLoginUrl}      http://localhost/student/login

       @{database}        127.0.0.1     3306           ----------------List变量

       &{user}             name=username    pw=password   ------------------Dict变量

使用变量文件:

       可以使用python模块文件提供公共变量给RF使用,只需要直接定义变量就可以了。语法完全就是python语法。如:

       StudentLoginUrl = 'http://localhsot/studfent/login'

        RF申明使用变量文件:

        *** Setings ***

        Variables    ../cfg/cfg.py

       *注意* :变量文件申明时,可以使用绝对路径,也可以使用相对路径。

                    使用相对路径时RF搜索规则和资源文件搜索规则一样:

                            - 先在相对当前文件的目录  匹配搜索;

                            - 在python的模块搜索路径中搜索,可以用  --python参数   如:robot --python .tc/t1.robot

                    命令行参数指定变量文件:robot --variablefile cfg\cfg.py  tc/t1.robot

                    也可以设置全局变量然后执行(写成全局的配置文件):

                                  set pythonpath=.

                                  robot tc/t1.robot

python扩展关键字:

       python模块作为关键字:模块文件名作为测试库的名字

                                        比如 python模块叫MyLibrary,对应的python文件就是MyLibrary.py,那么测试库的名字就是MyLibrary

                                        定义在python模块文件中的函数,名称前有_前缀的不会作为关键字

       def  returnList1():

                return[1, 2]

       def  _returnList2():

                return[1, 2]

       要保证其在python模块的搜索路径中,这样RF才能找到它。

          * 直接设置环境变量

          * 用 --python参数    如:robot --pythonpath lesson1 lesson1/tmp.robot

          如果在lesson里面的某个目录中执行呢?   如  robot --pythonpath ..  ../tmp2.robot

       RF使用关键字的时候: - 中间可以加上任意的空格   

                                     - 大小写也可以任意

       自己定义关键字的时候:如下

       from robot.api.deco import keyword   # 装饰器

       @keyword

       def returnList():

              return[1, 2]

python类作为测试库:

      * 比如:python文件是tlib2.py

      class SubLibrary:   

              def returnInt(self):

                     return 3

              def _returnInt(self)

          return  4

   * 申明

      *** Settings ***

      Library       tlib2.SubLibrary

      * 该类中的成员方法,名称前有_前缀的不会作为关键字

      * 导入时的参数,对应类的初始化方法

      * 如果类和模块文件同名,申明的时候就可以省略后面的类名

python 扩展库的搜索规则:

       完全是按照python的模块的搜索规则来的

          - 如果在包内,pylib/login/rightpass.py

            *** Settings ***

            Library       pylib.login.rightpass

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

            Library      pylib/login/rightpass.py

          - 在Settings中    申明资源文件和变量文件:

            路径、目录之间的分隔符,不用点  .    而是用斜杠 / 

          - 在Settings申明测试库:

            路径、目录之间的分隔符,可以用点  也可以用斜杠  /  

            路径分隔符    用点后面不加py,用斜杠后面加.py

猜你喜欢

转载自www.cnblogs.com/peipei-Study/p/12082585.html