Add string value to list - robotframework

My web AUT has about 17 line of text in a table. I have already get each value to a temp variable. And now I want to add that string to the list.
I am getting the following error:
AttributeError: 'str' object has no attribute 'insert'
For example, I have the following text line:
Text Line 1
Text Line 2
Text Line 3
...
And I want to add them to the list like this:
#{mylist} = Text Line 1 | Text Line 2 | Text Line 3
Here is my code, in Robot Framework format:
#{list} Create List ${EMPTY}
${list position} Set Variable 0
${number of row} Get Matching Xpath Count //table[#class="GridView"]//tr
${i} Set Variable 2
: FOR ${i} IN RANGE 2 ${number of row}
${i} Convert To String ${i}
${item control} Replace String ${table profile name default value} rownumber ${i}
${item name} Get Text ${item control}
Append To List #{list} ${item name}

This is the problem line:
Append To List #{list} ${item name}
The problem is the use of #. You need to use $:
Append To List ${list} ${item name}
(you also seem to have the problem that you only have a single space between the last two arguments)
Using $ refers to the list as an object; using # expands the list as if you had typed them into individual cells in the test.

Related

Parse error when text is split on multi lines

i'm getting a parse error when I split a text line on multiple lines and show the JSON file on screen with the command "jq . words.json".
The JSON file with the text value on a single line looks like this
{
"words" : "one two three four five"
}
The command "jq . words.json" works fine and shows the JSON file on screen.
But when i split the value "one two three four five" on two lines and run the same command I get a parse error
{
"words" : "one two
three four five"
^
}
parse error: Invalid string: control characters from U+0000 through
U+001F must be escaped at line 3, column 20
The parse error points to the " at the end of the third line.
How can i solve this?
Tia,
Anthony
That's because the JSON format is invalid. It should look like this:
{
"words" : "one two \nthree four five"
}
You have to escape end of line in JSON:
{
"words" : "one two\nthree four five"
}
To convert the text with the multi-line string to valid JSON, you could use any-json (https://www.npmjs.com/package/any-json), and pipe that into jq:
$ any-json --input-format=cson split-string.txt
{
"words": "one two three four five"
}
$ any-json --input-format=cson split-string.txt | jq length
1
For more on handling almost-JSON texts, see the jq FAQ: https://github.com/stedolan/jq/wiki/FAQ#processing-not-quite-valid-json
The parse error points to the " at the end of the third line.
The way jq flags this error may be counterintuitive, but the error in the JSON precedes the indicated quote-mark.
If the error is non-obvious, it may be that an end-quote is missing on the prior key or value. In this case, the value that matches the criteria U+0000 through U+001F could be U+000A, which is the line feed character in ASCII.
In the case of this question, the line feed was inserted intentionally. But, unescaped, this is invalid JSON.
In case it helps somebody, I had this error:
E: parse error: Invalid string: control characters from U+0000 through
U+001F must be escaped at line 3, column 5
jq was parsing the file containing this data, with missing " after "someKey
{
"someKey: {
"someData": "someValue"
}
}

multiple condition check in for loop using robot framework

I am trying to check multiple conditions in for loop using robot framework but it never returns true.
:FOR ${RowIndex} IN RANGE 0 ${rowscount}
${ColumnText1} Get Text //*[#id='RadSearchGrid_ctl00__${RowIndex}']/td[3]
${ColumnText2} Get Text //*[#id='RadSearchGrid_ctl00__${RowIndex}']/td[4]
${ColumnText3} Get Text //*[#id='RadSearchGrid_ctl00__${RowIndex}']/td[5]
${bStatus} | Run Keywords | Should Contain | ${ColumnText1} and ${ColumnText2} and ${ColumnText3} | ${VoucherNumber} and ${Voucherdate} and ${VoucherAmount}
Exit For Loop If ${bStatus}
${bStatus} Never returns true.
Try something like this
:FOR ${RowIndex} IN RANGE 0 ${rowscount}
${ColumnText1} Get Text //*[#id='RadSearchGrid_ctl00__${RowIndex}']/td[3]
${ColumnText2} Get Text //*[#id='RadSearchGrid_ctl00__${RowIndex}']/td[4]
${ColumnText3} Get Text //*[#id='RadSearchGrid_ctl00__${RowIndex}']/td[5]
${bStatus}= Run Keyword And Return Status Run Keywords Should Contain ${ColumnText1} ${VoucherNumber} AND Should Contain ${ColumnText2} ${Voucherdate} AND Should Contain ${ColumnText3} ${VoucherAmount}
Exit For Loop If ${bStatus}

