Cucumber: Cleanly finding visible text

I’m writing Cucumber features and step definitions for an app and I’d like to assert if there is some particular text on the page. As an example, I would like to find the visible text "section".

Currently I’m using something like:

...getSource().then(function(source){
  source.should.contain(expectedText);
});

This doesn’t work when a section element is present on the page.

Ideally I would also like to restrict the scope of this search to within a particular selector like .random-container, although this is less important.

Capybara’s way of handling this is awesome, so anything that feels similar would be ideal:

within '.random-container' do 
  should have_content 'section'
end

What is the best way to assert visible text, while reducing possible collisions?

DISCLAIMER: I come from a Rails background.

this.client.getText('.random-container').should.eventually.contain('section') I think.

You should also check the webdriverIO api:

Woah, I’m using so much code from both of you guys. Thanks for the help and for being so active in the community.

2 Likes