dpaluy
dpaluy

Reputation: 3705

How to use Regex expression in Capybara have_css/have_selector?

In Rspec Capybara test I want to check that no H4, H5, ..., H9 selector exists.

I would expect the following solution:

visit "/"
expect(page).not_to have_css(/(h|H)\[4-9]d/)

But it fails due to Nokogiri parsing

I also noticed that:

has_css?("h1")

# is not equal to

has_css?("H1")

Upvotes: 1

Views: 1411

Answers (1)

max
max

Reputation: 101891

Some alternatives:

1. Just create a slightly ugly expectation and get it done:

visit "/"
expect(page).not_to have_css("h1,H1,h2,H2,h3,H3,h4,H4,h5,H5,h6,H6,h7,H7,h8,H8,h9,H9")

I used the following snippet to generate that ugly selector:

(1..9).map { |n| "h#{n.to_s},H#{n.to_s}" }.join(',')

2. Create a case insensitive XPath selector:

How can I create a nokogiri case insensitive Xpath selector?

3. Use Nokogumbo

Nokogumbo is a HTML5 parser which creates lowercase element names.

Upvotes: 1

Related Questions