Looping Reference

Hi guys want to ask 2 things (will be grateful if someone can help to give reference or example)

So I have to case

  1. I already did query to db and get result I desired. Next I want to save it in csv with looping.
    the result should be
    a, b, c
    a, b, c

but my result is
a,b,c,a,b,c

  1. related to first question, I want to hit some api with request body that consist of 3 field
    a, b, c

but I want only c that get value from csv I save earlier.

If there is two result in query means will hit api 2 time
1st request a, b, c[0]
2nd request a,b,c[1]

Thanks in advance guys

Hi Fadhli,

It might help if you were a bit clearer about what a, b, & c are?

You mentioned that you “already did query to db and get result” did you use DatabaseLibrary Query for that? if so are a, b, & c your columns?

Assuming the answer is yes to all the above questions then rather than results being a single list as you described your result should be a list of lists i.e. [[a,b,c],[a,b,c]] not [a,b,c,a,b,c]

with a list of lists like [[a,b,c],[a,b,c]] it should be easy to do what you want, it should look something like this:

*** Settings ***
Library         OperatingSystem

*** Test Cases ***

DB Result 2 CSV
	# This line just simulates the result you should have gotten from you datanase query
	@{queryres}= 	Evaluate 	[["a1","b1","c1"],["a2","b2","c2"]]
	# Define the csv file name
	${csvfile}= 	Set Variable     ${CURDIR}/myfile.csv
	# for loop
	FOR 	${row}	IN	@{queryres}
		# Use python join to to conver a list to a delimited string
		# https://www.w3schools.com/python/ref_string_join.asp
		${srow}= 	Evaluate 	",".join(${row})
		# append the new line of delimited text to the files (don't forget to add a new line => \n)
		Append To File 	${csvfile} 	${srow}\n
	END

Dave.

Hi Dave thanks for answerin, the first problem already solved tho but thanks for give me another insight.

Coming to 2nd question if I can get more detail is the body request I will use is quite complex, and to generate that in robot I start with the deepest { }

So let’s say my code look like this

${a} create dictionary var_a var_b var_c

${a_list} create list {a}

${b} create dictionary var_a var_b var_c

${b_list} create list {b} {a_list}

Keep doing that until it become the body request I want to use and saved into variable below

${payload}

The problem is kinda like inside the {b} is dynamic, I have n variable that I got from csv file while the other on the payload is hard coded. So its kinda confusing where do I need to loop it. Is it in the api request part or looping in creating body request part

And then just like usual creating session to send the request with body payload

But the problem it’s like

But the problem its like in {b} there is value from csv I want to input and make request until all value from csv is used. But the other is hardcoded, so its kinda confusing how I can do that and where is I need to loop. In the create session part or since the creating body request

Hi Fadhli,

I’m not really sure what you were trying to acheive with that code?

I put it in a robot file and added “Keep doing that”

*** Test Cases ***
data example
	${a}  create dictionary  var_a  var_b  var_c
	Log 	${a}
	${a_list} 	create list 	${a}
	Log 	${a_list}
	${b} 	create dictionary 	var_a 	var_b 	var_c
	Log 	${b}
	${b_list} 	create list 	${b} 	${a_list}
	Log 	${b_list}
	FOR 	${i} 	IN RANGE 	3
		${b} 	create dictionary 	var_a 	var_b 	var_c
		Log 	${b}
		${b_list} 	create list 	${b} 	${b_list}
		Log 	${b_list}
	END

The problem is it fails on the first line because the dictionary keys have no values.

I then had a guess at what you might have wanted?:

*** Settings ***
Library 	Collections

*** Test Cases ***
data example da
	&{dict}  Create Dictionary  var_a=aValue  var_b=bValue  var_c=cValue
	Log 	${dict}
	@{mylist} 	Create List 	${dict}
	Log 	${mylist}
	FOR 	${i} 	IN RANGE 	3
		Append To List 	${mylist} 	${dict}
		Log 	${mylist}
	END

As I mentioned I’m not sure what your trying to acheive, so hopefully I’m not leading you in the wrong direction.

Dave.

Hi Dave,

For more clearer information I will put glimpse of my code here

${user} Create Dictionary user_name=${user_name} user_id=${user_id}

${instrument}  Create Dictionary  id=${loan_id}  name=${i_name_borrower} 

${instrument_list}  Create List  ${instrument}

${borrower}  Create Dictionary  id=${user_id}  name=${name_borrower} 

${borrower_list}  Create List  ${borrower}

${funder}  Create Dictionary  name=${f_name}  id=${f_id}  bank_name=${f_bank_name}  

${value}  Create Dictionary  payment_id=${p_id}  total_payment=${p_total}  total_payment_nett=${p_total_n}  cart_type=NON_REVOLVING  funder=${funder}  borrower=${borrower_list}  description=Disbursement Loan  user=${user}

#valueInfo Sec
${v_Info}  Create Dictionary  objectTypeName=${objTypeName}  serializationDataFormat=${serialDF}

#bulkPayment Sec
${bulk_payment}    Create Dictionary  valueInfo=${v_Info}  type=Object  value=${value}

#variable Sec
${variable}    Create Dictionary    bulk_payment=${bulk_payment}

