#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 48 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. 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 :-)


33. Burk Feb 01, 2010 at 13:17

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


34. 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.


35. http://hotwallpapers4u.com/ Apr 07, 2010 at 13:12

Now I see this:
Sun 08 de noviembre 13:17 / pt/tt11 maco $ script / cucumber Selenium-p
Feature: Home
So I can feel welcome as general user, I want to see a welcome page with 2 columns @ Scenario selenium: Show the Welcome page General features a user # / welcome_page.feature: 18 Taking into account the server is up # Equipment and step_definitions / welcome_page_steps.rb: 3 And I'm not connected to characteristics # / step_definitions / welcome_page_steps.rb: 7


36. acne clearing May 02, 2010 at 00:19

Webrat very practical. In the work often requires very efficient way, I think I have found. Thank you for sharing.


37. move to australia May 27, 2010 at 09:15

followed it and eventually implemented the lesson, gosh i think I need a lay down now. move over cat I'm having a nap now.


38. jeep Jun 08, 2010 at 19:02

jhkuhk


39. bridesmaid dreses Jun 12, 2010 at 01:52

The post is very nicely written and it contains many useful facts. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement.


40. ブランドコピー時計 Jun 19, 2010 at 07:54

ブランドコピー時計,http://www.brandcopywatch88.com
Hot Tags:ブランドコピー時計,ブランドコピー,スーパーコピー,ブランドコピー腕時計,ロレックスコピー,ブランドコピー販売
ホーム:http://www.brandcopywatch88.com
連絡メールアドレス:brandwatch999@gmail.com


41. ブランドコピー財布 Jun 19, 2010 at 07:55

ブランドコピー財布,www.brandcopyshop.net
hot tags: ブランドコピー,ブランドコピー財布,スーパーコピー,ルイヴィトンコピー
メールアドレス:brandbags8@gmail.com


42. 山崎雪子 Jun 25, 2010 at 01:31

4687812345188 お世話になっております。shopping-brandです! 
本日の情報メールは、ルイ ヴィトン 、グッチ 、エルメス、
シャネル、コーチ 、ブルガリ、クロエ 、プラダ 、カルティエ、腕時計新品が
到店しました。.
会社の商品は現在取引において信用は良好で、
数多くのお客様に安心して、商品の購入をしていたたいております.
━…━…━…━…━…━…━…━…━…━…━…
◇激安で通販が買える!他では見当たらないものや、圧倒的な量の商品を激安
で紹介しています。
◆通販のお店は通信販売サイト専門店でお買い得☆激安商品を探しましょう。
◇通販ならではの、プレゼント、直販、即納、新作、送料無料、
◆財布新作、人気モデル、中古、ディスカウント、激安商品をゲットしよう!
■URL:http://www.shopping-brand.com/
http://www.shopping-brand.com/categories-c-30.html
http://www.shopping-brand.com/categories-c-19.html
http://www.shopping-brand.com/categories-c-34.html
■店長:山崎雪子
■連絡先:virgoods1@yahoo.co.jp


43. mail Jun 28, 2010 at 14:07

David Heinemeier Hansson


44. free card sharing Jun 28, 2010 at 14:07

Please continue with these testing screencasts.
Thanks :-)


45. cardsharing free server Jun 28, 2010 at 14:08

thank you ver mucuks :)


46. essay writing help Jul 20, 2010 at 08:01

Please.. I would like to use some information from your blog from my research paper if you don't mind...I will refere to you in it. thanks


47. DVD to iPad Converter Jul 25, 2010 at 19:06

this is the best ah ha


48. tiffany Earrings sale Jul 27, 2010 at 02:39

good article,it is usefull informations again,thnx!


49. 漢方薬 Jul 29, 2010 at 02:20

漢方薬
精力剤、媚薬、ED薬、漢方薬の老舗「漢方世界」「信用第一」、当店の製品は全てメーカーより直接仕入れる100%保証の純製品です。【☆ www.kanpouworld.com ☆】。

☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆* ☆ * ☆

VEGETAL VIGRA Ⅱの説明
世界中の精力剤愛用者で、この名前を知らない人はいないくらい有名な商品です。
VEGETAL VIGRA(ベジタルバイグラ)は、男性が性的興奮により勃起するメカニズムはペニス中の化学物質Cyclic GMPの働きと考えられていますがED患者はGMPの分泌が少ない、もしくはGMPを破壊する酵素の分泌量が多いため十分な勃起を維持できませんこのような事実に基づいて製造されたスーパー精力剤がこの商品。http://www.kanpouworld.com/04/17.html

