You can declare a fieldset using the generic element
accessor method. For your fieldset, it would be:
element(:other_roles, :fieldset, :class => 'createUser-otherRolesFieldset')
To create a hash of the values you will have to create a method that iterates the spans and stores the label values and checkbox values. The following page object class has a method to do this:
class MyPage
include PageObject
element(:other_roles, :fieldset, :class => 'createUser-otherRolesFieldset')
def other_role_values()
other_roles = {}
other_roles_element.span_elements.each do |span|
other_roles[span.label_element.text] = span.checkbox_element.checked?
end
return other_roles
end
end
As you can see, the other_role_values
method will return a hash that is keyed by the name (which I assume you mean the label) with the value of the checkbox (true or false).
page = MyPage.new(browser)
p page.other_role_values
#=> {"ROLE_BLAH1_USER"=>true, "ROLE_BLAH2_USER"=>true, "ROLE_BLAH3_USER"=>true, "ROLE_BLAH4_USER"=>true, "ROLE_BLAH5_USER"=>true}
Aside
In reply to Chuck's comment, the same can be written similarly without the page object gem.
In Watir:
other_roles_element = browser.fieldset(:class => 'createUser-otherRolesFieldset')
other_roles = {}
other_roles_element.spans.each do |span|
other_roles[span.label.text] = span.checkbox.checked?
end
p other_roles
In Selenium-Webdriver:
other_roles_element = browser.find_element(:css => 'fieldset.createUser-otherRolesFieldset')
other_roles = {}
other_roles_element.find_elements(:tag_name => 'span').each do |span|
other_roles[span.find_element(:tag_name => 'label').text] = span.find_element(:tag_name => 'input').selected?
end
p other_roles