Hi everyone!
I’m learning RF and struggling with something…
I created some dictionaries and added them to a list, then I tried to iterate over it using a FOR loop, but the result is: “Dictionary does not contain key ‘…’”.
However, before the FOR loop, I tested the dictionary using the “keyword Dictionary Should Contain Key” — it passes the check.
I don’t know what I’m doing wrong.
Here’s my code:
Iterar sobre lista de dicionários para exibir seus valores
Log Dictionary dictionary=${TotalDebitos}
${TotalDebitos}[valorEncontrado]= Set Variable 1000,00
${DebitosProcessados}[valorEncontrado]= Set Variable 700,00
${DebitosPendentes}[valorEncontrado]= Set Variable 300,00
Log Dictionary dictionary=${TotalDebitos}
Dictionary Should Contain Key dictionary=${TotalDebitos} key=valorEncontrado
Dictionary Should Contain Item dictionary=${TotalDebitos} key=valorEncontrado value=1000,00
FOR ${dicionario} IN @{tabelaResultado}
${Campo}= Get From Dictionary ${dicionario} campo
${Locator}= Get From Dictionary ${dicionario} locator
${valor}= Get From Dictionary ${dicionario} valorEncontrado
Log Campo: ${campo}
Log Locator: ${locator}
Log Valor encontrado: ${valor}
END
In the log the first red ITERATION line reveals the dictionary content and that the requested key is not in the dictionary. So the dictionary content is as it was when it was added into the list.
It seems that later modifications into the original dictionary (after adding it to a list) are not affecting the dictionary in the list.
Hi, Hélio! I ran the keyword Log Many ${tabelaResultado[0], like you said, and the output shows that the dictionary does not contain the key I’m looking for. I assume I’m making a mistake when adding the key–value pair, but I don’t know at which point. I’ve already tried initializing the key without a value in the dictionary and then assigning the value later. I also tried adding it using Set To Dictionary dictionary=${TotalDebitos} valorEncontrado=R$ 1.000,00 D, but the result was the same.
Thanks for the help!
Hi, Sami! After your message, I tried again without using the list. I just created the dictionary and tried adding the key–value pair, and then it worked. However, I would like to keep my original idea of having several dictionaries in a list so I can iterate over it and then perform the checks on each dictionary’s values.
Could my logic be wrong? Is this not possible?
You can update the values straight to the list of dictionaries, something like this:
# ${TotalDebitos}[valorEncontrado]= Set Variable 1000,00
# ${DebitosProcessados}[valorEncontrado]= Set Variable 700,00
# ${DebitosPendentes}[valorEncontrado]= Set Variable 300,00
Set To Dictionary ${tabelaResultado}[0] valorEncontrado=1000,00
Set To Dictionary ${tabelaResultado}[1] valorEncontrado=700,00
Set To Dictionary ${tabelaResultado}[2] valorEncontrado=300,00
or after updating dictionaries you could recreate the original list with
${TotalDebitos}[valorEncontrado]= Set Variable 1000,00
${DebitosProcessados}[valorEncontrado]= Set Variable 700,00
${DebitosPendentes}[valorEncontrado]= Set Variable 300,00
VAR @{tabelaResultado} = ${TotalDebitos} ${DebitosProcessados} ${DebitosPendentes}