Mark
Mark

Reputation: 323

RobotFramework - Calling a function from python library

I'm trying to generate a random name when logging in, through a function in Python. The problem is that it doesn't consider it as a function, but as a plain text, though i've imported the library and called it explicitly. Here's the Python function in file Generators.py:

import random

letters = [chr(ord('a')+i) for i in range(26)]

def generate_name():
    name = ""
    for i in range(9):
        name = name + random.choice(letters)
    return "Testing_Mark[" + name + "]"

And here's the Robot file:

*** Settings ***
Library     Generators.py
Library     Selenium2Library

*** Variables ***
${User}                     Generators.Generate Name
${Password}                 1234
*** Test Cases ***
Organisation Test
    Open Browser    http://mywebsite.com/login     googlechrome
    Log In
    [Teardown]  Close Browser

*** Keywords ***
Log In
    Wait Until Page Contains    Log in
    Input Text          //input[@name="name"]        ${User}
    Input Password      //input[@name="password"]     ${Password}
    Click Button        //button[@type="submit"]
    Wait Until Element Is Visible   //span[@class="alert-description"]
    Sleep   3

What am I doing wrong? I know Robot should recognize the above function as a keyword, but still can't figure out why it is not. Both files are in the same folder.

Upvotes: 2

Views: 3224

Answers (1)

Psytho
Psytho

Reputation: 3384

Variable table only assigns values as is, keywords are not executed in variables table.

Quotation from Robot Framework User Guide:

Their (variables table) main disadvantages are that values are always strings and they cannot be created dynamically. If either of these is a problem, variable files can be used instead.

So your ${User} is now Generators.Generate Name. As string. Move

${User}                     Generators.Generate Name

into the test case.

*** Test Cases ***

Organisation Test
    ${User}                     Generators.Generate Name
    Open Browser    http://mywebsite.com/login     googlechrome
    Log In
    [Teardown]  Close Browser

Upvotes: 2

Related Questions