Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all classes of an element in capybara?

Tags:

ruby

capybara

Using capybara, is there a way to get all of the css classes of an element? Looking at the documentation, I do not see any easy way of doing this.

Also, given an element, elem, and a css class, someclass, should elem.has_css?('.someclass') be equivalent to the jquery $(elem).hasClass('someclass')?

This particular test is using Selenium, because it involves javascript.

like image 768
mushroom Avatar asked Jan 01 '13 20:01

mushroom


1 Answers

Element#[] looks pretty easy to me:

find('div')[:class]

Also, given an element, elem, and a css class, someclass, should elem.has_css?('.someclass') be equivalent to the jquery $(elem).hasClass('someclass')?

Element#has_css? checks for elements with specified CSS selector within element that method is invoked on. Jquery's hasClass checks if an element has specified class.

Update: Capybara matchers find elements/text within current element. Element#has_css? checks if an element contains element with specified css selector within it. If you want to check if element exists, use page.has_css?. Maybe matchers in form expect(page).to have_css('div') would be better for you.

like image 92
Andrei Botalov Avatar answered Oct 11 '22 16:10

Andrei Botalov