#timestamp func
${timestamp}=    Get Time

#payload Sec
${payload}    Create Dictionary    businessKey=${timestamp}    variable=${variable}

so basically, I need to get instrument and borrower value from csv file earlier for n times, and how do I make my api request is send everytime until the file from csv is done.

what confusing me is, how I make it loop to change value in instrument and borrower only while in the end the value request will be send is in form of

${response}= POST On Session token url=/message_templates/insert json=${payload}

Regards,
Fadhli

Hi Fadhli,

Ah OK, I initially thought you were trying to write the test data to a file now I see you want to read the test data from a file.

Before I continue I’ll first ask if you want each line as a separate test, or the whole file as a single test?

  1. If you want each line as a separate test, DataDriver might be the best option for you. If this is what you’re after I suggest you watch the video and play with the examples in the documentation to understand how DataDriver works as the documentation is quite good.

  2. If you want the whole file as a single test, then Get File and Split To Lines can be used to acheive that but I should warn you that if the data from 1 line fails then the remaining lines won’t get tested, so I think the DataDriver option is better. If you want to go this way let me know and I’ll suggest the best way to structure your test.

Dave.

Hi Dave,

actually this is will not be used as usual test. but to handle some kind heavy data execution, since there is like 500-2000 data we can’t use manual mode like you know put the value in body request 1 by 1 for every data and then hit the api in postman.

the problem is only data in instrument and borrower section that will change for every request, that’s why I kinda confuse about it. since it will be better to understand if the complete body request is dynamic and not partially like this case.

any example you can give with my case, not exactly same but maybe have the similar purpose or ideas?

Regards,

Fadhli

Hi Fadhli,

That’s more the reason to use DataDriver, It will handle reading the file and keeping the instrument and borrower aligned for you, I know it’s ~30 min but I suggest you watch the video, as that tool is designed for when you have >500 sets of data

What you’ll probably want is the Data Sources mode, something like:

*** Settings ***
Library         DataDriver    file=../data/my_data_source.csv
Test Template     The Keyword

your csv file will probably look something like:

loan_id,i_name_borrower,user_id,name_borrower
2300123,Muhammad,B012,Aisha
2300124,Ibrahim,B022,Abdullah
2300125,Bilal,B021,Omar
2300125,Fatima,B021,Omar

Move all the steps from your test case above to a keyword with the same name and set that as the Test Template in the settings section.

When you run robot, DataDriver will automatically create a new test case for every line in the csv file, the variables with be the same as the column headings from the first line of the csv file

I’d also suggest you start with a small csv file with only 2-5 lines make sure it all works how you want then switch to the big csv file with 2000 lines.

Dave.

Hi Dave,

Thanks for the suggestion. will watch the video first.

Regards,
Fadhli

1 Like

Hi Dave,

[Arguments]    ${row}
    ${instrument}    Create Dictionary    id=${row.id}    name=${row.name}    amount=${row.amount}    status=${row.status}    bid_amount=${row.bid_amount}    cart_detail_id=${row.cart_detail_id}    tenor=${row.tenor}

Is it correct to create it like that? and I want to ask. If the only variable that will get from csv is instrument, while in the end the one that used is {payload} which the instrument is part of it. will it send the api automatically until all value from csv is used?

regards,

Fadhli

Hi Fadhli,

Yes you can do that, as for if it’s correct, I don’t know enough about your app to answer that.

The values from the will be available as variables, It’s up to you which of those variables you use to construct the payload in your test case, you can use all of them, some or none, depending on what you tell it to use.

Will robot refuse to send the payload if you didn’t use all the variables? - No

Dave.

Hi Dave,

just to confirm so basically all variable like ${user}, ${instrument}, etc will be end up in ${payload} but I only make ddt like above on ${instrument} and ${borrower} right since the dynamic data will be there.

and I only send payload in post session api. and it automatically run request until value in csv that contained in instrument and borrower is done.

Regards,

Fadhli

Hi Fadhli,

I built a simple example for you, you can use this as a template, build your payload and send your request to the server inside the Data Driver Example keyword.

But before you do that, just recreate these two file, run robot dd_example.robot and confirm you get the same result I did:

dd_example.robot

*** Settings ***
Library         DataDriver    file=dd_example.csv 	dialect=unix

Test Template     Data Driver Example

*** Test Cases ***
Dummy Test ${loan_id} ${i_name_borrower} ${user_id} ${name_borrower}

*** Keywords ***
Data Driver Example
	[Arguments]		${loan_id} 	${i_name_borrower} 	${user_id} 	${name_borrower}
	Log 	${loan_id}
	Log 	${i_name_borrower}
	Log 	${user_id}
	Log 	${name_borrower}

dd_example.csv

${loan_id},${i_name_borrower},${user_id},${name_borrower}
2300123,Muhammad,B012,Aisha
2300124,Ibrahim,B022,Abdullah
2300125,Bilal,B021,Omar
2300125,Fatima,B021,Omar

I highlighted the variable names in Orange and the corresponding values in Green in the screen shot below of test report for the files above.

Hopefully this clears it up for you,

Dave.

Hi Dave,

Thankyou very much with the guidance. I will update here once I’ve tried it.

Regards,

Fadhli

1 Like