How to check CSS selector value using Capybara and RSpec?

The HTML page is supposed to have the following code:

<div class="user-image" style="background-image:url(/images/user_image.jpg)"> 

How can you test this with Capybara and RSpec?

+8
ruby-on-rails tdd rspec bdd capybara
source share
2 answers

Presumably, you are trying to verify that this div is using the specified background image. I would probably do something like this:

 it "has a user image" do page.should have_selector('div.user-image') end it "displays the user image" do page.find('div.user-image')['style'].should == 'background-image:url(/images/user_image.jpg)' end 
RSpec is most likely the wrong tool to work with. Consider using Cucumber for such tests.
+13
source share

Capybara and Selenium let you run javascript in a browser and return a result

here's how:

 page.execute_script 'return $("div.user-image").css("background-image");' 
+4
source share

All Articles