E2e transcavator for sorting

I am writing e2e test cases using a protractor. I am trying to write tests for sorting (ascending, descending). This is my html code

<tbody>
    <tr>
        <td>
         <a ng-click="sortBy('Name')" href="" style="text-decoration: none;" class=""> Name</a>
        </td>
        <td>
         <a ng-click="sortBy('RollNo')" href="" style="text-decoration: none;"> Roll Number</a>
        </td>
        <td>
         <a ng-click="sortBy('subject')" href="" style="text-decoration: none;"> Subject</a>
        </td>     
    </tr>
    <tr>
        <td>ANDREW, STRAUSS</td>
        <td>122</td>
        <td>Maths</td>
    </tr>  
    <tr>
        <td>ANDREW, STRAUSS</td>
        <td>123</td>
        <td>Science</td>
    </tr>      
</tbody>

Can we check the sorting. If so, how?

+4
source share
1 answer

To check the sorting in any order,

  • Click on the sortBy element to sort the element in order.
  • Get the text of an element from columns displayed with sort order.
  • Save each element text in an array later so that you can check the sorting later.
  • The next step is to copy this unsorted array into a temporary array and sort the temporary array.
  • , , , , expect.

Javascript sort() reverse() .

, . , -

var sorted = [] , unSorted = [];
var ele = element.all(by.css(tbody tr td:nth-of-type(1)));
ele.each(function(eachName){
    eachName.getText().then(function(name){
        unSorted[i] = name;
        i++;
    });
}).then(function(){
    //check sorting
    sorted = unSorted.slice();
    sorted.sort(); //use sort function of Javascript
    expect(sorted).toEqual(unSorted);
});

RollNo . , , .

map() , , . . -

    ele.map(function(eachName){
        return eachName.getText().then(function(unSorted){
            return unSorted;
        });
    }).then(function(unSorted){
            var sorted = unSorted.slice();
            sorted = sorted.sort(); //sort the array
            expect(sorted).toEqual(unSorted); //check if both sorted and unsorted arrays are same
    });

, .

+6

All Articles