protractor icon indicating copy to clipboard operation
protractor copied to clipboard

getText() not working on sub element - p tag, returns empty string

Open KamalakannanPE opened this issue 11 years ago • 25 comments

Hi, I have the below block in my ng-repeat

 <div ng-repeat="node in nodes">
     <div class="class1">
            <div class="class2">
                  <span class="cls-icon"></span>
                  <p>Text1</p>
            </div>
   </div>
</div>
 <div ng-repeat="node in nodes">
     <div class="class1">
            <div class="class2">
                  <span class="cls-icon"></span>
                  <p>Text2</p>
            </div>
   </div>
</div>
.....

Since the only text inside each repeated block exists in p tag, I'm using like below,

element(by.repeater('node in nodes').row(0)).getText().then(function(text) {
    expect(text).toBe('Text1');
});

But this always returns empty string ''.

KamalakannanPE avatar Feb 09 '15 10:02 KamalakannanPE

Hi,

If you could go through the FAQ link: http://angular.github.io/protractor/#/faq, you can find that 'getText()' returns empty string. so use "element.getAttribute('value')"

sallojusuresh avatar Feb 09 '15 15:02 sallojusuresh

Closing since this is an answered question. Thanks @sallojusuresh!

juliemr avatar Feb 09 '15 18:02 juliemr

Why did you close this issue ? The issue is not with input or textarea, it is with getText() of p in a ng-repeater. How can I use getAttribute('value') when there is value bound to any of the elements ?

KamalakannanPE avatar Feb 10 '15 04:02 KamalakannanPE

Try below one:

element(by.repeater('node in nodes').row(0)).getAttribute('value').then(function(text) { expect(text).toBe('Text1'); });

OR

element(by.tagName('p')).getAttribute('value').then(function(text) { expect(text).toBe('Text1'); });

sallojusuresh avatar Feb 10 '15 12:02 sallojusuresh

@KamalakannanPE do you run your tests in PhantomJS? If so, using getInnerHtml() instead of getText() did solve our problem (see #1229).

@juliemr @sallojusuresh using getAttribute('value') actually doesn't help, since the example doesn't use an input element. @KamalakannanPE tried to explain that getText() is expected to return the innerHTML of the p element. So, I also wouldn't regard this issue to be closed, but #1229 seems to be the better place to work on this issue.

gesellix avatar Feb 12 '15 11:02 gesellix

@gesellix Nope. I tried in Chrome browser only. not in PhantomJS.

KamalakannanPE avatar Feb 12 '15 11:02 KamalakannanPE

@KamalakannanPE so, could you try if using getInnerHtml() solves the issue for your setup? That would be an interesting aspect to add to the other issue #1229 .

gesellix avatar Feb 12 '15 11:02 gesellix

getInnerHtml() returns

<div class="class1">
            <div class="class2">
                  <span class="cls-icon"></span>
                  <p>Text2</p>
            </div>
   </div>

but I want "Text2" only.

KamalakannanPE avatar Feb 12 '15 11:02 KamalakannanPE

@KamalakannanPE ah, right, I forgot the different semantics :)

@juliemr I'm not sure if this issue and #1229 should be combined. We tried other places with the same code and sometimes getText() works, sometimes not. What would you need from us to investigate?

gesellix avatar Feb 12 '15 11:02 gesellix

There is definitely an issue here...

I ran a test on an array of <p> elements containing some <em> tags, and one time out of 5, the .getText() method ignores the text contained in the <em> tags.

For example:

[
  "<em>Beer</em> Production Up 17.9% To 18.5 Million Decaliters In April",
  "Anheuser-Busch temporarily halts <em>beer</em> production to help Texas, Oklahoma flood victims",
  "Texas floods: Budweiser brewery to can water instead of <em>beer</em> to help flood victims",
  "KC allows tiny breweries in neighborhoods, liquor and <em>beer</em> sales in City Market",
  "<em>Beer</em> news: Funky Buddha to open restaurant inside brewery"
]

The text is extracted well for all those string except for the 4th one, where I got: KC allows tiny breweries in neighborhoods, liquor and sales in City Market

EDIT: I am using selenium. Maybe the issue is in selenium rather than in protractor..?

paulintrognon avatar Jun 04 '15 15:06 paulintrognon

Is it possible to reopen this issue? The first given answer does not apply here, and using innerHTML does not do the job when needing to compare texts, regardless of additional tags.

I have the same issue, and I need to manually remove HTML tags and transform HTML entities back to their text value to get my tests passing. Current implementation of getText does not work consistently as described in the docs:

Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.

