2
votes

Please be gentle. I'm very new to Ruby and Watir. In general also new to UI automation testing. I'm trying to parse through a table then click a toggle that start/stops an application. The applications that can be turned on and off are not set. They can range from 1 to 'infinite' number applications. I need something that will parse through each row and click the toggle. if the application is started, I need to turn it off and vice-versa. No matter how many rows exist. The row also contains an element in the first column that allows the application to be clicked and brought to another page that gives details about said application. The toggle exists in the last column of the table. Here is the HTML I'm looking at with two applications deployed. It may be something simple that I'm missing. I'm also normally an IT admin that just got thrown into this. Thanks for all the help

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

I have this so far, but it's just going to the first line.

 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

1 Answers

0
votes

This might do what you want:

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

What this does is:

  1. Gets a collection of all tr elements in the table body. Each tr element is assumed to represent an "app".
  2. Iterates through each row (app):
    1. Counts the number of radio buttons that are not set.
    2. If neither of the 2 radio buttons is set, it does nothing.
    3. If only one unset radio button is found, it is set.

Note that since the 2 radio buttons of the app have the same name attribute value, setting the one radio button will automatically unset the other.