user6264
user6264

Reputation: 185

How to get the header from table in protractor?

I have table in my angular application. How to iterate through the table headers and compare the headings in it?

Following is sample html code.


<table>
        <tbody> 
             <th>
                <td>head1</td>
                <td>head2</td>
                <td>head2</td>
            </th>

            <tr>
                <td>row1Col1</td>
                <td>row1Col2</td>
                <td>row1Col3</td>
            </tr>

            <tr>
                <td>row2Col1</td>
                <td>row3Col2</td>
                <td>row4Col3</td>
            </tr>

            <tr>
                <td>row3Col1</td>
                <td>row3Col2</td>
                <td>row3Col3</td>
            </tr>

        </tbody>
    </table>

How to loop through the number of columns(headers).

Upvotes: 0

Views: 1050

Answers (1)

yong
yong

Reputation: 13722

Method 1

var expectHeaders = ['head1', 'head2', 'head3'];
var headers = element.all(by.css('table > tbody > th > td'));

expect(headers.getText()).toEqual(expectHeaders);

//or do assertion in then()
headers.getText().then(function(actualHeaders){
  expect(actualHeaders).toEqual(expectHeaders);
  // or compare header one by one in loop
  expectHeaders.forEach(function(expectHeader, index){
     expect(actualHeaders[index]).toEqual(expectHeader);
  })
})

Method 2
if you prefer to iterate through each header

var expectedHeaders = ['head1', 'head2', 'head3'];
var headers = element.all(by.css('table > tbody > th > td'));

expectedHeaders.forEach(function(header, index){
   expect(headers.get(index).getText()).toEqual(header);
   // or compare in then()
   headers.get(index).getText().then(function(actual){
      expect(actual).toEqual(header)
   })
});

Upvotes: 1

Related Questions