Webrat: Rails Acceptance Testing Evolved

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

3 comments

Comments 1 - 3 of 3 previous next Post a comment

  • + webops Matthias Marschall 5 months ago
    Where are your specs, which use webrat, located? Mine live in spec/integration. Using the code from http://reborg.tumblr.com/post/99813407/webrat-with-rspec-no-cucumber I have it working now, the gem you mention does not (in my setup, did not give it extensive trial and error testing...)
  • + guestf387c8 guestf387c8 5 months ago
    hey webops. i got it working by installing this gem: pivotal-webrat-rspec-rails and then using require 'webrat_rspec_rails'. hope this helps
  • + webops Matthias Marschall 5 months ago
    How can I use webrat with RSpec as shown in slide 16? Just putting

    require ’webrat’
    Webrat.configure do |config|
    config.mode = :rails
    end

    into my spec_helper.rb does not work (method ’visit’ not found).

    Any ideas?
Post a comment
Embed Video
Edit your comment Cancel

12 Favorites

Webrat: Rails Acceptance Testing Evolved - Presentation Transcript

  1. Ruby Acceptance Testing for Web Applications Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  2. Bryan Helmkamp Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  3. Beer Disclaimer Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  4. Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  5. class
RepositoryTest
<
ActionController::IntegrationTest 

test
\"create
a
new
repository\"
do 



get_via_redirect
repositories_path 



assert_response
:success 



assert_select
\"a[href=?]\",
new_repository_path,
\"Create
a
new
repo\" 



 



get_via_redirect
new_repository_path 



assert_response
:success 



 



assert_select
\"form[action=?][method=post]\",
repositories_path
do 





assert_select
\"input[name=?][type=text]\",
\"repository[name]\" 





assert_select
\"input[name=?][type=text]\",
\"repository[description]\" 





assert_select
\"input[name=?][type=radio]\",
\"repository[public]\" 



end 



 



post_via_redirect
repositories_path,
:repository
=>
{ 





:name
=>
\"rack‐test\",
:public
=>
\"true\" 



} 



assert_response
:success 



assert_select
\"Repository
created.\" 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  6. class
RepositoryTest
<
ActionController::IntegrationTest 

test
\"create
a
new
repository\"
do 



get_via_redirect
repositories_path 



assert_response
:success 



assert_select
\"a[href=?]\",
new_repository_path,
\"Create
a
new
repo\" 



get_via_redirect
new_repository_path 



assert_response
:success 



assert_select
\"form[action=?][method=post]\",
repositories_path
do 





assert_select
\"input[name=?][type=text]\",
\"repository[name]\" 





assert_select
\"input[name=?][type=text]\",
\"repository[description]\" 





assert_select
\"input[name=?][type=radio]\",
\"repository[public]\" 



end
 



post_via_redirect
repositories_path,
:repository
=>
{ 





:name
=>
\"rack‐test\",
:public
=>
\"true\" 



} 



assert_response
:success 



assert_select
\"Repository
created.\" 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  7. class
RepositoryTest
<
ActionController::IntegrationTest 

test
\"create
a
new
repository\"
do 



get_via_redirect
repositories_path 



assert_response
:success 



assert_select
\"a[href=?]\",
new_repository_path,
\"Create
a
new
repo\" 



get_via_redirect
new_repository_path 



assert_response
:success 



assert_select
\"form[action=?][method=post]\",
repositories_path
do 





assert_select
\"input[name=?][type=text]\",
\"repository[name]\" 





assert_select
\"input[name=?][type=text]\",
\"repository[description]\" 





assert_select
\"input[name=?][type=radio]\",
\"repository[public]\" 



end
 



post_via_redirect
repositories_path,
:repository
=>
{ 





:name
=>
\"rack‐test\",
:public
=>
\"true\" 



} 



assert_response
:success 



assert_select
\"Repository
created.\" 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  8. class
RepositoryTest
<
ActionController::IntegrationTest 

test
\"create
a
new
repository\"
do 



get_via_redirect
repositories_path 



assert_response
:success 



assert_select
\"a[href=?]\",
new_repository_path,
\"Create
a
new
repo\" 



get_via_redirect
new_repository_path 



assert_response
:success 



assert_select
\"form[action=?][method=post]\",
repositories_path
do 





assert_select
\"input[name=?][type=text]\",
\"repository[name]\" 





assert_select
\"input[name=?][type=text]\",
\"repository[description]\" 