Display empty line for non existing fields with jq

I have the following json data:
{"jsonrpc":"2.0","result":[],"id":1}
{"jsonrpc":"2.0","result":[{"hostmacroid":"2392","hostid":"10953","macro":"{$GATEWAY}","value":"10.25.230.1"}],"id":1}
{"jsonrpc":"2.0","result":[{"hostmacroid":"1893","hostid":"12093","macro":"{$GATEWAY}","value":"10.38.118.1"}],"id":1}
{"jsonrpc":"2.0","result":[{"hostmacroid":"2400","hostid":"14471","macro":"{$GATEWAY}","value":"10.25.230.1"}],"id":1}
{"jsonrpc":"2.0","result":[{"hostmacroid":"799","hostid":"10798","macro":"{$GATEWAY}","value":"10.36.136.1"}],"id":1}
{"jsonrpc":"2.0","result":[],"id":1}
{"jsonrpc":"2.0","result":[{"hostmacroid":"1433","hostid":"10857","macro":"{$GATEWAY}","value":"10.38.24.129"}],"id":1}
{"jsonrpc":"2.0","result":[{"hostmacroid":"842","hostid":"13159","macro":"{$GATEWAY}","value":"10.38.113.1"}],"id":1}
{"jsonrpc":"2.0","result":[],"id":1}
I am trying to extract the value of the "value" field from each line. jq -r '.result[].value' <jsonfile> works perfectly but it does not take into account the JSON lines where there is no "value" field. I would like it to print an empty line for them. Is this possible with jq?
You can use this:
jq -r '.result[].value // "" ' a.json
This uses the or operator //. If .result[].value is present, the value will get printed, otherwise an empty line gets printed.
This would work:
jq -r '.result | if length > 0 then .[0].value else "" end'
Since false // X and null // X produce X, .result[].value // "" may not be what you want in all cases.
To achieve the stated goal as I understand it, you could use the following filter:
.result[] | if has("value") then .value else "" end

Printing a column to the console and having it display string values

Considering the following code:
cbind(data$Preceding.Phone, data$word, word.position)
I want to print data$Preceding.Phone, data$word, and word.position to the console because I just created a new column (word.position) and I want to see if it matches up with the other columns. The other columns contain string entries like "alabama" or "magazine". When I print this to the console they all come out as numbers. How can I print them as their string values?

Extract texts from a single file and writing those texts to multiple files

I'm trying to achieve the following.
Find first '(' symbol (always located at the start of a new line) in a text file and make the hostname that follows a variable such as '(host' becomes 'host'.
Copy text between first instance of '=-=-=' delimiter and second instance of the same delimiter ('=-=-=') to a new file whose name is the variable in item 1 such as host-messages.txt
Copy text between second instance of '=-=-=' delimiter and third instance of the same delimiter to a new file whose name is the variable in item 1 such as host-df.txt
Copy text between third instance of '=-=-=' delimiter and subsequent next instance of a '(' on the first character of a new line such as the next instance of a '(hostname',to a new file whose name is the variable in item such as host-dfa.txt
Repeat steps 1 through 4 until end of file and no more '(host' are found.
Anyway what I'm trying to achieve is extract data such as:
(hostname1 : 056603)  1
=-=-=
TEXT
TEXT
TEXT
=-=-=
TEXT2
TEXT2
TEXT2
=-=-=
TEXT3
TEXT3
TEXT3
(hostname2
=-=-=
etc...
Newly created files should be hostname1-messages.txt, hostname1-df.txt and hostname1-dfa.txt.
The best I've come up with so far is
awk '/=-=-=/{x="F"++i;next}{print > x;}' test.txt
but it's not working.
Post some sample input and expected output to help us help you but something like this is what you want (untested, obviously):
awk '
BEGIN{ sfx[1]="-messages.txt"; sfx[2]="-df.txt"; sfx[3]="-dfa.txt" }
sub(/^\(/,"") { host=$0; nr=0; next }
/=-=-=/ { nr++; next }
{ print > host sfx[nr] }
' file

Resources