Behat step definition to verify whether the radio button is checked

582 Views Asked by At

I want to write a step definition verify whether a radio button is checked. Each radio button has an associated label:- 'Yes' or 'No'

The name of the radio buttons inside the input tags is same for the labels associated with a particular radio button

HTML Structure:

<label>
    <input type= "radio" name="subscribe_list" value="1" 
    checked="checked">
    :: before
    </input>
    "Yes"
</label>
<label>
    <input type= "radio" name="subscribe_list" value="1" 
    checked="checked">
    </input>
    "No"
<label>

Ex: Subscribe to list: Yes No

There are multiple radio buttons like the above example each with a 'Yes' or 'No' label

I want to write a function which checks whether a 'Yes' or 'No' is checked for a particular radio button name. How should I implement this? Thanks in advance!

1

There are 1 best solutions below

0
On

You can use one of the existing steps from Behat, or you can implement your own.

Existing step:

the "my_checkbox_identifier" checkbox should be checked

This will work if you are able to identify the checkbox(input) using field id|name|label|value.

For implementing a custom step that works with selectors you need to first find the element via existing find method and use isChecked() on the returned node, and after use an exception/assert to fail the step if needed.