How to click a button in row in a table using prot

2019-06-09 03:25发布

问题:

I am writing protractor test cases. I want to click on edit button. When I am checking by id it is not found.

Code:

<table class="table table-bordered table-hover">
   <thead>
       <tr>
          <th>Code</th>
          <th>Start Date</th>
          <th></th>
      </tr>
  </thead>
  <tbody>
      <tr ng-repeat="batch in batches.list">
         <td><a ui-sref="root.courses.detail.batches.assessments({ batch_id: batch.id,assessment_status: 'published'})">BID00{{batch.id}}</a></td>
         <td>{{batch.start_date}}</td>
         <td> 
             <button id="edit" type="button" class="btn btn-default btn-sm" tooltip-placement="top" tooltip="Edit" ng-controller="batchesCtrl" ng-click="edit_batch(batch.id)"><i class="glyphicon glyphicon-edit"></i></button> 
             <button type="button" id="delete(batch.id)" class="btn btn-danger btn-sm" tooltip-placement="top" tooltip="Delete" ng-click="remove_batch(1, batch.id)" confirmation-needed="Do you really want to delete this batch?"><i class="glyphicon glyphicon-trash" ></i></button>
         </td>
      </tr>
   </tbody>
</table>

How can I make it work?

回答1:

There are a few problems in your example. In html an ID should be unique on a page. So you can't use an id inside an element that repeats.

You have several options. You could address the button by one of it's styleclasses that the other button does no have like

element(by.repeater('batch in batches.list').row(0)).element(by.css('button.btn-default')).click();

You can also just specify you want the first button it finds like

element(by.repeater('batch in batches.list').row(0)).element(by.css('button:first-of-type')).click();

Alternatively you can also address the element with only css. Sometimes that is easier to maintain than the repeater:

element(by.css('tr:first-of-type > td:last-child > button:first-of-type')).click();


回答2:

Here i am not sending delay so what happen i am checking for the button which is not loaded ptor.sleep(500); fixes my problem. It took so much time to identify