Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assert with chai that some div contains some text?

Tags:

assert

chai

How to assert with chai that some div('.MyDive') contains text 'Some text'?

<div class="MyDive">
   <div>Some text</div>
</div>
like image 242
gandra404 Avatar asked Jun 27 '16 19:06

gandra404


1 Answers

You'll want to pull in jQuery to access elements on the DOM. Once you have that, you can assert against DOM elements using vanilla Chai like so:

expect($('div.MyDive').text()).to.have.string('Some text');

If you want to visually clean up your code a bit, you could also pull in a plugin like chai-jquery and use a custom assertion, such as contain(text):

expect($('div.MyDive')).to.contain('Some text');
like image 72
Nathan Thompson Avatar answered Sep 20 '22 01:09

Nathan Thompson