protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Cannot read property 'indexOf' of null error :Expected condition textToBePresentInElementValue

Open praveendvd opened this issue 5 years ago • 6 comments

If user tries to access an element that does not have the attribute 'value' then the EC.textToBePresentInElementValue throws Cannot read property 'indexOf' of null error :

let EC = protractor.ExpectedConditions;
describe('Test', function () {
   it('test {Regression} {Sanity} {Sanity}', async function () {
      await browser.get("http://juliemr.github.io/protractor-demo/")
      let resultElement=element(by.xpath('//h2'))
      await browser.wait(EC.textToBePresentInElementValue(resultElement, 'Z'), 5000,"this is first 1");
    });
   });

Expected:

Script should wait till value attribute is present and contains in the text. If not, it should time out with customised message "this is first 1",

Protractor version: Version 5.4.2 Node: v10.16.3

praveendvd avatar Jan 25 '20 21:01 praveendvd

Maybe a good solution would be to use by.cssContainingText("h2", "Z") and then use .presenceOf. This will basically do the same thing as what you have done as you will be looking for an element with the h2 tag in it and "Z" to be contained inside of it. Even if the h2 element loads, it will not prompt the test to continue until the text is populated inside of the element.

Fuun347 avatar Jan 31 '20 13:01 Fuun347

Maybe a good solution would be to use by.cssContainingText("h2", "Z") and then use .presenceOf. This will basically do the same thing as what you have done as you will be looking for an element with the h2 tag in it and "Z" to be contained inside of it. Even if the h2 element loads, it will not prompt the test to continue until the text is populated inside of the element.

Hi @Fuun347 textToBePresentInElementValue checks for the attribute "value" and not the text

praveendvd avatar Jan 31 '20 14:01 praveendvd

Haven't used that one before so I just assumed it was checking for element to contain text inside of it. What you can do, but is not very ideal is getAttribute("value") and use expect to compare if it matches, but that won't work very well if it takes longer for the value to appear inside of the element.

Fuun347 avatar Jan 31 '20 15:01 Fuun347

Haven't used that one before so I just assumed it was checking for element to contain text inside of it. What you can do, but is not very ideal is getAttribute("value") and use expect to compare if it matches, but that won't work very well if it takes longer for the value to appear inside of the element.

Thanks @Fuun347 i already wrote a promise that will check the status=getAttribute('value')==='' and returns !status , so that the wait waits till getAttribute('value') is not null. : ) The bug was raised to help others who encounter such a issue.

praveendvd avatar Jan 31 '20 16:01 praveendvd

hi the bug is that textToBePresentInElementValue use getAttribute that could return null and doing actualText.indexOf(text) > -1 where actualText is the return of getAttribute without checking is null or not it 's not fixed on 7.0 https://github.com/angular/protractor/blob/release-7.0/lib/expectedConditions.ts#L240 but seems catch on master branch but i think the code will not wait for a non null value in element value and directly raise https://github.com/angular/protractor/blob/master/lib/expectedConditions.ts#L237

sschoorens avatar Jun 08 '21 07:06 sschoorens

@praveendvd Could you share your solution, pls?

ludmilanesvitiy avatar Jun 14 '21 14:06 ludmilanesvitiy