Makeurchoice
Makeurchoice

Reputation: 7

Importing Python variable into text case in Robot Framework

I'm trying to use an global variable from python but i didn't find a way to use it in my custom keywork

My helper.robot it's look like this:


*** Settings ***

Resource              main.resource
Library               SeleniumLibrary
Variables             Login

*** Variables ***
${email}

*** Keywords ***

Login    
    Input    ${pageLogin.inputUser}        ${email}
    Input    ${pageLogin.inputPassword}    ${Variable.password}
    Click    ${pageLogin.buttonLogin}
    FOR  ${INDEX}  IN RANGE  0  10
        ${progress}  Run Keyword and Ignore Error  Wait Until Element Is Visible    ${pageHome.Logo}  timeout=15
        Exit For Loop If  '${progress[0]}'=='PASS'
    END

My python file it's creating an user using Faker Library and Requests:


from faker import Faker
from datetime import date, timedelta
import json
import requests

fake = Faker()

def create_student():
    get_admin_token()

    global email

    expirationAt = date.today()+timedelta(1)
    expirationAt = expirationAt.strftime("%d/%m/%Y")

    today = date.today()
    today = today.strftime("%d/%m/%Y")

    email = fake.ascii_safe_email()
    
    
    endpoint = "/api/admin/students/"

    payload = {
        "name":email + " " + today,
        "email":email,
        "expirationAt":expirationAt,
        "isAdmin":0,
        "isActive":1
        }

    headers = {
        "authorization": admin_token
    }

    r = requests.request("POST", API_URI + endpoint, data=payload, headers=headers)

    if r.status_code != 201:
        print('Erro: ' + str(r.status_code))
    else:
        print('User created')

I need to use the email stored in the email variable to login in the system.

I tried using the same aproach as the Robot User Guide and some possibles solutions I found in the internet but none of them worked, every time my script fails every time i need to input the email.

Upvotes: 0

Views: 172

Answers (1)

FOXi
FOXi

Reputation: 26

You should create robot test/suite/global variable ${email} that is accessible from robot file.

email = fake.ascii_safe_email()
BuiltIn().set_global_variable("${email}", email)

Upvotes: 1

Related Questions