RF03 自定义库

一、 注意点

        在rf中,如果使用py文件自定义库、或者使用py文件定义变量,在settings中导入这个自定义库和变量文件时。导入文件的绝对路径不能包含中文下面这种导包方式是错误的:

*** Settings ***
Variables    ../测试数据/testdatas-1.py

二、引用py文件中定义的变量

建立py文件

2.1 py文件内容示例

#coding=utf-8
# 由于rf是python编码的,所以需要定义编码格式

name=u"滚滚"

2.2 测试用例中使用py文件中定义的变量

*** Settings ***
Variables    ../测试数据/testdatas-1.py

*** Test Cases ***
引入py文件变量
    BuiltIn.Log    ${name}    

二、创建测试库类/模块

2.0 自定义测试类/模块注意事项

  1. 绝对路径不能包含中文
  2. 将自定义类库需要集成到测试脚本中,不要直接移植到Scriptd文件夹下,方便脚本移植到不同的测试环境中

2.1 案例

2.1.1 定义一个类的方法定义及使用第三方库

案例结构框架图
自定义第三方库代码:

#coding=utf-8
class ExtLib(object):
    '''
         定义了一个自己的外部库
    '''
    ROBOT_LIBRARY_SCOPE = "GLOBAL"
    ROBOT_LIBRARY_VERSION = 1.0
    
    def __init__(self):
        pass
    
    def keyword(self):
        '''
                    定义了自己的关键字
        '''
        pass

导入第三方库代码:

*** Settings ***
Library    library/ExtLib.py   

自定义库类中注释的作用:

2.1.2 使用模块的方法写一个自定义类库

        参考RF自带库Datetime(C:Python27Libsite-packages obotlibrariesDateTime.py)的方式,将想要定义成关键字的函数使用 all=["函数名称", ...] 来指定哪些函数是你想要被RF识别的。

__all__ = ['convert_time', 'convert_date', 'subtract_date_from_date',
           'subtract_time_from_date', 'subtract_time_from_time',
           'add_time_to_time', 'add_time_to_date', 'get_current_date']

2.1.3 使用__init__.py将自定的的类库打包引用

        参考AppiumLibrary(C:Python27Libsite-packagesAppiumLibrary\__init__.py)和SeleniumLibrary等

参考AppiumLibrary源码结构

# -*- coding: utf-8 -*-

import os
from AppiumLibrary.keywords import *
from AppiumLibrary.version import VERSION

__version__ = VERSION


class AppiumLibrary(
    _LoggingKeywords,
    _RunOnFailureKeywords,
    _ElementKeywords,
    _ScreenshotKeywords,
    _ApplicationManagementKeywords,
    _WaitingKeywords,
    _TouchKeywords,
    _KeyeventKeywords,
    _AndroidUtilsKeywords,
):
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'
    ROBOT_LIBRARY_VERSION = VERSION

    def __init__(self, timeout=5, run_on_failure='Capture Page Screenshot'):
        for base in AppiumLibrary.__bases__:
            base.__init__(self)
        self.set_appium_timeout(timeout)
        self.register_keyword_to_run_on_failure(run_on_failure)

原文地址:https://www.cnblogs.com/gupan/p/9000137.html