2

I want to select specific element from a table by the second column value (I removed the whitespaces that where rendered), and after that element is found I want to click on it (the return is true). I've tried this, but it doesn't click on it, but just finds the element.

The html code for that field I want to select is below:

<table ng-table="tableParams" id="Panel" class="tbl-option-list" template-pagination="directives/controls/Pager/Pager.html">
    <caption translate>Orders</caption>
    <tr id="Panel">
        <!-- 1 -->
        <th class="fixed-width-glyphicon"></th>
        <!-- 2 -->
        <th translate>Identifier</th>
    </tr>
        <tr ng-repeat="item in $data track by $index" ng-class="{'active-bg': order.$selected}" ng-click="changeSelection(order,  getRowActions(order))">
        <!-- 1 -->
        <td class="fixed-width-glyphicon">
            <div class="fixed-width-glyphicon">
                {{item.priority.toUpperCase()[0]}}
            </div>
        </td>
        <!-- 2 -->
        <td>{{item.identifierCode}}</td>
    </tr>
</table>

The select command from protractor is:

var deferred = protractorData.p.promise.defer();
element.all(by.repeater('item in $data track by $index')).filter(function(row) {
    row.getText().then(function(txt) {
        txt = txt.replace(/\s/g, '');
        var found = txt.split('ID0001');
        return found.length > 1;
    });
}).then(function(elem) {
        deferred.fulfill(elem[0]);
    });
    return deferred.promise;
}

I received the following error:

TypeError: Cannot call method 'click' of undefined.

Diana
  • 105
  • 1
  • 7
  • can you please add a jsfiddle with your code it will be easy to debug ;) On a first view the element you try to click seems to not be displayed. – Radu Sep 25 '15 at 09:27
  • @Radu, Protractor is a node.js module and it won't run in JSFiddle. – Michael Radionov Sep 25 '15 at 09:28
  • 1
    Can you add your code to click the element? I guess you are trying to click on the returned promise and not returned element. – giri-sh Sep 25 '15 at 09:33
  • So what does that error message tell you? Trace backwards and find the error. – JeffC Sep 25 '15 at 14:34
  • Avoid the [deferred antipattern](http://stackoverflow.com/q/23803743/1048572) – Bergi Sep 27 '15 at 12:22
  • Please show us the code where you call `click`. Also, your select command is incomplete and weirdly indented. – Bergi Sep 27 '15 at 12:28

3 Answers3

3

Seems like the element is not getting returned to be clicked. Try the below example to click and see if it works right in the filter function instead of returning the element using promises -

element.all(by.repeater('item in $data track by $index')).filter(function(row) {
    //return found element
}).then(function(elem) {
    elem[0].click(); //click it here
});

Or return the element in the below way and then click on it in your test spec. Here's how -

var clickElement = element.all(by.repeater('item in $data track by $index')).filter(function(row) {
    //return found element
}).then(function(elem) {
    return elem[0]; //return the element
});
return protractor.promise.fulfilled(clickElement);

Hope it helps.

giri-sh
  • 6,934
  • 2
  • 25
  • 50
0

Why don't you just return the element? there is no need for promise.defered here:

return element.all(by.repeater('item in $data track by $index')).filter(function(row) {
    row.getText().then(function(txt) {
    txt = txt.replace(/\s/g, '');
    var found = txt.split('ID0001');
    return found.length > 1;
});
}).then(function(elem) {
    return elem[0];
});

Note the added return in the start, that way you are returning a promise for return elem[0];

vrachlin
  • 817
  • 5
  • 15
0

The .filter function should return a value. In your example I believe it should be (on the third line):

return row.getText().then(function(txt) {