创建用户关键字

实现在测试库。 的语法创建用户 关键词非常接近的语法创建测试用例 很容易学习。

用户关键字语法

基本语法

。 用户创建关键字关键字表 这不同于测试用例表,用于只有名字 识别它们。 用户在第一列类似关键字名称 测试用例的名称。 也从关键字,创建用户的关键词 从关键词在测试库或其他用户的关键词。 关键字的名字 通常在第二列,但当设置变量的 关键字返回值,它们在随后的列。

*** Keywords ***
Open Login Page
    Open Browser    http://host/login.html
    Title Should Be    Login Page

Title Should Start With
    [Arguments]    ${expected}
    ${title} =    Get Title
    Should Start With    ${title}    ${expected}

。 关键字中创建资源 文件可用于文件使用它们,而其他关键字 只能在他们创建的文件。

设置关键字表中

,他们 有相同的方括号语法分离关键字 的名字。 下面列出了所有可用的设置和稍后解释 这一节。

(文档)
(标签)
关键字。
(参数)
(返回)
(拆卸)
(超时)
讨论了 在自己的部分。

用户关键字名称和文档

中定义的用户关键字的名字是第一列的用户 关键字表。 当然,这个名字应该是描述性的,它是 接受有很长的关键字的名字。 实际上,当创建 通常use-case-like测试用例,最高级别的关键词 制定句子甚至段落。

部分。

用户关键词标签

前缀和由逗号分隔。 例如, 后两个关键词都得到相同的三个标签。

*** Keywords ***
Settings tags using separate setting
    [Tags]    my    fine    tags
    No Operation

Settings tags using documentation
    [Documentation]    I have documentation. And my documentation has tags.
    ...                Tags: my, fine, tags
    No Operation

命令行选项也支持选择关键词 标签,和新用法的关键词标签可能被添加在后面的版本。

除非真正激活前缀 特殊的功能。

用户关键字参数

位置参数

最简单的方法来指定参数(除了没有) 仅使用位置参数。 在大多数情况下,这就是一切了 这是必要的。

*** Keywords ***
One Argument
    [Arguments]    ${arg_name}
    Log    Got argument ${arg_name}

Three Arguments
    [Arguments]    ${arg1}    ${arg2}    ${arg3}
    Log    1st argument: ${arg1}
    Log    2nd argument: ${arg2}
    Log    3rd argument: ${arg3}

默认值与用户关键词

的部分或全部参数。 同时用户关键字 支持默认值,需要新的语法不会增加很多 已经讨论了基本的语法。

,但局部变量的关键字 遗嘱执行人不能使用。 从机器人Framework 3.0,默认值 早些时候还定义基于参数关键字所接受。

请注意

不允许,和可能的空间 后被认为是默认值本身的一部分。

*** Keywords ***
One Argument With Default Value
    [Arguments]    ${arg}=default value
    [Documentation]    This keyword takes 0-1 arguments
    Log    Got argument ${arg}

Two Arguments With Defaults
    [Arguments]    ${arg1}=default 1    ${arg2}=${VARIABLE}
    [Documentation]    This keyword takes 0-2 arguments
    Log    1st argument ${arg1}
    Log    2nd argument ${arg2}

One Required And One With Default
    [Arguments]    ${required}    ${optional}=default
    [Documentation]    This keyword takes 1-2 arguments
    Log    Required: ${required}
    Log    Optional: ${optional}

 Default Based On Earlier Argument
    [Arguments]    ${a}    ${b}=${a}    ${c}=${a} and ${b}
    Should Be Equal    ${a}    ${b}
    Should Be Equal    ${c}    ${a} and ${b}

仍将得到其默认值。

*** Test Cases ***
Example
    Two Arguments With Defaults    arg2=new value

正如所有的python支持者必须已经注意到的,语法 指定默认参数严重受Python语法 函数的默认值。

Varargs和用户关键字

在关键字可能位置参数后签名。 这个语法可以结合前面描述的默认值,和 最后列表变量得到所有剩下的参数不匹配 其他参数。 列表变量可以有任意数量的物品,甚至是零。

*** Keywords ***
Any Number Of Arguments
    [Arguments]    @{varargs}
    Log Many    @{varargs}

One Or More Arguments
    [Arguments]    ${required}    @{rest}
    Log Many    ${required}    @{rest}

Required, Default, Varargs
    [Arguments]    ${req}    ${opt}=42    @{others}
    Log    Required: ${req}
    Log    Optional: ${opt}
    Log    Others:
    : FOR    ${item}    IN    @{others}
    \    Log    ${item}

。 这种组合的两个,而先进的功能 有时是非常有用的。

再次,python支持者可能注意到变量的数量 参数语法非常接近一个Python。

Kwargs与用户关键字

不匹配任何位置参数的关键字 签名。

*** Keywords ***
Kwargs Only
    [Arguments]    &{kwargs}
    Log    ${kwargs}
    Log Many    @{kwargs}

Positional And Kwargs
    [Arguments]    ${required}    &{extra}
    Log Many    ${required}    @{extra}

Run Program
    [Arguments]    @{varargs}    &{kwargs}
    Run Process    program.py    @{varargs}    &{kwargs}

对于一个完整的示例相同的关键字。

将参数嵌入到关键字的名字

机器人框架也另一种方法向用户传递参数 关键字比指定关键字名称后细胞内 在前一节中解释。 这种方法是基于嵌入 参数直接进入关键字名称和它的主要好处是 使其更容易使用真实和清晰的句子作为关键字。

基本语法

*** Keywords ***
Select ${animal} from list
    Open Page    Pet Selection
    Select Item From List    animal_list    ${animal}