ghusse avatar Jun 04 '15 15:06 ghusse

I had an empty string yielded from the promise getText() returned, neither from an input nor a textarea Here is an excerpt of my HTML:

<li data-ng-repeat="emitter in account.identifier.emitters" data-ng-if="emitter._id != account.emitter._id">
    <a href="" data-ng-click="switchToEmitter(emitter)" data-ng-bind="emitter.name"></a>
</li>

While this yielded an empty string:

element(by.css('li[data-ng-repeat="emitter in account.identifier.emitters"] a')).getText()

that did the trick:

element(by.css('li[data-ng-repeat="emitter in account.identifier.emitters"] a')).getAttribute('innerText')

blarsy avatar Jun 11 '15 18:06 blarsy

I've tried out the initial code and I cannot reproduce this issue. (https://github.com/juliemr/protractor/commit/4ecfad8329d83ae3de07f07e348b801ac59cdbca)

Additional comments here aren't enough for me to try to reproduce. @blarsy I tried running with ng-bind instead of using interpolation and was unable to reproduce the issue.

I tried with Chrome and Firefox.

I'm closing this as unable to repro. Please open a new issue if you find a reliable way to do this and believe there is still an issue with protractor or selenium (and if it's with selenium, please open an issue on their repository!)

juliemr avatar Jan 12 '16 18:01 juliemr

I have a similar issue where our protractor tests fail occasionally where getText has returned an empty string. This fails quite often on our CI and often it will pass if you just run it again.

Sometimes it is just one of them in the array, sometimes it is 2 or 3 of them, but never all of them and it does get the correct number of elements.

We are using ui-select and we have to click it to ensure it is visible first. It almost feels like the promise is resolving before being able to get the text for the first couple of elements, although I have tried a browser.sleep after the click and it didn't help.

Could you reopen this or should I create a new issue as it is an occasional failure?

11:32:03.856 [chrome #1-33]   1) when I navigate to room types it should display the bed configuration in the correct order
11:32:03.856 [chrome #1-33]    Message:
11:32:03.856 [chrome #1-33]      Expected [ '', 'Double', 'Twin', 'Double or Twin' ] to equal [ 'Single', 'Double', 'Twin', 'Double or Twin' ].
// EditPage
    self.getConfigurations = function () {
        finder.element(by.css('[ng-model="vm.roomType.configuration"]')).click();
        return finder.all(by.id('ui-select-choices-1')).get(0).all(by.css('.ui-select-choices-row')).getText();
    };

//....

    it('it should display the bed configuration in the correct order', function () {
        var editPage = listPage.createRoomType();

        expect(editPage.getConfigurations()).toEqual(['Single', 'Double', 'Twin', 'Double or Twin']);
    });

jamlen avatar Jan 13 '16 11:01 jamlen

@jamlen please open a new issue with a reproducible test that we can run (and some sort of indication of how often you're seeing the failure).

juliemr avatar Jan 13 '16 17:01 juliemr

I have the exact same issue here. Using phantonjs 1.9.19. I'm trying to extract the text of a th element, and a blank string is returned. using getInnerHtml instead of getText works though in this situation.

@jamlen if you do create a new issue, please do link it here.

jrharshath avatar Jan 28 '16 07:01 jrharshath

One possible cause for this is operator error, bad use of Promises. For example,

    .then( () => {
        AnElement.getText();
    })
    .then( function (foundText) {
        expect( foundText ).to.equal( expectText );
    });

Without a return in the first clause, the test will always fail.

leegee avatar Mar 15 '16 16:03 leegee

Why would getText() return an empty string while getAttribute('textContent') returns the expected text? I feel like I can't trust getText and need to use getAttribute all the time now.

mvolkmann avatar Oct 08 '16 14:10 mvolkmann

Not working for me either. I am trying to get the text for an option in a select element and getting blank text from getText()

carlos-granados avatar Oct 19 '16 10:10 carlos-granados

I ran into this as well. I don't have a nice repro, as it happened when I tried to get the text from an ACE editor. But invariably the text that was missing was the text within internal HTML elements that were not visible due to falling outside of the bounds of the ACE editor box. Example:

This is <i>all</i> visible.
This is a longer line, and the second sentence is beyond the visible area. The text in tags is <i>not visible</i>.

getText() would then give me:

This is all visible.
This is a longer line, and the second sentence is beyond the visible area. The text in tags is .

(The not visible piece is missing from the second line.)

I checked the DOM and the elements were all there. The workaround with getAttribute('innerText') works perfectly.

darabos avatar Oct 27 '16 13:10 darabos