I want to use variable throughout my test cases. For that, I have some script in Common_File.robot, and I want to use that variable in my Main.robot file without using Set Suit Variable. How can I do this? If I have following code in my Common_File.robot
*** Keywords ***
Keys for dictionary
${First_Dictionary}= Create Dictionary
${boxes_count}= Get Element Count ${coulumns}
FOR ${index} IN RANGE 1 ${boxes_count}+1
${boxes_txt}= Get Text ${column}\${index}
Set To Dictionary ${First_Dictionary} ${boxes_txt} ${EMPTY}
END
${Second_Dictionary}= Create Dictionary ${box1}=${EMPTY}
${box2}=${EMPTY} ${box3}=${EMPTY} ${box4}=${EMPTY}
[RETURN] ${First_Dictionary} ${Second_Dictionary}
Now In Main.robot file I want to use this variables
*** Test Cases ***
Test Case 001
Parse Xpath To Dictionary ${xpath1}
Test Case 002
Parse Xpath To Dictionary ${xpath2}
*** Keywords ***
Parse Xpath To Dictionary
[Arguments] ${xpath}
${final_dictionary}= Create Dictionary
${Count}= Get Element Count ${boxes}
FOR ${index} IN RANGE 1 ${Count}+1
${xpath_available}= Run Keyword And Ignore Status Element Should Be Visible ${xpath_with_index}
IF ${xpath_available}
${webelements}= Get Webelements ${xpath}
FOR ${element} IN @{webelement}
${element_text}= Get Text ${element}
END
END
${key}= Evaluate list(${First_Dictionary}[${index}]
Set To Dictionary ${final_dictionary} ${key}=${element_text}
END
${final_dictionary2}= Create Dictionary
${Count2}= Get Element Count ${boxes2}
FOR ${index} IN RANGE 1 ${Count2}+1
${xpath_available}= Run Keyword And Ignore Status Element Should Be Visible ${xpath2}
IF ${xpath_available}
${webelements}= Get Webelements ${xpath2}
FOR ${element} IN @{webelement}
${element_text}= Get Text ${element}
END
END
${key}= Evaluate list(${Second_Dictionary}[${index}]
Set To Dictionary ${final_dictionary2} ${key}=${element_text}
END
For above code I’m getting error as variable ‘${First_Dictionary}’ not found and works fine with Set Suit Variable
can anyone help to solve this issue or any suggestions
you didn’t close the round bracket ) for the cast to list
you can’t use a numeric value to select a key from a dictionary, and you can’t cast a dictionary directly to a list, but python provides a keys function for a dictionary that can be cast to a list.
Hi @damies13 thanks for the response I have tried above code by using Set Suit Variable code works fine but I dont want to use Set Suite Variable also
I want to use keys of ${First_Dictionary} and ${Second_Dictionary} and make seperate Dictionary for both of them I dont know how can I use key created in common file(i.e${First_Dictionary}) ${Second_Dictionary} to Main.robot file (i.e in ${final_dictionary} ${final_dictionary2} )
Can you please show with the code for better understanding
If we look at the first part of Parse Xpath To Dictionary from your post:
*** Keywords ***
Parse Xpath To Dictionary
[Arguments] ${xpath}
${final_dictionary}= Create Dictionary
${Count}= Get Element Count ${boxes}
FOR ${index} IN RANGE 1 ${Count}+1
${xpath_available}= Run Keyword And Ignore Status Element Should Be Visible ${xpath_with_index}
IF ${xpath_available}
${webelements}= Get Webelements ${xpath}
FOR ${element} IN @{webelement}
${element_text}= Get Text ${element}
END
END
Somewhere before this next line you should have called Keys for dictionary so that you can receive the dictionary First_Dictionary from it, but I’m not sure where (not sure exactly what you are trying to acheive), because Keys for dictionary was never called First_Dictionary doesn’t exist, don’t forget as First_Dictionary is a local variable in Keys for dictionary you need to set it to the return value from Keys for dictionary in the context of this keyword.
${key}= Evaluate list(${First_Dictionary}[${index}]
Set To Dictionary ${final_dictionary} ${key}=${element_text}
END
Refer to my last 2 posts, in the first one I gave you the line you need to add, in the second I gave you the last line it need to be add before, as to where exactly it should be added, I’m not sure only you can answer that.