Question

I'm trying to find a set of checkboxes, but I need to locate them in a fieldset. The html is like this (it's a gwt app, so tons of stuff is generated:

<div id="UpdateUserView-RolesColumn">
  <fieldset style="">
    <legend>Primary Role</legend>
    <select class="gwt-ListBox">
      <option value="ROLE_GENERAL_USER">ROLE_GENERAL_USER</option>
      <option value="ROLE_ADMIN">ROLE_ADMIN</option>
    </select>
    </fieldset>
  <fieldset style="" class="createUser-otherRolesFieldset">
    <legend>Other Roles / Permissions</legend>
    <div style="overflow: auto; position: relative; zoom: 1; height: 250px;">
      <div style="position: relative; zoom: 1;">
        <div>
          <span class="gwt-CheckBox">
            <input type="checkbox" value="on" id="gwt-uid-760" tabindex="0" checked="">
            <label for="gwt-uid-760">ROLE_BLAH1_USER</label>
          </span>
          <span class="gwt-CheckBox">
            <input type="checkbox" value="on" id="gwt-uid-761" tabindex="0" checked="">
            <label for="gwt-uid-761">ROLE_BLAH2_USER</label>
          </span>
          <span class="gwt-CheckBox">
            <input type="checkbox" value="on" id="gwt-uid-762" tabindex="0" checked="">
            <label for="gwt-uid-762">ROLE_BLAH3_USER</label>
          </span>
          <span class="gwt-CheckBox">
            <input type="checkbox" value="on" id="gwt-uid-763" tabindex="0" checked="">
            <label for="gwt-uid-763">ROLE_BLAH4_USER</label>
          </span>
          <span class="gwt-CheckBox">
            <input type="checkbox" value="on" id="gwt-uid-764" tabindex="0" checked="">
            <label for="gwt-uid-764">ROLE_BLAH5_USER</label>
          </span>
        </div>
      </div>
    </div>
  </fieldset>
</div>

I'm using Watir and the page-object gem. I'm trying to find the fieldset, but there is no fieldset element. What I need to do in the long run is find each checkbox, get the value of whether it is checked or not, and store that in a hash along with its name.

Even if page-object had a fieldset method I don't know how I would find each consecutive checkbox and get the value and the label.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top