How to process multi row data in test template - robotframework

Overview
I have a json which feed on multiple rows and fixed columns
Use case : Extract the value of 0222 & 0444 and 0712 & 0786 in each iteration to feed in json
*** Settings ***
Documentation DataDriven Test
Test Template Create CPR Data
*** Variables ***
*** Test Cases *** NPA0 CARRIER0 TEL0
scenario1 684,670,671 0222 123456789
... 622,670,671 0444 123456789 # This data is multirow and this is the requirement
scenario2 633,621,652 0712 123456789
... 626,470,671 0786 123456789
*** Keywords ***
Create CPR Data
[Arguments] ${npa_row1} ${carrier_row1} ${tel_row1}
${ReqBody_PR} set variable {"destNums":[{"NPA":"${carrier_row1}","CARRIER":"${carrier_row2}"}]}
log {ReqBody_PR}
PS : I am not sure if it is possible through Test Template or not, just trying to get some suggestions.

There are multiple solutions, but the most straightforward is to model the keyword based on the data.
You said all scenarios are going to have 2 rows, each with 3 elements ("columns"); in that case just make the keyword take 6 arguments:
*** Test Cases *** NPA1 CARRIER1 TEL1 NPA2 CARRIER2 TEL3
scenario1 684,670,671 0222 123456789
... 622,670,671 0444 123456789 # This data is multirow and this is the requirement
scenario2 633,621,652 0712 123456789
... 626,470,671 0786 123456789
*** Keywords ***
Create CPR Data
[Arguments] ${npa_row1} ${carrier_row1} ${tel_row1} ${npa_row2} ${carrier_row2} ${tel_row3}
${ReqBody_PR} set variable {"destNums":[{"NPA":"${carrier_row1}","CARRIER":"${carrier_row2}"}]} # in your original question, you were already referencing a variable carrier_row2 - but you haven't defined it already; now it works
log {ReqBody_PR}
# you can also access now the 0222 and 0444 from scenario1 directly:
Log tel1: ${tel_row1} tel2: ${tel_row2}
Now, if for - reasons - you want to have a newline inside the value, you could use the \n character; e.g. something like
scenario1 684,670,671 0222 123456789\n622,670,671 0444 123456789
, which will be accepted by your original keyword. But - this is not a table-like data (rows & columns) - the 3rd argument will be a string, that you can split first on the newline - to get the last element of the first row, and then - on the whitespace character, to get the 3 elements of the second row.
Which is much cumbersome and error-prone; I've added the \n solution just for completeness, and your question included it.

Related

Keeping same variable name inside a keyword

Anyone know is it possible for the keyword to keep same variable name as it received?
Following short example will add values to list.
Once it has been updated it needs to be set as a new variable (If i add "${first_list_values}" as test variable after new list insertion, it will only work with one specific list) and the same keyword can't be used anymore.
Id much rather use same keyword to loop multiple lists.
**Keyword**
Change List value
[Arguments] ${received_list}
${new_list} Insert Into List ${received_list} 0 xxx
Set Test Variable ${new_list}
**Test Cases*
Checking new list values
Change List value ${first_list_values}
Maybe something like this:
*** Settings ***
Library Collections
*** Variables ***
#{ORIGINAL_LIST} First Second
*** keywords ***
Change List value
[Arguments] ${received_list}
Append to List ${received_list} Appended Third Appended Fourth
[return] ${received_list}
*** Test cases ***
Use Return
Log To Console ORIGINAL_LIST: #{ORIGINAL_LIST}
#{ORIGINAL_LIST}= Change List value ${ORIGINAL_LIST}
Log To Console ORIGINAL_LIST AFTER CHANGE: #{ORIGINAL_LIST}

How to pass only required variable value and get other values as default in robot framework

Here I want to pass only specific variable value and should get other values as default but I am not getting that...
Ex: I want to pass only word2 variable value and want to get word1 value as default Like "Hello See"
*** Keywords ***
Check order
[Arguments] ${word1}=Hello ${word2}=World
log to console word1 is : ${word1}
log to console word2 is : ${word2}
*** Test Cases ***
Test case 1
Check order ${word2}=See
Test case 1 | FAIL |
Variable '${word2}' not found.
The way to do this is to use only the variable name to the left of the equals:
Check order word2=see
This is from the robot framework user guide, in a section titled Named arguments
When the named argument syntax is used with user keywords, the argument names must be given without the ${} decoration. For example, user keyword with arguments ${arg1}=first, ${arg2}=second must be used like arg2=override.

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

