Get all value from For loop in robot framework - robotframework

Hi I am new in robot framework,
I need to click in some element with xpath.
I get the number of element and stored in a variable ${element}
when I run my code it found ${element}=4
Now I want to click on each element.
So i tried to get all index in a variable like ${i1} untill ${i4}
The result should give me this:
${i1} = 0
${i2} = 1
${i3} = 2
${i4} = 3
i tried this
:FOR ${i} IN RANGE 0 ${element}
\ log ${i}
But it gives result like
20181101 19:21:07.269 : INFO : ${i}: 0
20181101 19:21:08.269 : INFO : ${i}: 1
20181101 19:21:09.269 : INFO : ${i}: 2
20181101 19:21:10.269 : INFO : ${i}: 3
Thank you

In your case you should be use Nest Variable of robotframework . you can find my sample below and its should be solve your current problem.
test
${i} Set Variable i
:FOR ${n} IN RANGE 4
\ ${x} Evaluate ${n}+1
\ Set Test Variable ${${i}${x}} ${n}
log to console ${i3}
and the result is same as you expetcation
${i1} = 0
${i2} = 1
${i3} = 2
${i4} = 3

What you want to do can be achieved by using the keyword Set (Test/Suite/Global) Variable keyword. This takes two parameters and allows you to use variables in variables for creating the name.
*** Test Cases ***
TC
${element} Set Variable ${4}
:FOR ${i} IN RANGE 0 ${element}
\ Set Test Variable ${i${i}} ${i}
This results in the following Message Log in RED.
Starting test: Folder.Forloop I.TC
20181102 07:41:39.010 : INFO : ${element} = 4
20181102 07:41:39.033 : INFO : ${i0} = 0
20181102 07:41:39.044 : INFO : ${i1} = 1
20181102 07:41:39.055 : INFO : ${i2} = 2
20181102 07:41:39.064 : INFO : ${i3} = 3
Ending test: Folder.Forloop I.TC

Related

[robotframework]Why Create a empty element to a list can treat as True, but not applicable to a empty variable

Create a empty element to a list, and return True value with "Should Be True" key word. but not suiteable for create a empty variable.Why is that?
code:
${list1} Create List ${EMPTY}
${list2} Set Variable ${EMPTY}
Should Be True ${list1} Create empty list
Should Be True ${list2} Set empty variable
Log:
20211225 14:24:58.913 : INFO : ${list1} = ['']
20211225 14:24:58.916 : TRACE : Arguments: [ '' ]
20211225 14:24:58.916 : TRACE : Return: ''
20211225 14:24:58.917 : INFO : ${list2} =
20211225 14:24:58.918 : TRACE : Arguments: [ [''] | 'Create empty list' ]
20211225 14:24:58.918 : TRACE : Return: None
20211225 14:24:58.919 : TRACE : Arguments: [ '' | 'Set empty variable' ]
20211225 14:24:58.920 : FAIL : Evaluating expression '' failed: ValueError: Expression cannot be empty.
It is a bit counter-intuitive because ${list1} Create List ${EMPTY} is not creating an empty list, but a list with an empty element inside, e.g. [''], while ${list2} Set Variable ${EMPTY} will create an empty variable, e.g. ''. This is why Should Be True passes on the list (list is not empty) but fails on the variable (variable is empty).
Below code exemplifies these cases:
# empty_list == []
${empty_list} = Create List
log to console ${empty_list}
should be empty ${empty_list}
# empty_list_1 == ['']
${empty_list_1} = Create List ${EMPTY}
log to console ${empty_list_1}
should not be empty ${empty_list_1}
# empty_var == '' (empty string with length 0)
${empty_var} = Set Variable
log to console ${empty_var}
should be empty ${empty_var}
# empty_var_1 == '' (empty string with length 0)
${empty_var_1} = Set Variable ${EMPTY}
log to console ${empty_var_1}
should be empty ${empty_var_1}

