I am using robot framework. How can I test if a checkbox is selected? I want to run a keyword if a checkbox is selected like:
${checked} = Checkbox Should Not Be Selected xpath=.//*[@id='0,1,1,6']/td[11]/input
Run Keyword if '${checked}' Click Element xpath=.//*[@id='0,1,1,6']/td[11]/input and wait
i.e. to click there if is not already clicked.
(The code of above is wrong, I know)
3 Answers 3
Use Run Keyword And Return Status
It runs the given keyword with given arguments and returns the status as a Boolean value.
This keyword returns True
if the keyword that is executed succeeds and False
if it fails. This is useful, for example, in combination with Run Keyword If
. If you are interested in the error message or return value, use Run Keyword And Ignore Error
instead.
From: Selenium2Library documentation.
Keyword: Checkbox Should Be Selected
Arguments: locator
Documentation: Verifies checkbox identified by locator is selected/checked. Key attributes for checkboxes are id and name. See introduction for details about locating elements.
-
Thanks for answering. As you can see I am using "Checkbox should be selected" in my code (in fact I am using the negation). This command works like an assertion and not a question. When the assertion fails the test stops. I want to ask if the checkbox is checked and not to assert that the checkbox should be checked.Luixv– Luixv2013年02月24日 07:24:03 +00:00Commented Feb 24, 2013 at 7:24
-
As mentioned in the other answer, "trap/wrap" the assertion by calling it via
Run Keyword And Return Status
which converts the pass/fail into a True/False return flag.MarkHu– MarkHu2020年06月18日 17:16:53 +00:00Commented Jun 18, 2020 at 17:16
You can try:
${isCheck} = Run Keyword And Return Status Checkbox Should Be Selected xpath=.//*[@id='0,1,1,6']/td[11]/input