assert_select
\"input[name=?][type=radio]\",
\"repository[public]\" 



end
 



post_via_redirect
repositories_path,
:repository
=>
{ 





:name
=>
\"rack‐test\",
:public
=>
\"true\" 



} 



assert_response
:success 



assert_select
\"Repository
created.\" 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  9. Webrat class
RepositoryTest
<
ActionController::IntegrationTest 

test
\"create
a
new
repository\"
do 



visit
repositories_path 



click_link
\"Create
a
new
repo\" 



fill_in
\"Name\",
:with
=>
\"rack‐test\" 



click_button
\"Create\" 



assert_contain
\"Repository
created.\" 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  10. Webrat class
RepositoryTest
<
ActionController::IntegrationTest 

test
\"create
a
new
repository\"
do 



visit
repositories_path 



click_link
\"Create
a
new
repo\" 



fill_in
\"Name\",
:with
=>
\"rack‐test\" 



click_button
\"Create\" 



assert_contain
\"Repository
created.\" 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  11. Webrat class
RepositoryTest
<
ActionController::IntegrationTest 

test
\"create
a
new
repository\"
do 



visit
repositories_path 



click_link
\"Create
a
new
repo\" 



fill_in
\"Name\",
:with
=>
\"rack‐test\" 



click_button
\"Create\" 



assert_contain
\"Repository
created.\" 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  12. Webrat class
RepositoryTest
<
ActionController::IntegrationTest 

test
\"create
a
new
repository\"
do 



visit
repositories_path 



click_link
\"Create
a
new
repo\" 



fill_in
\"Name\",
:with
=>
\"rack‐test\" 



click_button
\"Create\" 



assert_contain
\"Repository
created.\" 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  13. Webrat class
RepositoryTest
<
ActionController::IntegrationTest 

test
\"create
a
new
repository\"
do 



visit
repositories_path 



click_link
\"Create
a
new
repo\" 



fill_in
\"Name\",
:with
=>
\"rack‐test\" 



click_button
\"Create\" 



assert_contain
\"Repository
created.\" 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  14. Getting started #
config/environments/test.rb config.gem
\"webrat\",
:version
=>
\">=0.4.4\" #
test/test_helper.rb Webrat.configure
do
|config| 

config.mode
=
:rails end #
sudo
rake
gems:install
RAILS_ENV=test Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  15. Webrat’s Core API #visit #click_link #fill_in #check
and
#uncheck #choose #select #attach_file #click_button Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  16. Works with RSpec describe
\"repository
management\"
do 

it
\"should
create
a
repository
with
a
name\"
do 



visit
repositories_path 



click_link
\"Create
a
new
repo\" 



fill_in
\"Name\",
:with
=>
\"rack‐test\" 



click_button
\"Create\" 



response.should
contain(\"Repository
created.\") 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  17. Works with Shoulda class
RepositoryTest
<
ActionController::IntegrationTest 

context
\"repository
management\"
do 



should
\"create
a
repository\"
do 





visit
repositories_path 





click_link
\"Create
a
new
repo\" 





fill_in
\"Name\",
:with
=>
\"rack‐test\" 





click_button
\"Create\" 





assert_contain
\"Repository
created.\" 



end 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  18. Works with Cucumber Feature:
Manage
repositories 

Scenario:
Create
repository
with
name 



When
I
create
a
repository 



Then
I
should
see
\"Repository
created.\" Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  19. Works with Cucumber When
/^I
create
a
repository$/
do 

visit
repositories_path 

click_link
\"Create
a
new
repo\" 

fill_in
\"Name\",
:with
=>
\"rack‐test\" 

click_button
\"Create\" end When
/^I
should
see
\"([^\\\"]*)\"$/
do
|text| 

response.should
contain(text) end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  20. Sinatra Application Frameworks Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  21. Matches labels to fields fill_in
\"Name\",
:with
=>
\"rack‐test\" <label
for=\"repository_name\">Name</label> <input
id=\"repository_name\"
name=\"repository[name]\"
/> Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  22. Matches labels to fields fill_in
\"Name\",
:with
=>
\"rack‐test\" <label
for=\"repository_name\">Name</label> <input
id=\"repository_name\"
name=\"repository[name]\"
/> Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  23. Matches labels to fields <label> 

<input
type=\"checkbox\"
name=\"tos\"
/> 

I
accept
the
Terms
of
Service </label> check
\"I
accept
the
Terms
of
Service\" Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  24. Matches labels to fields <label> 

<input
type=\"checkbox\"
name=\"tos\"
/> 

I
accept
the
Terms
of
Service </label> check
\"I
accept
the
Terms
of
Service\" Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  25. Webrat verifies web app behaviour Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  26. Verifies HTTP status codes 

1)
Error: test_create_a_new_repository(RepositoryTest): Webrat::PageLoadError:
Page
load
was
not
successful
(Code:
500): <snip> 



