#156
Apr 06, 2009

Webrat

If you prefer writing integration tests in ruby instead of Cucumber's plain english, consider interacting with Webrat directly as I show in this episode.
Download (17.6 MB, 7:39)
alternative download for iPod & Apple TV (10.8 MB, 7:39)

Resources

sudo rake gems:install RAILS_ENV=test
script/generate integration_test authentication
rake test:integration
script/generate nifty_authentication
rake db:migrate
# config/environments/test.rb
config.gem "webrat", :version => ">=0.4.3"

# test/test_helper.rb
Webrat.configure do |config|
  config.mode = :rails
end

# test/integration/authentication_test.rb
class AuthenticationTest < ActionController::IntegrationTest
  test "logging in with valid username and password" do
    User.create!(:username => "rbates", :email => "ryan@example.com", :password => "secret")
    visit login_url
    fill_in "Username", :with => "rbates"
    fill_in "Password", :with => "secret"
    click_button "Log in"
    assert_contain "Logged in successfully."
  end
  
  test "logging in with invalid username and password" do
    User.create!(:username => "rbates", :email => "ryan@example.com", :password => "secret")
    visit login_url
    fill_in "Username", :with => "rbates"
    fill_in "Password", :with => "badsecret"
    click_button "Log in"
    assert_contain "Invalid login or password."
  end
end

RSS Feed for Episode Comments 36 comments

1. Russ Jones Apr 06, 2009 at 03:32

Thanks Ryan, I didn't realize it was so easy to use webrat on its own!

oh and ... "dealing directly with Webrat through ruby" ... 6:37 and 6:56 ... its looping through my brain now and won't stop.


2. bryanl Apr 06, 2009 at 04:31

I've had the same issues getting mechanize and selenium to work. I still won't give up on webrat. Bryan has done a great job with this tool.


3. erik Apr 06, 2009 at 08:31

After last weeks episode, I was curious about Webrat and now you have answered my questions!


4. Bill Apr 06, 2009 at 09:30

Too...many...testing...frameworks!!

(hence no tests)


5. Francois Apr 06, 2009 at 09:46

Just passing on a note of thanks for all these great screencasts. I watch them every week and have learned a ton.


6. Mourad Apr 06, 2009 at 10:04

Please continue with these testing screencasts.
Thanks :-)


7. Bryan Helmkamp Apr 06, 2009 at 11:14

Great screencast, Ryan! Thanks for covering Webrat.

For anyone interested in Webrat's Selenium support... grab the 0.4.4 gem I just released (once it's made it to the gem mirrors, or directly from git).

Fixed a couple bugs and added much better output to explain what's going on in that mode. I'll do a write up on my blog on how to run the example from this screencast in Selenium ASAP.

Cheers,

-Bryan


8. Bryan Helmkamp Apr 06, 2009 at 11:41

As promised, here are instructions for running those exact same tests with Webrat's Selenium mode:

http://www.brynary.com/2009/4/6/switching-webrat-to-selenium-mode


9. Ryan Bates Apr 06, 2009 at 14:03

Thanks for providing those instructions Bryan! I'll link to them in the show notes for the episode.


10. Rupak Ganguly Apr 06, 2009 at 14:32

Hi Ryan,
   Great screencast as usual. I just happened to notice that in the portion for test_helper.rb where you add the webrat configure command, you miss to highlight the point that a line for require 'webrat' needs to be added to the top of the file as well. Some users can get confused when they run rake test:integration and get an error as it cannot find Webrat. Hope it helps.

Thanks,
Rupak Ganguly


11. George Apr 06, 2009 at 22:34

keep it up.


12. Victor Moroz Apr 07, 2009 at 03:18

For those on Linux using FF 3 (Selenium mode) -

config.selenium_browser_key =
'*firefox3 /opt/firefox/firefox-bin'

binary path may vary, space is required.


13. Ryan Bates Apr 07, 2009 at 07:23

@Rupak, webrat should already be required by the config.gem line in the test.rb environment file which is why I didn't need to require it at the top of the test_helper.rb. Did that not work for you?


14. Jacob Apr 08, 2009 at 13:22

I've been trying to get this to work but every time I try to run the test I get `test': wrong number of arguments (1 for 2) (ArgumentError).

I copied the tests from the episode so that I would have a starting point so there are no typos. I'm lost on this one.


15. Ryan Bates Apr 09, 2009 at 10:32

@Jacob, interesting. Are you using the latest version of Rails? Do you have any other testing plugins installed which may be interfering with this?


16. Marco Apr 10, 2009 at 07:56

Thanks again for showing webrat! We just started with Cucumber, but this is easier!


