Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable Declaration and Initialization in Robot Framework within a Test Cases block

I tried to Declare and Initialize a variable in Robot Framework using Selenium platform. But I'm getting an Error Keyword name cannot be empty.

I tried the following code

Integer:

*** Test Cases ***
Test Case 1
    ${item}       ${0}  # ${}

Boolean:

*** Test Cases ***
Test Case 2
    ${item}    ${true}    #${}

String:

*** Test Cases ***
Test Case 3
    ${item}    Stackoverflow

Kindly assist me how to declare and initialize a variable within a Test Cases block in Robot Framework.

Reply for @Goralight

I'm getting an error

enter image description here

like image 409
B.Balamanigandan Avatar asked Jun 26 '17 08:06

B.Balamanigandan


2 Answers

You need to use the Set Variable Keyword to assign values to Variables outside the Variable Header:

*** Test Cases ***
Test Case 1
    ${item}    Set Variable    ${0}    #${}

    ${item}    Set Variable    ${true}    #${}

    ${item}    Set Variable    Stackoverflow

The above assigns the variable you have given in your test cases to the correct value. (This will overwrite ${item} every time of course however) But this will assign the value, to the var ${item}.

Read the Docs about it here

Any questions please ask :)

like image 124
Goralight Avatar answered Sep 25 '22 08:09

Goralight


In my opinion, the following is a more readable way to do it:

*** Test Cases ***
Test Case 1

    ${item} =    Set Variable    ${0}            #${}

    ${item} =    Set Variable    ${true}         #${}

    ${item} =    Set Variable    Stackoverflow

You will get an error if you do the following:

    ${item} =   Stackoverflow

The reason is that this assignment is expecting a keyword Stackoverflow on right hand side.

Here is a working example of such assignment.

*** Test Cases ***
Test Case 1
    ${item} =    Get My Value
    Log          ${item}

*** Keywords ***
Get My Value
    ${my text} =    Set Variable    Stackoverflow
    [return]        ${my text}
like image 20
Atiq Avatar answered Sep 26 '22 08:09

Atiq