CSV file creation - robotframework - robotframework

I want to create csv file which contains:
Header1,Header2,Header3
Value1,Value2,Value3
Value11,Value22,Value33
I also want to dynamically generate the values and append to the file; How can this be achieved in robotframework?

You could do this with a number of helper keywords, although it's not clear exactly what you need but you can create a number of header values dynamically by appending to a list based on a specified number of columns as well as two other keywords where the first creates a list of random values based on the header count and another that appends that list of values to a list which would effectively be your row. You can have that also in a for loop which calls the keyword to generate the row values until reaching the specified number of rows.
You can probably do without the csv library if you just convert the list of headers/rows to a comma seperated string and then use standard Append to File keyword to append this string.
So in summary, you could define keywords like the following:
Generate Header
Generate Set of Random Rows
Generate Random Row of Values
Convert List to Comma Seperated String
Generate CSV File
You will also need:
The test case itself which calls some of the keywords (Generate Header, Generate Set of Random Rows and Generate CSV File)
In Variable section a defined header count and row count
Libraries (String, Collections and OperatingSystem)
*** Settings ***
Library String
Library Collections
Library OperatingSystem
*** Variables ***
${header_count} 5 # Change this for a different number of headers
${row_count} 20 # Change this for a different number of rows
*** Test Cases ***
Scenario: Create CSV With Random Data
${header} Generate Header ${header_count}
${data} Generate Set of Random Rows ${header_count} ${row_count}
Generate CSV File ${header} ${data}
*** Keywords ***
Generate Header
[Documentation] Generate a number of headers, stored in a list with prefix "Header" and Suffixed with the current index of the iteration
[Arguments] ${header_count}
#{header_list} Create List
FOR ${header_num} IN RANGE 1 ${header_count}+1
Append To List ${header_list} Header${header_num}
END
[Return] ${header_list}
Generate Set of Random Rows
[Documentation] Takes the header count and row count which is used to create a list of lists containing randomly generated strings
[Arguments] ${header_count} ${row_count}
#{random_row_list} Create List
FOR ${row} IN RANGE 0 ${row_count}
${values} Generate Random Row Of Values ${header_count}
Append To List ${random_row_list} ${values}
END
[Return] ${random_row_list}
Generate Random Row Of Values
[Documentation] Takes header count to derive the amount of random strings to append to a list
[Arguments] ${header_count}
#{random_value_list} Create List
FOR ${column} IN RANGE 0 ${header_count}
${value} Generate Random String
Append To List ${random_value_list} ${value}
END
[Return] ${random_value_list}
Convert List To Comma Seperated String
[Documentation] Converts list values to string so they can be appended to csv file
[Arguments] ${values}
${converted_values} Evaluate ", ".join(${values})
[Return] ${converted_values}
Generate CSV File
[Documentation] Takes header (list of column headers) and data (List containing nested list of row values)
[Arguments] ${header} ${data}
Create File test.csv
${header_string} Convert List To Comma Seperated String ${header}
# Create the header
Append To File test.csv ${header_string}
# Append all the values row by row
FOR ${row} IN #{data}
${row_values} Convert List To Comma Seperated String ${row}
Append To File test.csv \n${row_values}
END

Related

How to count columns of an output csv file using robot script

I am trying to get the column count from an output csv file using Robot script.
I tried the following lines. Count of rows is working fine. But the 'Get Column Count' is throwing error :
No keyword with name 'Get Column Count' found.
${file-content}= Get File ${OUTPUT_CSV_FILE}
${numberOfLine=}= Get Line Count ${file-content}
Should Be Equal As Integers ${numberOfLine=} X
${numberOfColumn=}= Get Column Count ${file-content}
Should Be Equal As Integers ${columnCount=} Y
I haven't defined any keywords for 'Get Line Count'..but its working.
Then why not Get Column Count. Is it necessary to write a keyword in robot?
Thanks in advance
Aneesh
I haven't defined any keywords for 'Get Line Count'..but its working. Then why not Get Column Count. Is it necessary to write a keyword in robot?
The reason why "Get line count" is working is that you have likely imported robot's String library which has a keyword named Get Line Count. It's not counting rows in a csv file, it's counting lines of text by splitting the string on newlines.
Robot doesn't itself have a keyword for counting columns in a csv file. For that you'll need to use a library that specifically imports the data as csv and has a keyword for counting columns.