。 显然它不是 强制使用这些参数在关键字,他们可以 因此作为通配符。

嵌入参数不支持默认值或变量的数量 参数正常参数。 使用变量时 把这些关键词是可能的,但可以减少可读性。 只注意到嵌入式参数处理用户的关键词。

嵌入参数匹配的太多

在定义变量时, 在下一节中解释。 最后,如果事情变得复杂, 这可能是一个更好的主意转而使用普通位置参数。

,解决这个问题 很容易。

使用自定义正则表达式

这两个定义匹配 关键词。

*** Test Cases ***
Example
    I execute "ls"
    I execute "ls" with "-lh"

*** Keywords ***
I execute "${cmd}"
    Run Process    ${cmd}    shell=True

I execute "${cmd}" with "${opts}"
    Run Process    ${cmd} ${opts}    shell=True

解决这个问题是使用自定义正则表达式 确保关键字匹配只应该在那 特定的上下文。 能够使用该功能,并完全 理解本节中的示例,您需要理解 最基本的正则表达式语法。

。 使用自定义 下面的例子说明了正则表达式。

*** Test Cases ***
Example
    I execute "ls"
    I execute "ls" with "-lh"
    I type 1 + 2
    I type 53 - 11
    Today is 2011-06-27

*** Keywords ***
I execute "${cmd:[^"]+}"
    Run Process    ${cmd}    shell=True

I execute "${cmd}" with "${opts}"
    Run Process    ${cmd} ${opts}    shell=True

I type ${a:\d+} ${operator:[+-]} ${b:\d+}
    Calculate    ${a}    ${operator}    ${b}

Today is ${date:\d{4\}-\d{2\}-\d{2\}}
    Log    ${date}

变体。

提示

直到第一个保证的参数匹配 关闭报价。

支持正则表达式语法

转义特殊字符

),但是,这取决于下一个 性格,还两个反斜杠可能就足够了。

不应该逃跑。

使用自定义的变量嵌入参数正则表达式

每当定制的嵌入式参数使用正则表达式时,机器人 框架会自动提高指定的regexp,这样他们 匹配变量除了与模式相匹配的文本。 这 意味着它总是可以用关键字使用变量 嵌入参数。 例如,下面的测试用例能通过 从早期的例子使用关键词。

*** Variables ***
${DATE}    2011-06-27

*** Test Cases ***
Example
    I type ${1} + ${2}
    Today is ${DATE}

仍将匹配相同的关键字。

行为驱动开发的例子

*** Test Cases ***
Add two numbers
    Given I have Calculator open
    When I add 2 and 40
    Then result should be 42

Add negative numbers
    Given I have Calculator open
    When I add 1 and -2
    Then result should be -1

*** Keywords ***
I have ${program} open
    Start Program    ${program}

I add ${number 1} and ${number 2}
    Input Number    ${number 1}
    Push Button     +
    Input Number    ${number 2}
    Push Button     =

Result should be ${expected}
    ${result} =    Get Result
    Should Be Equal    ${result}    ${expected}

请注意

用户关键字返回值

在测试用例和其他用户的关键词。

设置

设置,这是 完成在接下来的细胞通过返回值后设置。

设置。

*** Test Cases ***
One Return Value
    ${ret} =    Return One Value    argument
    Some Keyword    ${ret}

Multiple Values
    ${a}    ${b}    ${c} =    Return Three Values
    @{list} =    Return Three Values
    ${scalar}    @{rest} =    Return Three Values

*** Keywords ***
Return One Value
    [Arguments]    ${arg}
    Do Something    ${arg}
    ${value} =    Get Some Value
    [Return]    ${value}

Return Three Values
    [Return]    foo    bar    zap

使用特殊的关键字返回

设置上面所讨论的。

*** Test Cases ***
One Return Value
    ${ret} =    Return One Value  argument
    Some Keyword    ${ret}

Advanced
    @{list} =    Create List    foo    baz
    ${index} =    Find Index    baz    @{list}
    Should Be Equal    ${index}    ${1}
    ${index} =    Find Index    non existing    @{list}
    Should Be Equal    ${index}    ${-1}

*** Keywords ***
Return One Value
    [Arguments]    ${arg}
    Do Something    ${arg}
    ${value} =    Get Some Value
    Return From Keyword    ${value}
    Fail    This is not executed

Find Index
    [Arguments]    ${element}    @{items}
    ${index} =    Set Variable    ${0}
    :FOR    ${item}    IN    @{items}
    \    Return From Keyword If    '${item}' == '${element}'    ${index}
    \    ${index} =    Set Variable    ${index + 1}
    Return From Keyword    ${-1}    # Could also use [Return]

请注意

因为机器人框架2.8。

用户关键字拆卸

设置。

。 最重要的是,拆卸总是一个 关键字,尽管它可以被另一个用户的关键字,就会执行 也当用户关键字失败。 此外,所有的步骤 拆卸执行即使其中一个失败。 然而,失败 关键字拆卸将会失败的测试用例和后续步骤 测试不运行。 的名称关键字执行拆卸 也可以是变量。

*** Keywords ***
With Teardown
    Do Something
    [Teardown]    Log    keyword teardown

Using variables
    [Documentation]    Teardown given as variable
    Do Something
    [Teardown]    ${TEARDOWN}

相关文章:

  • 2021-08-18
  • 2021-09-06
  • 2021-06-24
  • 2021-09-05
  • 2021-07-04
  • 2022-02-13
  • 2021-06-19
  • 2021-10-19
猜你喜欢
  • 2022-01-29
  • 2022-02-21
  • 2022-02-25
  • 2021-07-02
  • 2021-11-28
  • 2021-09-10
  • 2022-02-04
相关资源
相似解决方案