robotframework: what is the easiest way to verify a value is bigger than 10

so there is a website: https://www.guruwatch.nl/Aandelen/Default.aspx
I click the element 'koop' and then I want to verify that the value in the top is bigger then 10
id="ctl00_ctl00_ContentPlaceHolder1_RightContent_ListAandelen_repAandelen_ctl01_lblCountBuy"
what is the fastest way to do this?
i used
Element Text Should Be ctl00_ctl00_ContentPlaceHolder1_RightContent_ListAandelen_repAandelen_ctl01_lblCountBuy 24
but that is for the exact match, i just want to verify the integer is bigger then 10.
there is also an
Should Be Equal As Integers first second
builtin
but not a
Should Be Bigger As Integer
Should be Smaller As Integer
p.s. why are those not builtin? is it so strange to use this?
There are plenty of ways of checking if one value is larger than another with the * If keywords that can be found in the BuiltIn Library. Below is an example of how you can craft the larger than keyword:
*** Test Cases ***
Test Positive
${value} Set Variable 24
Should Be Larger Than ${value} 1
Test Negative
${value} Set Variable 24
Run Keyword And Expect Error * Should Be Larger Than ${value} 100
*** Keywords ***
Should Be Larger Than
[Arguments] ${value_1} ${value_2}
Run Keyword If ${value_1} <= ${value_2}
... Fail The value ${value_1} is not larger than ${value_2}
In order to do this, you need to find the xpath locator first. Since the xpath is quite long so, I just assign it to a variable as below.
${top_position_xpath}= set variable //span[#id='ctl00_ctl00_ContentPlaceHolder1_RightContent_ListAandelen_repAandelen_ctl01_lblCountBuy'
${get_number}= GET TEXT xpath=${top_position_xpath} ## --> This is xpath locator for that top column
${check}= SHOULD BE TRUE ${get_number} > 10 # --> The current test will fail if the result is false..
Okay, so I summarize the steps I use above here:
Get the xpath locator for the specific column that you want to verify.
Use the GET TEXT keyword to get the exact output (number) of that column.
Verify using the SHOULD BE TRUE keyword. This keyword is sufficient to verify the condition.
But just to highlight, that if you use the SHOULD BE TRUE keyword as above, the test will fail immediately, so a good approach is to use with keyword RUN AND RETURN STATUS and assign a variable to tell either the condition is true or false, so that you can proceed with your next code or statements..
${result}= RUN KEYWORD AND RETURN STATUS SHOULD BE TRUE ${get_number} > 10 #
Before I read the messages I solved it myself this way:
Should Match Regexp ctl00_ctl00_ContentPlaceHolder1_RightContent_ListAandelen_repAandelen_ctl01_lblCountBuy [0-9]{1}[0-9]{1}

How do I get the count of items in a list using Robot Framework?

I have created a keyword saying
Select Topic
[Arguments] #{input}
${cnt}= Get length #{input}
Log ${cnt}
And in another Robot Framework file I am calling the keyword with parameters as
select topic 1 2 3 4
So the expected output is 4, but the result is shown as 7 (it is including the spaces).
When you pass something like 1 2 3 4, you are passing a single argument that is the literal string 1 2 3 4. If you want to pass in a list of four or more arguments, you need two or more spaces between each argument:
Also, as a general rule you need to use $ rather than # when trying to refer to an object itself. When you use #, robot will split the list into separate arguments.
Here's a working example. Notice the two spaces between each number when calling the keyword, and the use of $ when calling get length:
*** Test Cases ***
test1
Select Topic 1 2 3 4
*** Keywords ***
Select Topic
[Arguments] #{input}
${cnt}= Get length ${input}
should be equal as numbers ${cnt} 4
The keyword is expecting one argument in list form. Robot Framework running the interpretation will throw an exception for invalid number of arguments. Create a list and pass it to the keyword.
The below solution should work for you:
**** Test Cases ***
test1
${List1} Create List 1 2 3 4
Select Topic ${List1}
*** Keywords ***
Select Topic
[Arguments] #{input}
${cnt}= Get length #{input}
Log ${cnt}*

Resources