17. kunal Apr 11, 2009 at 13:16

First off you are really helping people adopt rails.
Excellent work, really. thanks !

Couple of questions.
when you do script/console and modified a model,
1) why don't you have to do a model.save to apply the updates to the database. Looks like it does that automatically. why ?

2) i see you do model.build how is it different from model.save or model.new ?

thanks again.


18. Michael Apr 16, 2009 at 04:25

I can´t install webrat. I always get this error

In file included from ./html_document.h:4,
                 from html_document.c:1:
./native.h:11:28: error: libxml/xmlsave.h: No such file or directory

Any idea?


19. gedoens Apr 16, 2009 at 08:26

what do i need to pass in "visit '/'" to access a page with basic_http_authentication?
i couldn't find anything on the web...


20. Macario Apr 24, 2009 at 17:30

Hi, thanks for the screencast.
The only issue I am having is with the Xpaths.

I would like to test if a page has certain title, this wont't work:

response.should have_xpath("//title{#{my_title}}")


21. jnstq May 10, 2009 at 10:01

Hi Ryan, thanks again for a great screencast. What do you prefer, webrat och cucumber for integration tests?


22. Diego Algorta May 24, 2009 at 15:57

After showing the Cucumber and raw webrat approaches for integration tests... I think it would be GREAT if you can screencast Stories, the minimalistic integration testing framework announced recently.

I think it fits all those people who think Cucumber is too much, but love Cucumber's plain english output and webrat. Take a look over here:

http://blog.citrusbyte.com/2009/05/20/stories/


23. zxcvb Jun 05, 2009 at 12:06

how can i click a button when dialog shows during test? example: when deleting element dialog shows to confirm this action, is it possible to test it with webrat? if yes, how?


24. evimser Jun 08, 2009 at 05:03

Webrat is the best!!!


25. Kang Jun 11, 2009 at 14:48

Is it possible to have webrat check for things in the popup after you click on a link?


26. mrsk4y Jun 11, 2009 at 19:04

Good work! I will use it in my project.


27. Tonypm Jun 26, 2009 at 23:22

Webrat does not currently allow use of xpath, but there is a small patch to allow this at:

https://webrat.lighthouseapp.com/projects/10503-webrat/tickets/153-within-should-support-xpath


28. Tonypm Jun 26, 2009 at 23:49

Asciicasts are great, but in this instance it is out of step. Quite different from the screencast.


29. AJ Jul 18, 2009 at 08:24

Thanks Ryan for the railscast. I am a beginner to Webrat. I tried out the step you showed in it, but i am getting an error
undefined method `title' for #<ActionView::Base:0x49390c8>

Extracted source (around line #1):

1: <% title "Log in" %>
2:
3: <p>Don't have an account? <%= link_to "Sign up!", signup_path %></p>
4:

Please let me know where the issue is. I would greatly appreciate your help.


30. БогданTTmiR Jul 24, 2009 at 03:14

that's awesome!


31. filly dee Sep 05, 2009 at 12:37

Thanks for this screencast! By the way, I tried to modify the tests so that they check for a redirect after logging in. So I used assert_redirected_to .... But now I get an error saying there was no expected redirect and just a 200 status code. Argh!!! This sucks! I want to use assert_redirected_to but I can't because something gets broke when you mix Webrat with Rspec!!! Any ideas??


32. sick Nov 30, 2009 at 22:09

Yesterday someone tell me the <a href="http://www.air-yeezy.com">air yeezy </a>shoes is not as comfortable as the <a href="http://www.buymbt.com">MBT Sport</a> shoes like <a href="http://www.air-yeezy.com/products/?catagory-c58_p1.html">kanye west louis vuitton </a>shoes, but I think if you want to feel the best comfortable, <a href="http://www.buymbt.com">MBT shoes </a> and <a href="http://www.buymbt.com"> Discount MBT shoes </a>will be the good choice,please<a href="http://www.buymbt.com"> buy mbt </a>now!
The <a href="http://www.air-yeezy.com">nike air yeezy</a> is so good too.


33. Russ Jones Dec 05, 2009 at 08:47

Holy cow, I was wondering *yesterday evening* when will you release a jQuery railscast, and voilà! Railscasts make Monday mornings enjoyable again :-)


34. javon Jan 25, 2010 at 22:25

Railscasts make Monday mornings enjoyable again


35. Burk Feb 01, 2010 at 13:17

@AJ - Rails is looking for the layout that nifty_scaffold generated. Try "script/generate nifty_layout".


36. college papers Feb 02, 2010 at 22:23

so far no issues and errors experienced. much easier than cucumber. thanks for this easy to follow tutorials and instructions.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source