【问题标题】:Finding a fieldset using page-object使用页面对象查找字段集
【发布时间】:2013-03-27 20:10:00
【问题描述】:

我正在尝试查找一组复选框,但我需要在字段集中找到它们。 html 是这样的(它是一个 gwt 应用程序,所以会生成大量的东西:

<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>

我正在使用 Watir 和页面对象 gem。我正在尝试查找字段集,但没有字段集元素。从长远来看,我需要做的是找到每个复选框,获取它是否被选中的值,并将其与其名称一起存储在哈希中。

即使页面对象有一个字段集方法,我也不知道如何找到每个连续的复选框并获取值和标签。

【问题讨论】:

    标签: ruby watir fieldset pageobjects page-object-gem


    【解决方案1】:

    您可以使用通用的element 访问器方法声明一个字段集。对于您的字段集,它将是:

    element(:other_roles, :fieldset, :class => 'createUser-otherRolesFieldset')
    

    要创建值的散列,您必须创建一个迭代跨度并存储标签值和复选框值的方法。以下页面对象类具有执行此操作的方法:

    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
    

    如您所见,other_role_values 方法将返回一个哈希值,该哈希值由名称(我假设您的意思是标签)和复选框的值(true 或 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}
    

    一边

    在回复 Chuck 的评论时,不用页面对象 gem 也可以类似地编写。

    在瓦提尔:

    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
    

    在 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
    

    【讨论】:

    • 啊,效果很好。我完全忘记了元素访问器方法。谢谢!
    • 贾斯汀干得好,我总是喜欢这样复杂的事情只需几行干净的代码就可以解决。我确实想知道,使用 Selenium 尝试同样的事情会是什么样子?我有这种感觉,这是 Watir 优越的情况,但这可能是偏见,很高兴看到代码知道我是否正确。
    • @ChuckvanderLinden,纯水和硒解决方案基本相同(请参阅更新的答案)。 selenium 解决方案有点冗长,但不会比平时多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2016-04-16
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    相关资源
    最近更新 更多