【问题标题】:How to assign robot framework global variable to another variable in another robotfile如何将机器人框架全局变量分配给另一个机器人文件中的另一个变量
【发布时间】:2022-01-15 22:38:33
【问题描述】:

我已将全局变量指定为 ${googlesite} = http://google.com,我想在变量部分下的另一个机器人文件中使用此变量

robot1.robot

${googlesite} = http://google.com

*** keywords ***

set suite variable ${googlesite}

robot2.robot

*** Variables ***

${googlelogin} = ${googlesite}/login.html


*** Keywords ***
log to console ${googlesite}-- Its printing as http://google.com

log to console ${googlelogin}-- printing only ./login.html

(不从变量部分附加 ${googlesite})

【问题讨论】:

  • 您的代码语法不正确。 = 后面没有足够的空格。此外,robot1.robot 正在定义一个名为 set suite variable ${googlesite} 的关键字,这可能不是您想要的。您确定 this 代码会重现您的问题吗?请提供完整的minimal reproducible example

标签: robotframework


【解决方案1】:

您可以通过使用关键字执行变量赋值来做到这一点。

所以在robot1.robot中,定义一个关键字来设置这个变量:

*** Keywords ***
Setup
    Set Global Variable    ${googlesite}    http://google.com

然后将robot2.robot中的robot1.robot作为资源导入,并在访问变量之前的任意位置执行Setup关键字:

*** Settings ***
Resource          robot1.robot

Suite Setup       Setup

*** Test Cases ***
Test global variable

    # The global variable is available in robot2.robot
    log to console    ${googlesite}
    # Use it to form the new variable
    ${googlelogin} =    Convert To String     ${googlesite}/login.html
    log to console    ${googlelogin}

这将打印:

Test global variable                                                  http://google.com
http://google.com/login.html
| PASS |

【讨论】:

  • 您可以以Suite Setup 的身份执行该步骤,并将适用于该套件中的所有测试。设置变量后,它将可用于所有测试,直到机器人停止。我已使用此解决方案编辑了响应。
  • 这可行,但在变量下寻找选项
【解决方案2】:

鉴于您在关键字部分直接使用日志到控制台并且所有间距都是错误的,因此我根本看不出您提供的代码是如何工作的。

您可以将robot1.robot 导入robot2.robot,robot2.robot 无需将其设置为套件变量即可访问。

robot1.robot:

*** Variables ***
${googlesite}   http://google.com

robot2.robot:

*** Settings ***
Resource  robot1.robot

*** Variables ***
${googlelogin}   ${googlesite}/login.html

*** Test Cases ***
Check the Google variables are correct
    log to console  \nGoogle Site: ${googlesite}
    log to console  Google Login: ${googlelogin}

【讨论】:

    猜你喜欢
    • 2018-01-23
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    相关资源
    最近更新 更多