Why remove the letter "C" before ${expression} causes error?

I am using Python-RF framework with the example below. I scanned the file
"C" is considered as "". If in the robot file I remove the letter C before the ${expression}, it causes error
Original:
Test Template Calculate
Library CalculatorLibrary.py
*** Test Cases *** Expression Expected
Addition 12 + 2 + 2 16
2 + -3 -1
*** Keywords ***
Calculate
[Arguments] ${expression} ${expected}
Push buttons C${expression}=
Result should be ${expected}
Changed:
*** Test Cases ***
Additions 12 + 2 + 2 16
2 + -3 -1
*** Keywords ***
Calculate
[Arguments] ${expression} ${expected}
Push buttons ${expression}=
Result should be ${expected}
There is an error " 159 != -1". Everyone can share your ideas what wrong it is?
Library file:
https://bitbucket.org/robotframework/robotdemo/src/51f472687b6a46e88b7c179423f0f336e19497fc/CalculatorLibrary.py?at=master&fileviewer=file-view-default
https://bitbucket.org/robotframework/robotdemo/src/51f472687b6a46e88b7c179423f0f336e19497fc/calculator.py?at=master&fileviewer=file-view-default
Without "C":
After your first test case ${expression} is "16". Then you put "2" at the end of it, now ${expression} is "162". Then you substract 3 from it which gives you 159 and this is what you see in the error.
With "C":
Using "C" you set ${expression} to "" (empty string) and then you calculate "2-3" which gives you correct "-1".

Robot framework how to set own name for each test case in data driven tests for report output

Could you please help me how to set own name for each test case into Data Driven to make report more readable.
REAL REPORT EXAMPLE:
Status: FAIL (critical)
Message: Several failures occurred:
1) ******************************
FAIL: Wrong value received. Expected: 0 . Actual: 3
2) ******************************
FAIL: Wrong value received. Expected: 0 . Actual: 3
3) ******************************
FAIL: Wrong value received. Expected: 0 . Actual: 3
But it is not clear from the output about details. And I would like to have some details instead of ***************,
THAT I NEED :
Status: FAIL (critical)
Message: Several failures occurred:
1) if param is empty
FAIL: Wrong value received. Expected: 0 . Actual: 3
2) if param is out of range
FAIL: Wrong value received. Expected: 0 . Actual: 3
3) if param is something more
FAIL: Wrong value received. Expected: 0 . Actual: 3
I have these detail as ${comment} for each table line into data driven. Could you please help me how to assign it for each test case inside data driven to have more understandable report.
DATA DRIVEN TEST EXAMPLE
st_ddt_test_example
[Template] st_ddt_test_example_keyword
# comment # # value setup # # value expected #
if param is empty 0 0
if param is out of range 100 0
if param is something more -8 0
Your keyword controls the error that is displayed, so it just needs to include the name as part of the error message.
Here is an example:
*** Keywords ***
Example
[Arguments] ${comment} ${1} ${2}
should be equal ${1} ${2}
... ${comment}: '${1}' != '${2}'
... False
*** Test Cases ***
Test 1
[Template] example
Test 1.0 a b
Test 1.1 b c
Test 1.2 c d
When run, the test yields the following results:
Test 1 | FAIL |
Several failures occurred:
1) Test 1.0: 'a' != 'b'
2) Test 1.1: 'b' != 'c'
3) Test 1.2: 'c' != 'd'

How to perform pandas drop_duplicates based on index column

