madhur
madhur

Reputation: 173

how to use variable from one robot file into another robot file in Robot Framework

Here is example add.robot file

*** Settings ***
Library   SudsLibrary
Library   XML
Library   String

*** Test Cases ***
Test Webservice

    create Soap Client    http://www.dneonline.com/calculator.asmx?wsdl

    ${add}    Create Wsdl Object   Add
    ${add.intA}    Set Variable    3
    ${add.intB}    Set Variable    4

    # # call soap web service
    call soap method  Add  ${Add}
    ${soap_response}    Get Last Received
    log to console  ${soap_response}
    ${XML_object}=  Parse XML  ${soap_response}
    log    ${XML_object}
    ${result}  get element text  ${XML_object}  .//AddResult
    set global variable  ${result}

substract.robot file

trying to use result variable into this file but its showing error. Both file exist in same folder

*** Settings ***
Library   SudsLibrary
Library   XML
Library   String


*** Test Cases ***
Test Webservice
    create Soap Client    http://www.dneonline.com/calculator.asmx?wsdl

    ${subs}    Create Wsdl Object   Subtract
    ${subs.intA}    Set Variable   ${result} #shows error
    ${subs.intB}    Set Variable    4

    # # call soap web service
    call soap method  Subtract  ${subs}
    ${soap_response}    Get Last Received
    log to console  ${soap_response}
    ${XML_object}=  Parse XML  ${soap_response}
    log    ${XML_object}
    ${result11}=  get element text  ${XML_object}  .//SubtractResult

Upvotes: 1

Views: 5939

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385970

It's a best practice to make each file independent, so I would encourage you to rewrite your tests so that they aren't dependent on each other.

Global Robot Variables

That being said, if you want to set a variable in one file and be able to use it in another, you need to use the built-in keyword Set global variable.

From the documentation:

Variables set with this keyword are globally available in all test cases and suites executed after setting them. Setting variables with this keyword thus has the same effect as creating from the command line using the options --variable or --variablefile. Because this keyword can change variables everywhere, it should be used with care.

Your question mentions var1 but your code doesn't have var1. If you meant to write ${node} (which appeared in the original version of your question), you would do it like this:

set global variable  ${node}

Global library scope

If you're not asking about a robot variable, but rather how to have your library keep hold of a variable between suites, you need to set the ROBOT_LIBRARY_SCOPE variable in your library to "GLOBAL". For example:

class token1:
    ROBOT_LIBRARY_SCOPE = "GLOBAL"
    ...

For more information see Test library scope

Upvotes: 3

P S
P S

Reputation: 158

You can try this. Value form Test 1.robot is used in Test 2.robot

Test 1.robot

*** keywords ***
Test1 Webservice
   set global variable  ${node}

Test 2.robot

*** Settings ***
Resource Test 1.robot

*** Test Cases ***
Test2 Webservice
  Test 1.Test1Webservice
  ${node1} set Variable ${node} 

Upvotes: 2

Related Questions