1

I have the following element

element(by.css("span[aria-label='Picker ID, TESTUSER']")

I want to assure the text on this filed is TESTUSER, so I tried:

 expect(element(by.css("span[aria-label='Picker ID, TESTUSER']")).getAttribute('value')).toEqual('TESTUSER');

then I got the error: Expected null to equal 'TESTUSER'

So I tried:

element(by.css("span[aria-label='Picker ID, TESTUSER']")).getText().then(function (value) {
        console.log(value);
      })

And I got this error:

Expected '' to be 'TESTUSER'.

How to get the value of this field and expect it is TESTUSER?

1 Answer 1

1

So in most cases try in the following order:

getText() if it doesn't work use getAttribute('textContent') if that too doesn't work use getAttribute('value')

getAttribute('value') works only if there is an attribute called value in your element (like id, name etc)

So in most cases, if getText doesn't work use .getAttribute('textContent') Use:

element(by.css("span[aria-label='Picker ID, TESTUSER']")).getAttribute('textContent')

Because getText or text() actually uses innerText ,and will not detect text from hidden elements .

Don't get confused by the differences between Node.textContent and HTMLElement.innerText. Although the names seem similar, there are important differences:

textContent gets the content of all elements, including and elements. In contrast, innerText only shows “human-readable” elements.

textContent returns every element in the node. In contrast, innerText is aware of styling and won’t return the text of “hidden” elements.

Moreover, since innerText takes CSS styles into account, reading the value of innerText triggers a reflow to ensure up-to-date computed styles. (Reflows can be computationally expensive, and thus should be avoided when possible.)

Unlike textContent, altering innerText in Internet Explorer (version 11 and below) removes child nodes from the element and permanently destroys all descendant text nodes. It is impossible to insert the nodes again into any other element or the same element after doing so

https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent

2
  • tks @PDHide, your suggestion, totally works. I did an updated to print what I wanted: element(by.css("span[aria-label='Picker ID, TESTUSER']")).getAttribute('textContent').then(function(text){ console.log(text); }) Mar 9, 2020 at 13:22
  • @IngriddBrito please accept the answer by clicking the tick sign
    – PDHide
    Mar 9, 2020 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.