I am banging my head against the wall when trying to perform a drop duplicate for time series, base on the value of a datetime index.
My function is the following:
def csv_import_merge_T(f):
dfsT = [pd.read_csv(fp, index_col=[0], parse_dates=[0], dayfirst=True, names=['datetime','temp','rh'], header=0) for fp in files]
dfT = pd.concat(dfsT)
#print dfT.head(); print dfT.index; print dfT.dtypes
dfT.drop_duplicates(subset=index, inplace=True)
dfT.resample('H').bfill()
return dfT
which is called by:
inputcsvT = ['./input_csv/A08_KI_T*.csv']
for csvnameT in inputcsvT:
files = glob.glob(csvnameT)
print ('___'); print (files)
t = csv_import_merge_T(files)
print csvT
I receive the error
NameError: global name 'index' is not defined
what is wrong?
UPDATE:
The issue appear to arise when csv input files (which are to be concatenated) are overlapped.
inputcsvT = ['./input_csv/A08_KI_T*.csv'] gets files
A08_KI_T5
28/05/2015 17:00,22.973,24.021
...
08/10/2015 13:30,24.368,45.974
A08_KI_T6
08/10/2015 14:00,24.779,41.526
...
10/02/2016 17:00,22.326,41.83
and it runs correctly, whereas:
inputcsvT = ['./input_csv/A08_LR_T*.csv'] gathers
A08_LR_T5
28/05/2015 17:00,22.493,25.62
...
08/10/2015 13:30,24.296,44.596
A08_LR_T6
28/05/2015 17:00,22.493,25.62
...
10/02/2016 17:15,21.991,38.45
which leads to an error.
IIUC you can call reset_index and then drop_duplicates and then set_index again:
In [304]:
df = pd.DataFrame(data=np.random.randn(5,3), index=list('aabcd'))
df
Out[304]:
0 1 2
a 0.918546 -0.621496 -0.210479
a -1.154838 -2.282168 -0.060182
b 2.512519 -0.771701 -0.328421
c -0.583990 -0.460282 1.294791
d -1.018002 0.826218 0.110252
In [308]:
df.reset_index().drop_duplicates('index').set_index('index')
Out[308]:
0 1 2
index
a 0.918546 -0.621496 -0.210479
b 2.512519 -0.771701 -0.328421
c -0.583990 -0.460282 1.294791
d -1.018002 0.826218 0.110252
EDIT
Actually there is a simpler method is to call duplicated on the index and invert it:
In [309]:
df[~df.index.duplicated()]
Out[308]:
0 1 2
index
a 0.918546 -0.621496 -0.210479
b 2.512519 -0.771701 -0.328421
c -0.583990 -0.460282 1.294791
d -1.018002 0.826218 0.110252

TCSH/CSH | assign variable with commands result

I wrote this code :
1 #!/bin/tcsh
2
3 set myFiles = `ls`
4 set i = 1;
5 echo "argc is $#argv"
6 while ($i <= $#argv)
7 $myFiles = `echo $myFiles | tr "$argv[$i]" " "`
8 echo "argv now is $argv[$i]"
9 echo "my files are : $myFiles"
10 # i++;
11 end
12 echo "my files post proccess are $myFiles"
13 foreach name ($myFiles)
14 set temp = `cat $name`
15 echo "temp is : $temp"
16 unset temp
17 end
This piece should get a list of file names within the current folder, and print the content of the files that are not specified
IE : folder has the files : A B C D E
and the input is : A B C
so the content of D E will be printed.
now the logic is right, but I have some syntactic issues regarding line 7 (the tr)
I've tried with sed as well, but I get "permission denied" to the console for some reason, and I really don't know how to fix it.
So the help I need is actually syntactic regarding assigning a variable with commands output plus including other variables within those commands.
Hope that's alright..
THANKS !
Please note that tr will replace all matching characters, so if your input includes "A", it will replace all "A" with " " in all file names returned by ls.
There is a much cleaner solution. You want to find all files, exclude files matching the input and print what is left. Here you go:
#!/bin/tcsh
set exclude_names = ""
# if any argument is passed in, add it as "! -name $arg"
foreach arg ( $* )
set exclude_names = "$exclude_names ! -name $arg"
end
# Find all files in the current dir, excluding the input
# then print out the filtered set of files
find . -maxdepth 1 -type f $exclude_names -exec cat {} +

Resources