webrat
(0.4.4)
lib/webrat/core/session.rb:110:in
`request_page' 



webrat
(0.4.4)
lib/webrat/core/session.rb:205:in
`visit' 



(eval):2:in
`visit' 



/test/integration/repository_test.rb:5:in
`test_create_a_new_repository' Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  27. Verifies form fields 

1)
Error: test_create_a_new_repository(RepositoryTest): Webrat::NotFoundError:
Could
not
find
field:
\"Name\" 



webrat
(0.4.4)
lib/webrat/core/locators/locator.rb:14:in
`locate!' 



webrat
(0.4.4)
lib/webrat/core/locators/field_locator.rb:21:in
`field' 



webrat
(0.4.4)
lib/webrat/core/scope.rb:327:in
`locate_field' 



webrat
(0.4.4)
lib/webrat/core/scope.rb:50:in
`fill_in' 



(eval):2:in
`fill_in' 



/test/integration/repository_test.rb:8:in
`test_create_a_new_repository' Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  28. Verify HTML content response.should
contain(\"Hello,
world!\") response.should
have_selector(\"li\",
:class
=>
\"new\", 

:count
=>
2) response.should_not
have_xpath(\".//meta[@name
=
'robots']\") Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  29. Verify HTML content response.should
have_selector(\"#album
li:nth‐child(3)\")
do
|li|
 

li.should
have_selector(\"img\",
:src
=>
photo_path(@photo))
 

li.should
contain(\"Vacation
Photo\")
 end
 Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  30. save_and_open_page Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  31. Webrat adapters • Rails • Merb • Sinatra • Selenium • Mechanize Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  32. WWW::Mechanize require
\"webrat\" require
\"webrat/mechanize\" session
=
Webrat::MechanizeSession.new session.visit
\"http://google.com/\" session.fill_in
\"q\",
:with
=>
\"GoGaRuCo\" session.click_button
\"Google
Search\" session.dom.search(\"h3
a\").each_with_index
do
|link,
i| 

puts
\"#{i+1})
#{link[\"href\"]}\" end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  33. Selenium Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  34. #
test/test_helper.rb class
ActiveSupport::TestCase 

#
... 

self.use_transactional_fixtures
=
false 

#
... 

setup
do
|session| 



session.host!
\"localhost:3001\" 

end end 

 Webrat.configure
do
|config| 

config.mode
=
:selenium end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  35. $
rake
test:integration Started ==>
Waiting
for
Selenium
RC
server
on
port
4444...
Ready! ==>
Waiting
for
Rails
application
server
on
port
3001...
Ready! .. Finished
in
23.54011
seconds. 2
tests,
2
assertions,
0
failures,
0
errors Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  36. $
rake
test:integration Started ==>
Waiting
for
Selenium
RC
server
on
port
4444...
Ready! ==>
Waiting
for
Rails
application
server
on
port
3001...
Ready! .. Finished
in
23.54011
seconds. 2
tests,
2
assertions,
0
failures,
0
errors Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  37. $
rake
test:integration Started ==>
Waiting
for
Selenium
RC
server
on
port
4444...
Ready! ==>
Waiting
for
Rails
application
server
on
port
3001...
Ready! .. Finished
in
23.54011
seconds. 2
tests,
2
assertions,
0
failures,
0
errors Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  38. Webrat::Selenium Selenium RC Server Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  39. One method, two modes def
upload_photo 

webrat.simulate
do 



simulate_uploads 

end 

webrat.automate
do 



simulate_upload_via_javascript(\"avatar\",
\"foo.jpg\") 



response.should
contain(\"Uploading
image\") 



response.should
contain(\"successfully
uploaded\") 



click_link
\"Continue\" 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  40. One method, two modes def
upload_photo 

webrat.simulate
do 



simulate_uploads 

end 

webrat.automate
do 



simulate_upload_via_javascript(\"avatar\",
\"foo.jpg\") 



response.should
contain(\"Uploading
image\") 



response.should
contain(\"successfully
uploaded\") 



click_link
\"Continue\" 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  41. One method, two modes def
upload_photo 

webrat.simulate
do 



simulate_uploads 

end 

webrat.automate
do 



simulate_upload_via_javascript(\"avatar\",
\"foo.jpg\") 



response.should
contain(\"Uploading
image\") 



response.should
contain(\"successfully
uploaded\") 



click_link
\"Continue\" 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  42. class
MediaTest
<
ActionController::IntegrationTest 

test
\"drag
and
drop
media\"
do 



album


=
create_album
:title
=>
\"Vacation\" 



photo1

=
create_photo
:album
=>
album 



photo2

=
create_photo
:album
=>
album 



 



visit
album_path(album) 



click_link
\"Order
media\" 



 



selenium.dragdrop(\"id=#{dom_id(photo1)}\",
\"+350,
0\") 



wait_for
do 





assert
selenium.is_ordered( 







\"id=#{dom_id(photo1)}\", 







\"id=#{dom_id(photo2)}\") 



end 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  43. class
MediaTest
<
ActionController::IntegrationTest 

test
\"drag
and
drop
media\"
do 



album


=
create_album
:title
=>
\"Vacation\" 



photo1

=
create_photo
:album
=>
album 



photo2

=
create_photo
:album
=>
album 



 



visit
album_path(album) 



click_link
\"Order
media\" 



 



selenium.dragdrop(\"id=#{dom_id(photo1)}\",
\"+350,
0\") 



wait_for
do 





assert
selenium.is_ordered( 







\"id=#{dom_id(photo1)}\", 







\"id=#{dom_id(photo2)}\") 



end 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  44. class
MediaTest
<
ActionController::IntegrationTest 

test
\"drag
and
drop
media\"
do 



album


=
create_album
:title
=>
\"Vacation\" 



photo1

=
create_photo
:album
=>
album 



photo2

=
create_photo
:album
=>
album 



 



visit
album_path(album) 



click_link
\"Order
media\" 



 



selenium.dragdrop(\"id=#{dom_id(photo1)}\",
\"+350,
0\") 



wait_for
do 





assert
selenium.is_ordered( 







\"id=#{dom_id(photo1)}\", 







\"id=#{dom_id(photo2)}\") 



end 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  45. Automating a real web browser is slow Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  46. Quick demo Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  47. One More Thing... Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  48. Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  49. Rack::Test require
\"rack/test\" class
HomepageTest
<
Test::Unit::TestCase 

include
Rack::Test::Methods 

 

def
app 



MyApp.new 

end 

 

def
test_redirect_logged_in_users_to_dashboard 



authorize
\"bryan\",
\"secret\" 



get
\"/\" 



follow_redirect! 



 



assert_equal
\"http://example.org/redirected\",
last_request.url 



assert
last_response.ok? 

end end Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  50. Rack::Test API #get(uri,
params
=
{},
env
=
{}) #post,
#put,
#delete,
and
#head #request(uri,
env
=
{}) #follow_redirect! #header(name,
value) #authorize(username,
password) #last_request #last_response Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  51. Webrat adapters • Rails • Merb • Sinatra • Selenium • Mechanize Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  52. Webrat adapters • Rack::Test • Selenium • Mechanize Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  53. Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009
  54. Thanks http://www.flickr.com/photos/tambako/2908186658/ http://www.flickr.com/photos/pkmousie/2199520904/ http://www.flickr.com/photos/audreyjm529/155024495/ http://www.flickr.com/photos/acaben/541334636/ http://www.flickr.com/photos/millermz/3267766667/ Webrat brynary.com http://bit.ly/wbrt-ggrc @brynary #webrat Sunday, April 19, 2009

+ brynarybrynary, 6 months ago

custom

4352 views, 12 favs, 3 embeds more stats

Webrat slides from GoGaRuCo 2009 in San Francisco.

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 4352
    • 3880 on SlideShare
    • 472 from embeds
  • Comments 3
  • Favorites 12
  • Downloads 43
Most viewed embeds
  • 470 views on http://www.brynary.com
  • 1 views on http://blog.xuite.net
  • 1 views on http://anonymouse.org

more

All embeds
  • 470 views on http://www.brynary.com
  • 1 views on http://blog.xuite.net
  • 1 views on http://anonymouse.org

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories