Define variables as variables in Robot Framework with Python file - python

This works
<<file1.robot>>
**Setting**
Resource file2.robot
**Variables**
${file1_var1} ${file2_var1}
**Keyword**
Check It
Click ${file1_var1}
where
<<file2.robot>>
**Settings**
Variables file2Locator.py
<<file2Locator.py>>
file2_var1 = "id=Clickable"
Click id=Clickable is called successfully, with keyword "Click It"
However, the following FAILS:
<<file1Fail.robot>>
**Setting**
Resource file2.robot
Variables file1Fail.py firstSet
**Keyword**
Check It
Click ${file1_var1}
Where
<<file1Fail.py>>
SetOneVar = {'file1_var1': "${file2_var1}"}
def get_variables(arg):
if arg == 'firstSet': return SetOneVar
else return
due to the error where UiSelector[DESCRIPTION=${file2_var1}] clearly does not work.
In other words, upon getting variables from a python file 1, Click ${file2_var1} is literally called, instead of Click id=Clickable. "${file2_var1}" is not being replaced by "id=Clickable" anymore.
Any solution to this? How to call define a variable as another variable imported at another resource file

In the python file the value of the variable you've defined (file1_var1) is set to the string ${file2_var1} (which looks like a RF variable definition due to the $ and {}, but it's just a string). When the framework imports it as a variable file, it does not automatically substitute it - "you set the value to be a string, you're getting a string".
Later on, after it's imported you can substitute/replace it with the value behind ${file2_var1} - as long as that file2_var1 is defined in the current scope. That's done by calling Replace Variables which returns the substituted version (e.g. doesn't modify the original variable):
${file2_var1}= Replace Variables ${file2_var1}

Related

How do I return a value extracted by a "combined keyword" in GoTest?

I created a combined keyword in GoTest to run a bit of script to extract the index of a component on a web page. I want to use that as a locator in my Selenium test.
GoTest is a wrapper UI around RobotFramework.
I tried to create a global variable and put the value in that global variable, but it doesn't seem to work. The variable is empty when I try to use it.
You can place the result calculated by your combined keyword script into a variable and then put that variable in the return field of the combined keyword.
Example of the result:

Python tkinter app requires a variable that doesn't exist at the moment

At the moment, I am trying to program a python application that tracks my consumed time on different projects. I also use the following modules: Sqlite3 and tkinter.
APP-GUI
On the right side in the image you can see the list of my current example projects I would like to track. The issue I have right now is that I have to choose a project in the list in order to pass the variable to the fetch_times function that requests the corresponding times from the database. However, I need kind of a default variable that is activated before I get the chance to select a project in the list.
The code below defines at first the function to select a project in my list on the right side in the image. The other function is a loop that should print the requested data in the list on the left side.
def selected_project(event):
global selected_project
index = project_list.curselection()[0]
selected_project = project_list.get(index)
def show_times(selected_project):
for row in db.fetch_times(selected_project):
times_list.insert(END, row)
The following code is part of a second python file that contains database functions.
def fetch_times(self, selected_project):
self.cur.execute("SELECT date_day, start_time, stop_time FROM projects WHERE project_name=?",(selected_project,))
rows = self.cur.fetchall()
return rows
I already tried to generate a default selected_project variable but then I got the error message that the "function is already defined". I also thought about to implement an IF-statement that checks if the variable exists but I am just not sure what the best solution is. This is my first post here so I hope this overview describes my problem well enough.
Thank you for you help!
I got the error message that the "function is already defined"
As Cool Cloud said:
You have a function named selected_project and a variable named selected_project too. Change your function name or variable name and it will be fixed.You declared selected_project as a None but after that, you declared selected_project as a function. so selected_project is a function and it's not a value anymore, and you are trying to change your function name inside your function and it's impossible.

Python use input to select local variable inside another function

All, I have this request but first I will explain what I'm trying to achieve. I coded a python script with many global variables but also many methods defined inside different modules (.py files).
The script sometimes moves to a method and inside this method I call another method defined in another module. The script is quite complex.
Most of my code is inside Try/Except so that every time an exception is triggered my code runs a method called "check_issue()" in which I print to console the traceback and then I ask myself if there's any variable's value I want to double check. Now, I read many stackoverflow useful pages in which users show how to use/select globals(), locals() and eval() to see current global variables and local variables.
What I would specifically need though is the ability to input inside method "check_issue()" the name of a variable that may be defined not as global and not inside the method check_issue() either.
Using classes is not a solution since I would need to change hundreds of lines of code.
These are the links I already read:
Viewing all defined variables
Calling variable defined inside one function from another function
How to get value of variable entered from user input?
This is a sample code that doesn't work:
a = 4
b = "apple"
def func_a():
c = "orange"
...
check_issue()
def check_issue():
print("Something went wrong")
var_to_review = input("Input name of var you want to review")
# I need to be able to enter "c" and print the its value "orange"
print(func_a.locals()[var_to_review ]) # this doesn't work
Could somebody suggest how to fix it?
Many thanks
When you call locals() inside check_issue(), you can only access to the locals of this function, which would be : ['var_to_review'].
You can add a parameter to the check_issue function and pass locals whenever you call it.
a = 4
b = "apple"
def func_a():
c = "orange"
check_issue(locals())
def check_issue(local_vars):
print("Something went wrong")
var_to_review = input("Input name of var you want to review")
print(local_vars[var_to_review])

Variables viewer on Eclipse debugging evaluates Python property automatically

When I new some instance, its __ init __ will do, e.g.
self._regex = value
self._regex_dict = {}
In the Eclipse/PyDev debugger's variables view,self._regex_dict is not equal to empty but has one entry instead.
It seems variables view generatesself.regexautomatically, and unfortunatelly, the following method is defined and will output one entry toself._regex_dict
#property
def regex(self):
...
self._regex_dict[language_code] = compiled_regex
return self._regex_dict[language_code]
Is there any configuration to prevent such auto-generation behavior of Eclipse/PyDev debugger's variables view?
P.S. If I turn off the Eclipse/PyDev debugger's variables view, this issue won't happen. But I really need it when debugging
Thanks,
Well, when the debugger hits a breakpoint and you have the variables view visible, it'll do a dir() on all the objects in the context and for each value it'll do a str() to show them.
As you have a property (regex) which will add an entry to your dict 'behind the scenes' when accessed and as the variables view will definitely try to show that value to you, I'm not sure you have a way around it other than changing your program or changing the debugger itself (it'll get the vars at pydevd_vars.frameVarsToXML).

Python range() builtin function, erm... malfunctioning with Django

What, if anything, is wrong with this line of python code:
daterange = [begin + timedelta(n) for n in range((end - begin).days)]
Where begin and end are datetime.date objects with valid values.
I'm using this in a Django view to process some data, but everytime the view this is in gets called I get the following error with the aforementioned line highlighted:
UnboundLocalError at /url/of/error/creating/view/here/
local variable 'range' referenced before assignment
If I execute this line inside the interpreter it works fine, but somehow it doesn't fly inside a Django view. I don't understand why range is being interpreted as a variable name at all. Is there actually something wrong with this line, or is it something else in the code that's making Django complain?
Help!
There's nothing wrong with Django. You create a local variable range in the same scope (by assigning one). For instance range = None in the very last line of a function makes Python consider an occurrence of range in the first line of the same function a reference to that local variable. Since it doesn't have a value assigned at that point, you get an UnboundLocalError.

Categories

Resources