Watir-Webdriver - 解析表格并单击开始/停止切换

时间:2013-12-04 21:37:58

标签: ruby selenium-webdriver watir-webdriver

请保持温柔。我对Ruby和Watir很新。通常也是UI自动化测试的新手。我正在尝试通过表解析然后单击启动/停止应用程序的切换。可以打开和关闭的应用程序未设置。它们的范围从1到'无限'数量的应用程序。我需要能够解析每一行并点击切换的东西。如果应用程序启动,我需要将其关闭,反之亦然。无论存在多少行。该行还包含第一列中的一个元素,该元素允许单击该应用程序并将其带到另一个页面,该页面提供有关所述应用程序的详细信息。切换存在于表的最后一列中。这是我正在查看的HTML,部署了两个应用程序。这可能是我想念的简单事情。我通常也是一个刚刚陷入其中的IT管理员。感谢所有帮助

<table id="apps_data" class="table table-striped table-bordered table-hover ng-scope">
  <thead>
    <tr>
  </thead>
  <tbody>
    <tr class="link ng-scope" ng-repeat="app in services.data | orderBy:services.sort">
      <td class="ng-binding" ng-click="click_app(app.Id)">Zenoss</td>
      <td class="ng-binding deploy-success" ng-class="app.deploymentClass">
        <span class="glyphicon glyphicon-ok" ng-class="app.deploymentIcon"></span>
        successful
      </td>
      <td class="ng-binding">default</td>
      <td>
        <div class="toggle btn-group" data-toggle="buttons">
          <label class="ng-binding btn btn-success active" ng-click="clickRunning(app, 'start', servicesService)" ng-class="app.runningClass">
            <input class="ng-pristine ng-valid" type="radio" value="1" ng-model="app.Running" name="running0">
            started
          </label>
          <label class="ng-binding btn btn-default off" ng-click="clickRunning(app, 'stop', servicesService)" ng-class="app.notRunningClass">
            <input class="ng-pristine ng-valid" type="radio" value="stopped" ng-model="app.Running" name="running0">
          </label>
        </div>
      </td>
    </tr>
    <tr class="link ng-scope" ng-repeat="app in services.data | orderBy:services.sort">
      <td class="ng-binding" ng-click="click_app(app.Id)">Zenoss</td>
      <td class="ng-binding deploy-success" ng-class="app.deploymentClass">
        <span class="glyphicon glyphicon-ok" ng-class="app.deploymentIcon"></span>
        successful
      </td>
      <td class="ng-binding">default</td>
      <td>
        <div class="toggle btn-group" data-toggle="buttons">
          <label class="ng-binding btn btn-success active" ng-click="clickRunning(app, 'start', servicesService)" ng-class="app.runningClass">
            <input class="ng-pristine ng-valid" type="radio" value="1" ng-model="app.Running" name="running1">
            started
          </label>
          <label class="ng-binding btn btn-default off" ng-click="clickRunning(app, 'stop', servicesService)" ng-class="app.notRunningClass">
            <input class="ng-pristine ng-valid" type="radio" value="stopped" ng-model="app.Running" name="running1">
          </label>
        </div>
      </td>
    </tr>
  </tbody>
</table>

到目前为止我有这个,但它只是到了第一行。

 if ff.div(:class, 'toggle btn-group').label(:class, 'ng-binding btn btn-success active').exist?
    ff.div(:class, 'toggle btn-group').label(:class, 'ng-binding btn btn-default off').click; sleep 1
  ff.div(:class, 'toggle btn-group').label(:class, 'ng-binding btn btn-default off').exist?
    ff.div(:class, 'toggle btn-group').label(:class, 'ng-binding btn btn-default off').click; sleep 1
 end

1 个答案:

答案 0 :(得分:0)

这可能会做你想要的:

apps = browser.table(:id => 'apps_data').tbody.trs
apps.each do |app|
    set_radios = app.radios.find_all{ |radio| !radio.set? }
    if set_radios.length == 2
        # Neither radio button is set, so do nothing? 
    else
        set_radios.first.set
    end
end

这是做什么的:

  1. 获取表正文中所有tr元素的集合。假设每个tr元素代表一个“app”。
  2. 遍历每一行(app):
    1. 计算未设置的单选按钮数。
    2. 如果两个单选按钮都没有设置,则不执行任何操作。
    3. 如果只找到一个未设置的单选按钮,则会设置。
  3. 请注意,由于应用的2个单选按钮具有相同的名称属性值,因此设置一个单选按钮将自动取消另一个。