How to generate random string only on certain time through excel file?

#I need to generate random test strings for pass case and for fail case I need to give manual vale through excel file
#I have tried:
#Give input to new bill cycle
[Arguments] ${CYCLE_NAME}
${ret}= Generate Random String 12
input text ${XPATH_TO_CYCLE_NAME_TEXTBOX} ${CYCLE_NAME}
#and when I give ${ret} on my excel file it says variable not found error.
#thank all for you support i got the answer
*** Settings ***
Suite Setup Initialize Random Variables
*** Keywords ***
Initialize Random Variables
${RANDOM_STRING}= Generate random string 15
Set global variable ${RANDOM_CYCLE_NAME}
#Then i was able to call ${RANDOM_STRING} through my excel when ever i want random string

How to count any column's cells count in excel with robotframework

this is the excel:
enter image description here
I want to get the first column's cell count, I use excellibrary, but the Get Row Count method doesn't suite my case, it get the max row in excel,any solution?
my code as below(i use ride):
Open Excel | D:\\try.xls
${a} | Get Column Count | Sheet1
the result is 4, but i want to get 3
The excellibrary uses internally a python lib for working with Excel files - xlrd; the keyword Get Column Count is actually returning the property ncols of a Sheet object in it, the documentation of which says:
Nominal number of columns in sheet. It is one more than the maximum column index found, ignoring trailing empty cells.
So it will always return one more than the actual used - just subtract the extra one.

How to sort a list and return the value in Robotframework

I have a use case for which I have to automate the following steps:
Create an empty list
Push data into the empty list
Keep/save the original order in a variable
Sort the original order
Save the sorted list
Reverse the sorted list and return value
*** Settings ***
Library SeleniumLibrary
Library Collections
*** Keywords ***
Sort order verification
#{username_list}= Create List //creates an empty list
#{get_name}= Get WebElements css=#userTable > tbody > tr> td:nth-child(1)
:FOR ${each} IN #{get_name}
\ ${get_username}= Get Text ${each}
\ Append To List ${username_list} ${get_username} //pushes data into list in iteration
${original_order}= Copy list ${username_list} //returns original order
${sorted_list}= Sort List ${original_order} //sorts the list but returns none(nothing is saved in the variable
${reverse_sorted_list}= Reverse List ${sorted_list} //returns AttributeError: 'NoneType' object has no attribute 'reverse'
The Sort List and Reverse List keywords modify the list in-place, e.g. they change the value of the target variable.
They also don't return anything - thus on the lines you've used them, you have assigned the value None to the variables, which led to the error.
You can read about this behavior in the Collections library documentation

Robot framework, generic function to give name to a list

I'm trying to create a keyword that will export a list as a suite variable, but I can't figure out how to pass a name to turn into a variable name.
*** Test Cases ***
Get Ref
${list} = Create List k l m n e
Rename List myName ${list}
log #{myName}
*** Keywords ***
Rename List
[Arguments] ${name} ${values}
log first: ${values[1]}
#{name}= Create List ${values[1]} ${values[3]}
set suite variable #{name}
The keyword takes a string and a list, creates a smaller list and exports it with the name string provided.
As a use-case, you want a generic function that can take values from a dropdown list on a webpage and return you just item 1, 3, and 5 as a new list with the name you provide. This way you could call it multiple times with different names, exporting different lists that you could use later.
Is there any way to make this work?
This seems to do what you want, if I understand the question correctly:
*** keywords ***
rename list
[Arguments] ${name} ${values}
${new}= create list #{values}
set suite variable ${${name}} ${new}

Resources