如何在机器人框架中声明全局变量并在另一个机器人文件中使用它



在回复中我得到一个访问令牌,我存储在${token}变量,并希望将其设置为全局变量,并希望在SendFax中使用它。机器人文件

**GenerateToken.robot**
*** Variables ***
${base_Url}=    https://pssidpdev01.modmedclouddev.com:5001
*** Keywords ***
*** Test Cases ***
Generator Token with valid credentials
    ${body}=    create dictionary    grant_type=client_credentials   client_id=OrgClient3    client_secret=2M7A$Lbw567#WJdEixE&qFc#k
    ${headers}=  create dictionary   Content-Type=application/x-www-form-urlencoded
    create session  mysession   ${base_Url}     disable_warnings=1
    ${response}=    POST On Session    mysession   /connect/token  data=${body}    headers=${headers}
    log to console  ${response.json()} 
 ${Token}=    Collections.Get From Dictionary    ${response.json()}    access_token
 Set Global Variable     ${Token}
   
**SendFax.robot**
*** Settings ***
Resource    GenerateToken.robot 
*** Variables ***
*** Test Cases ***
Send Fax Request
    
    log to console  ${Token} //Want to print above token here

与其将令牌的生成作为测试,不如考虑将其作为关键字在套件设置

上运行像这样:

GenerateToken.resource

*** Variables ***
${base_Url}=    https://pssidpdev01.modmedclouddev.com:5001

*** Keywords ***
Generator Token with valid credentials
    
    ${is_token}   Run Keyword And Return Status   Variable Should Exist  ${Token}
    # Only generate a token if ${TOKEN} var doesn't exist
    IF  ${is_token}
        Return From Keyword
    ELSE
        ${body}=    create dictionary    grant_type=client_credentials   client_id=OrgClient3    client_secret=2M7A$Lbw567#WJdEixE&qFc#k
        ${headers}=  create dictionary   Content-Type=application/x-www-form-urlencoded
        create session  mysession   ${base_Url}     disable_warnings=1
        ${response}=    POST On Session    mysession   /connect/token  data=${body}    headers=${headers}
        log to console  ${response.json()} 
        ${Token}=    Collections.Get From Dictionary    ${response.json()}    access_token
        Set Global Variable     ${Token}
    END

SendFax.robot

*** Settings ***
Resource    GenerateToken.resource 
Suite Setup   Generator Token with valid credentials

*** Test Cases ***
Send Fax Request
    log to console  ${Token} //Want to print above token here

最新更新