Search code examples
javascriptseleniumnightwatch.jsnightwatch

How to verify the text in a paragraph element in Nightwatch?


I have a text on a page and i have to verify that the text is present. The html for the element is <p class="subfooter__text" data-v-79ab1348=""> The Tesla is part of the <a href="//www.xyz.com/" target="_blank" rel="noopener nofollow">Automobile</a> publishing family.</p>

The text need to be verified is "The Tesla is part of the Automobile publishing family". The issue is, the assertion fails when asserted for full text, but it does pass when asserted for "Automobile" or "publishing family" or "Automobile publishing family". The assertion never passes for the text before the href tag.

Here is the code part browser.assert.containsText('p.subfooter__text', 'The Tesla is part of the Automobile publishing family'); I made sure that all spaces are taken care. Any help is greatly appreciated.


Solution

  • Your assertion looks correct. The only thing I could see is missing is the dot at the end of the sentence.

    browser.assert.containsText('p.subfooter__text', 'The Tesla is part of the Automobile publishing family.');
    

    Below is the screenshot of when I added your code to an HTML page and extracted the innerText.

    Checking inner text