Search code examples
pythonselenium-webdriverrandomrobotframeworktextfield

Different random strings of numbers for text fields in Robot Framework using a variable


I'm testing a web application using Robot Framework/Selenium. I need to input numerical values to several text fields on a page, and I'm wondering if there's a way I can use a variable to get different random values for each of the fields.

I'm already using this keyword as suite setup:

Random Values
    ${RANDOM_VALUE} =    Generate Random String    3    [NUMBERS]100-200
    Set Global Variable    ${RANDOM_VALUE}

And then these in the test case:

Input Text    ${TEXT_FIELD_1}    ${RANDOM_VALUE}
Input Text    ${TEXT_FIELD_2}    ${RANDOM_VALUE}
Input Text    ${TEXT_FIELD_3}    ${RANDOM_VALUE}

and it works, but it gives me the same string for each text field I call the ${RANDOM_VALUE} variable to. Is there some way to generate a different random string for every text field on the page by using the same variable for all of them?

I'm still a total newbie in the world of automated testing, and I'm not at all well-versed in Python... I have a feeling that the solution involves using Python in some way, but I have no idea how to actually do that. I've tried searching for the answer or tutorials or something to help me, but I haven't been able to figure it out. Maybe the answer is just so simple that nobody else is having the same problem?

Thank you!


Solution

  • Robot Framework assigns values to variables, not function/keyword calls. You should adjust your keyword to return a list with the three random numbers.

    Here is an example:

    *** Settings ***
    Library           Collections
    Library           String
    
    *** Test Cases ***
    Test Random Numbers List
        ${RANDOM_VALUE} =    Generate Random Numbers List    3    3    [NUMBERS]100-200
        Log    "Input Text\ \ \${TEXT_FIELD_1}\ \ ${RANDOM_VALUE[0]}"
        Log    "Input Text\ \ \${TEXT_FIELD_2}\ \ ${RANDOM_VALUE[1]}"
        Log    "Input Text\ \ \${TEXT_FIELD_3}\ \ ${RANDOM_VALUE[2]}"
    
    *** Keywords ***
    Generate Random Numbers List
        [Arguments]    ${list_size}=3    @{args}
        @{RANDOM_VALUE}=    Create List
        FOR    ${x}    IN RANGE    ${list_size}
            ${num}=    Generate Random String    @{args}
            Append To List    ${RANDOM_VALUE}    ${num}
        END
        RETURN    ${RANDOM_VALUE}