Rakesh
Rakesh

Reputation: 1575

Global variable value is not reflected across functions

I want to calculate the cumulative time which gets updated across different functions.

Test Case file

*** Settings ***
Suite Setup         Pre_Execution_Steps
Suite Teardown      Post_Execution_Steps
Library             DateTime
Library             Extended Selenium2Library

*** Test Cases ***
Test Case 1
    Function A

Resource File

*** Settings ***
Library             DateTime
Library             ExtendedSelenium2Library

*** Keywords *** 
Pre_Execution_Steps
     Set Global Variable    ${WAIT_TIME}    00:00:00.000

Function A:
     ${WAIT_TIME}=    Add Time To Time    ${WAIT_TIME}    00:00:02.001    result_format=timer           
     Log ${WAIT_TIME}
     ${WAIT_TIME}=    Add Time To Time    ${WAIT_TIME}    00:00:02.001    result_format=timer
     Log ${WAIT_TIME}

Post_Execution_Steps
     Log    Wait time is ${WAIT_TIME}

When i execute the test case, global variable ${WAIT_TIME} value gets updated inside the Function A. But ${WAIT_TIME} value is not reflected in Post_Execution_Steps though it is global variable. It prints the default value 00:00:00.000

Any inputs/suggestions would be helpful

Upvotes: 0

Views: 64

Answers (2)

Krzysztof Waberski
Krzysztof Waberski

Reputation: 1

I had similar problem. Additional declaration may help. Your code should look like below.

*** Settings ***

Library DateTime

Library ExtendedSelenium2Library

*** Keywords ***

Pre_Execution_Steps

 Set Global Variable    ${WAIT_TIME}    00:00:00.000

Function A:

 ${WAIT_TIME}=    Add Time To Time    ${WAIT_TIME}    00:00:02.001    result_format=timer           

 Log ${WAIT_TIME}

 ${WAIT_TIME}=    Add Time To Time    ${WAIT_TIME}    00:00:02.001    result_format=timer

 Log ${WAIT_TIME}

 Set Global Variable    ${WAIT_TIME}

Post_Execution_Steps

 Log    Wait time is ${WAIT_TIME}

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 386255

I think that within Function A:, ${WAIT_TIME} is treated as local once it is set to a new value. To see a change to the global variable you need to call Set global variable inside Function A:.

Upvotes: 2

Related Questions