RobotFramework测试框架(13)--扩展RF

扩展RF

可以写Python库

Static Library

静态库中RF的关键字被定义为python的方法。

Static Library With a Class

将Python类导入为Library,则类中的方法可以是关键字。

python 复制代码
class DemoLibrary:
    def __init__(self, *args, **kwargs):
        print(f"Sample Library initialized with args: {args} and kwargs: {kwargs}")
 
    def my_keyword(self, *args, **kwargs):
        print(f"Keyword got args: {args} and kwargs: {kwargs}")
        return "Hello World"
python 复制代码
*** Settings ***
Library    DemoLibrary.py

*** Test Cases ***
Use a Keyword with multiple arguments
    My Keyword    Argument 1    Argument 2    Named Argument=One Value

Static Library withouth a Class

将关键字定义在python方法中

将py文件导入为Library,则文件中的方法可以是关键字

python 复制代码
import base64

def encode_as_base64(string):
    """
    Encode string as base64.
    """
    return base64.b64encode(string.encode())

def decode_from_base64(string):
    """
    Decode string from base64.
    """
    return base64.b64decode(string).decode()
python 复制代码
*** Settings ***
Library    LibraryWithoutClass.py

*** Test Cases ***
Use Custom Keywords
    ${base64}    Encode As Base64    This is a Test String
    Log    ${base64}
    ${decoded}    Decode From Base64    ${base64}
    Log    ${decoded}

Decorators

可以使用装饰@@keyword和@not_keyword将方法装饰为关键字。

python 复制代码
from robot.api.deco import keyword, not_keyword


@keyword('Login via user panel')
def login(username, password):
      # ...

@not_keyword
def this_is_not_keyword():
    pass
python 复制代码
from robot.api.deco import keyword


@keyword(tags=['tag1', 'tag2'])
def login(username, password):
    # ...

@keyword('Custom name', ['tags', 'here'])
def another_example():
    # ...

@keyword(types={'count': int, 'case_insensitive': bool})
def example_keyword(count, case_insensitive=True):
    if case_insensitive:
        # ...

@keyword(types=[int, bool])
def example_keyword(count, case_insensitive=True):
    if case_insensitive:
        # ...
相关推荐
明月与玄武7 个月前
RF 框架实现企业级 UI 自动化测试
robotframework
好的!文西8 个月前
Mac robotframework+pycharm运行suite报错情况:ImportError: No module named request
ide·macos·pycharm·测试·robotframework
北极之熊熊10 个月前
05 robotFrameWork+selenium2library 一维数组的使用
自动化·robotframework
Mr. G K1 年前
RobotFramework 自动化测试实战基础篇
robotframework
搬砖,攒路费1 年前
RobotFramework流程控制(最新版本)
自动化测试·robotframework