0

I am trying to write a Protractor test for the below requirement in my Angular app:

I am trying to validate the text of all column headers in my Angular grid below:

myPage.column_headers
      .map(function (header) {
          return header.getText()
      }).then(function (headers) {
    expect(headers).toEqual(['Column1', 'Column2', 'Column3', 'Column4', 'Column5', 'Column6']);
});

When I run this, I get the below console error:

Expected $.length = 3 to equal 6.
Expected $[0] = 'Col1' to equal 'Column1'.
Expected $[1] = 'Col2' to equal Column2'.
Expected $[2] = 'Col3' to equal 'Column3'.
Expected $[3] = undefined to equal 'Column4'.
Expected $[4] = undefined to equal 'Column5'.
Expected $[5] = undefined to equal 'Column6'.

As a real-time user, when I navigate to the page, only 3 of the columns are immediately visible to the user.

To see the other 3 columns, you would need to scroll across the page / grid, so I am wondering if that is why this test is failing with the above error.

If that is the case, can someone please tell me what changes I need to make so that I can validate all of the column headers within the Angular grid?

user9847788
  • 2,135
  • 5
  • 31
  • 79
  • Yes, those columns are technically not present in the DOM until you scroll so you can't `getText()` on them. You have to scroll the elements into view first. – tehbeardedone Sep 23 '20 at 18:04

1 Answers1

1

if elements are in DOM but not present, you should use .getAttribute('innerText') instead of .getText()

if element are not in DOM, then you have to scroll to these elements. A request will be sent, and when resolved you can get their text value.

Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40
  • Good answer. Not trying to speak for OP but just looking at his failure logs, I'm guessing those other columns aren't present. Otherwise `$.length` would have the right number of columns. The selector would still find the missing columns if they were present. – tehbeardedone Sep 23 '20 at 19:32
  • Thanks for your answer, but I've tried `.getAttribute('innerText')` & am unfortunately still getting the same failure. There are 6 columns in the grid, only 3 visible when you navigate to the page though. So for this test, would I have to scroll across the grid? And if so, is there a function I can use for that? Or is there another possible solution? – user9847788 Sep 24 '20 at 13:59
  • a function exists, you can look it up – Sergey Pleshakov Sep 24 '20 at 21:15