超級雄覇(スーパー雄覇)の説明
人参、冬虫夏草、各種動物のペニス、タツノオトシゴのコンビネーションパワー!
この超級雄覇は最新の漢方医学と科学の技術を融合させ,様々な希少漢方生薬を配合して作られた精力増強剤です。抗疲労等の作用もあり、体内分泌のバランスを調節し虚弱体質を改善します。
効能:ED、早漏、虚弱体質、免疫力低下の改善http://www.kanpouworld.com/04/22.html

☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆ * ☆* ☆ * ☆


50. 漢方世界 Jul 29, 2010 at 23:31

漢方世界
精力剤、精力増強剤、漢方精力剤、男性用精力剤、男性用性欲剤、媚薬、女性用媚薬、女性用催情剤、女性用性欲剤、ED薬、男性用ED薬【www.kanpouworld.com】

☆.:・'゜☆.:・'゜☆.:・'゜☆.:・'゜☆.:・'゜☆.:・'゜☆.:・'゜☆.:・'゜☆.:・'゜☆

威哥王(ウェイカワン)の説明
中国バイアグラ・パワーアップ 威哥王ウェイかワンでSEXに自信ある男に大変身!見事復活風俗嬢も喜ぶ!
2005年もこの精力剤・威哥王(ウェイカワン)が上海で圧倒的人気である。滋養強壮作用・性機能の強化向上・勃起力・持続力の強化。威哥王の本家「錦州中美」の製品で、かずある威哥王のなかでも最強の威哥王です。
この威哥王(ウェイカワン)の爆発的パワーを一度体感してください!http://www.kanpouworld.com/04/14.html


53. battery clip Aug 09, 2010 at 00:47

It's a very meaningful activity. Looking forward to joining you.


54. free directory list Aug 11, 2010 at 22:34

I really appreciate what you post.


55. natural ways to burn belly fat Aug 14, 2010 at 06:01

It's a very meaningful activity. Looking forward to joining you.


56. 漢方精力剤 Aug 15, 2010 at 21:01

漢方精力剤
精力剤、精力増強剤、漢方精力剤、男性用精力剤、男性用性欲剤、媚薬、女性用媚薬、女性用催情剤、女性用性欲剤、ED薬、男性用ED薬【www.kanpouworld.com】

☆.:・'゜☆.:・'゜☆.:・'゜☆.:・'゜☆.:・'゜☆.:・'゜☆.:・'゜☆.:・'゜☆.:・'゜☆
超能持久(超パワー耐久)の説明
チベットの秘薬、宝くじ!持続時間なんと120時間!ペニスが大きくなる!太くなる!
時間が延びる!精子を生かす!ニュージーランドのハイテク!安全でスーパーパワー。純漢方成分 http://www.kanpouworld.com/04/04.html

香港版蟻力神の説明
この蟻力神は、すでに製造中止となった「沈陽長港蟻宝」の蟻力神とは異なり、製造メーカーの違う香港の蟻力神です。http://www.kanpouworld.com/04/03.html

◆ 「漢方世界」精力剤、媚薬、ED薬、漢方薬の老舗「信用第一」
◆ 当店の製品は全てメーカーより直接仕入れる100%保証の純製品です。
◆ 秘密厳守、完全二重包装、商品名は記載しておりません。
◆ 国際EMS便で最短3日でのお届け、ご入金後当日発送となります。
◆ 商品の発送状況を24時間確認できます。


57. wholesale new era hats Aug 20, 2010 at 20:41

The dissertation chapters should be perfectly composed by distinguished thesis service, when people want to present a writing talent. Thus, this is manifestly that you understand the right way to finish a superb issue referring to this topic. Thank you very much for distributing this.


58. Jimmy Choo sale Aug 21, 2010 at 00:55

Thank you for sharing!


59. Air Jordan Force 4 Aug 24, 2010 at 22:53

Fantastic screencast! Helped me refactor some similar stuff I had been doing with Rails and jQuery. i love your articles youre always giving me great ideas on how to progress with my blog. thanks a lot for keeping us informed.


60. louis vuitton shoes Aug 26, 2010 at 23:20

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


61. Wholesale Electronics Aug 27, 2010 at 00:01

Discount Wholesale Electronics, Wholesale Cell Phones, Electronic Gadgets and More from the Best Dropship Wholesaler


62. Indonesia Furniture Handicraft Wholesale Marketplace Aug 28, 2010 at 19:26

i agree with you, this is very helpful


63. My Blogpost Aug 28, 2010 at 19:27

this is what i have search for a while. i found here.. many thanks to author


64. vuitton neverfull Aug 29, 2010 at 22:56

I really appreciate what you post.


65. snow boots Aug 30, 2010 at 21:04

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


66. levis belts Sep 01, 2010 at 20:36

Thanks for sharing your article.


67. batteries Sep 02, 2010 at 07:35

i love your articles youre always giving me great ideas on how to progress with my blog. thanks a lot for keeping us informed.

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