+ All Categories
Home > Technology > Webrat: Rails Acceptance Testing Evolved

Webrat: Rails Acceptance Testing Evolved

Date post: 18-May-2015
Category:
Upload: brynary
View: 6,923 times
Download: 0 times
Share this document with a friend
Description:
Webrat slides from GoGaRuCo 2009 in San Francisco.
Popular Tags:
54
Webrat brynary.com @brynary #webrat http://bit.ly/wbrt-ggrc Ruby Acceptance Testing for Web Applications Sunday, April 19, 2009
Transcript
Page 1: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrcWebrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Ruby Acceptance Testing for Web Applications

Sunday, April 19, 2009

Page 2: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrcWebrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Bryan Helmkamp

Sunday, April 19, 2009

Page 3: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Beer Disclaimer

Sunday, April 19, 2009

Page 4: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Sunday, April 19, 2009

Page 5: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

classRepositoryTest<ActionController::IntegrationTesttest"createanewrepository"doget_via_redirectrepositories_pathassert_response:successassert_select"a[href=?]",new_repository_path,"Createanewrepo"get_via_redirectnew_repository_pathassert_response:successassert_select"form[action=?][method=post]",repositories_pathdoassert_select"input[name=?][type=text]","repository[name]"assert_select"input[name=?][type=text]","repository[description]"assert_select"input[name=?][type=radio]","repository[public]"endpost_via_redirectrepositories_path,:repository=>{:name=>"rack‐test",:public=>"true"}assert_response:successassert_select"Repositorycreated."endend

Sunday, April 19, 2009

Page 6: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

classRepositoryTest<ActionController::IntegrationTesttest"createanewrepository"doget_via_redirectrepositories_pathassert_response:successassert_select"a[href=?]",new_repository_path,"Createanewrepo"

get_via_redirectnew_repository_pathassert_response:success

assert_select"form[action=?][method=post]",repositories_pathdoassert_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_redirectrepositories_path,:repository=>{:name=>"rack‐test",:public=>"true"}assert_response:successassert_select"Repositorycreated."endend

Sunday, April 19, 2009

Page 7: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

classRepositoryTest<ActionController::IntegrationTesttest"createanewrepository"doget_via_redirectrepositories_pathassert_response:successassert_select"a[href=?]",new_repository_path,"Createanewrepo"

get_via_redirectnew_repository_pathassert_response:success

assert_select"form[action=?][method=post]",repositories_pathdoassert_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_redirectrepositories_path,:repository=>{:name=>"rack‐test",:public=>"true"}assert_response:successassert_select"Repositorycreated."endend

Sunday, April 19, 2009

Page 8: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

classRepositoryTest<ActionController::IntegrationTesttest"createanewrepository"doget_via_redirectrepositories_pathassert_response:successassert_select"a[href=?]",new_repository_path,"Createanewrepo"

get_via_redirectnew_repository_pathassert_response:success

assert_select"form[action=?][method=post]",repositories_pathdoassert_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_redirectrepositories_path,:repository=>{:name=>"rack‐test",:public=>"true"}assert_response:successassert_select"Repositorycreated."endend

Sunday, April 19, 2009

Page 9: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

classRepositoryTest<ActionController::IntegrationTesttest"createanewrepository"dovisitrepositories_pathclick_link"Createanewrepo"fill_in"Name",:with=>"rack‐test"click_button"Create"assert_contain"Repositorycreated."endend

Webrat

Sunday, April 19, 2009

Page 10: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

classRepositoryTest<ActionController::IntegrationTesttest"createanewrepository"dovisitrepositories_pathclick_link"Createanewrepo"fill_in"Name",:with=>"rack‐test"click_button"Create"assert_contain"Repositorycreated."endend

Webrat

Sunday, April 19, 2009

Page 11: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

classRepositoryTest<ActionController::IntegrationTesttest"createanewrepository"dovisitrepositories_pathclick_link"Createanewrepo"fill_in"Name",:with=>"rack‐test"click_button"Create"assert_contain"Repositorycreated."endend

Webrat

Sunday, April 19, 2009

Page 12: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

classRepositoryTest<ActionController::IntegrationTesttest"createanewrepository"dovisitrepositories_pathclick_link"Createanewrepo"fill_in"Name",:with=>"rack‐test"click_button"Create"assert_contain"Repositorycreated."endend

Webrat

Sunday, April 19, 2009

Page 13: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

classRepositoryTest<ActionController::IntegrationTesttest"createanewrepository"dovisitrepositories_pathclick_link"Createanewrepo"fill_in"Name",:with=>"rack‐test"click_button"Create"assert_contain"Repositorycreated."endend

Webrat

Sunday, April 19, 2009

Page 14: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Getting started

#config/environments/test.rbconfig.gem"webrat",:version=>">=0.4.4"

#test/test_helper.rbWebrat.configuredo|config|config.mode=:railsend

#sudorakegems:installRAILS_ENV=test

Sunday, April 19, 2009

Page 15: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Webrat’s Core API#visit#click_link#fill_in#checkand#uncheck#choose#select#attach_file#click_button

Sunday, April 19, 2009

Page 16: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

describe"repositorymanagement"doit"shouldcreatearepositorywithaname"dovisitrepositories_pathclick_link"Createanewrepo"fill_in"Name",:with=>"rack‐test"click_button"Create"response.shouldcontain("Repositorycreated.")endend

Works with RSpec

Sunday, April 19, 2009

Page 17: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

classRepositoryTest<ActionController::IntegrationTestcontext"repositorymanagement"doshould"createarepository"dovisitrepositories_pathclick_link"Createanewrepo"fill_in"Name",:with=>"rack‐test"click_button"Create"assert_contain"Repositorycreated."endendend

Works with Shoulda

Sunday, April 19, 2009

Page 18: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Works with Cucumber

Feature:Managerepositories

Scenario:Createrepositorywithname

WhenIcreatearepositoryThenIshouldsee"Repositorycreated."

Sunday, April 19, 2009

Page 19: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Works with CucumberWhen/^Icreatearepository$/dovisitrepositories_pathclick_link"Createanewrepo"fill_in"Name",:with=>"rack‐test"click_button"Create"end

When/^Ishouldsee"([^\"]*)"$/do|text|response.shouldcontain(text)end

Sunday, April 19, 2009

Page 20: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Application Frameworks

Sinatra

Sunday, April 19, 2009

Page 21: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Matches labels to fields

<labelfor="repository_name">Name</label><inputid="repository_name"name="repository[name]"/>

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

Sunday, April 19, 2009

Page 22: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Matches labels to fields

<labelfor="repository_name">Name</label><inputid="repository_name"name="repository[name]"/>

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

Sunday, April 19, 2009

Page 23: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Matches labels to fields

<label><inputtype="checkbox"name="tos"/>IaccepttheTermsofService</label>

check"IaccepttheTermsofService"

Sunday, April 19, 2009

Page 24: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Matches labels to fields

<label><inputtype="checkbox"name="tos"/>IaccepttheTermsofService</label>

check"IaccepttheTermsofService"

Sunday, April 19, 2009

Page 25: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Webrat verifies web app behaviour

Sunday, April 19, 2009

Page 26: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Verifies HTTP status codes

1)Error:test_create_a_new_repository(RepositoryTest):Webrat::PageLoadError:Pageloadwasnotsuccessful(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'

Sunday, April 19, 2009

Page 27: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Verifies form fields

1)Error:test_create_a_new_repository(RepositoryTest):Webrat::NotFoundError:Couldnotfindfield:"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'

Sunday, April 19, 2009

Page 28: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Verify HTML content

response.shouldcontain("Hello,world!")

response.shouldhave_selector("li",:class=>"new",:count=>2)

response.should_nothave_xpath(".//meta[@name='robots']")

Sunday, April 19, 2009

Page 29: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Verify HTML content

response.shouldhave_selector("#albumli:nth‐child(3)")do|li|li.shouldhave_selector("img",:src=>photo_path(@photo))li.shouldcontain("VacationPhoto")end

Sunday, April 19, 2009

Page 30: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrcWebrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

save_and_open_page

Sunday, April 19, 2009

Page 31: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Webrat adapters

• Rails

• Merb

• Sinatra

• Selenium

• Mechanize

Sunday, April 19, 2009

Page 32: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

WWW::Mechanizerequire"webrat"require"webrat/mechanize"

session=Webrat::MechanizeSession.newsession.visit"http://google.com/"session.fill_in"q",:with=>"GoGaRuCo"session.click_button"GoogleSearch"

session.dom.search("h3a").each_with_indexdo|link,i|puts"#{i+1})#{link["href"]}"end

Sunday, April 19, 2009

Page 33: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Selenium

Sunday, April 19, 2009

Page 34: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

#test/test_helper.rbclassActiveSupport::TestCase#...self.use_transactional_fixtures=false

#...

setupdo|session|session.host!"localhost:3001"endendWebrat.configuredo|config|config.mode=:seleniumend

Sunday, April 19, 2009

Page 35: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

$raketest:integrationStarted==>WaitingforSeleniumRCserveronport4444...Ready!==>WaitingforRailsapplicationserveronport3001...Ready!..Finishedin23.54011seconds.

2tests,2assertions,0failures,0errors

Sunday, April 19, 2009

Page 36: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

$raketest:integrationStarted==>WaitingforSeleniumRCserveronport4444...Ready!==>WaitingforRailsapplicationserveronport3001...Ready!..Finishedin23.54011seconds.

2tests,2assertions,0failures,0errors

Sunday, April 19, 2009

Page 37: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

$raketest:integrationStarted==>WaitingforSeleniumRCserveronport4444...Ready!==>WaitingforRailsapplicationserveronport3001...Ready!..Finishedin23.54011seconds.

2tests,2assertions,0failures,0errors

Sunday, April 19, 2009

Page 38: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Webrat::Selenium

Selenium RC Server

Sunday, April 19, 2009

Page 39: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

One method, two modesdefupload_photowebrat.simulatedosimulate_uploadsend

webrat.automatedosimulate_upload_via_javascript("avatar","foo.jpg")response.shouldcontain("Uploadingimage")response.shouldcontain("successfullyuploaded")click_link"Continue"endend

Sunday, April 19, 2009

Page 40: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

defupload_photowebrat.simulatedosimulate_uploadsend

webrat.automatedosimulate_upload_via_javascript("avatar","foo.jpg")response.shouldcontain("Uploadingimage")response.shouldcontain("successfullyuploaded")click_link"Continue"endend

One method, two modes

Sunday, April 19, 2009

Page 41: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

defupload_photowebrat.simulatedosimulate_uploadsend

webrat.automatedosimulate_upload_via_javascript("avatar","foo.jpg")response.shouldcontain("Uploadingimage")response.shouldcontain("successfullyuploaded")click_link"Continue"endend

One method, two modes

Sunday, April 19, 2009

Page 42: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

classMediaTest<ActionController::IntegrationTesttest"draganddropmedia"doalbum=create_album:title=>"Vacation"photo1=create_photo:album=>albumphoto2=create_photo:album=>albumvisitalbum_path(album)click_link"Ordermedia"selenium.dragdrop("id=#{dom_id(photo1)}","+350,0")wait_fordoassertselenium.is_ordered("id=#{dom_id(photo1)}","id=#{dom_id(photo2)}")endendend

Sunday, April 19, 2009

Page 43: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

classMediaTest<ActionController::IntegrationTesttest"draganddropmedia"doalbum=create_album:title=>"Vacation"photo1=create_photo:album=>albumphoto2=create_photo:album=>albumvisitalbum_path(album)click_link"Ordermedia"selenium.dragdrop("id=#{dom_id(photo1)}","+350,0")wait_fordoassertselenium.is_ordered("id=#{dom_id(photo1)}","id=#{dom_id(photo2)}")endendend

Sunday, April 19, 2009

Page 44: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

classMediaTest<ActionController::IntegrationTesttest"draganddropmedia"doalbum=create_album:title=>"Vacation"photo1=create_photo:album=>albumphoto2=create_photo:album=>albumvisitalbum_path(album)click_link"Ordermedia"selenium.dragdrop("id=#{dom_id(photo1)}","+350,0")wait_fordoassertselenium.is_ordered("id=#{dom_id(photo1)}","id=#{dom_id(photo2)}")endendend

Sunday, April 19, 2009

Page 45: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Automating a real web browser is slow

Sunday, April 19, 2009

Page 46: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Quick demo

Sunday, April 19, 2009

Page 47: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

One More Thing...

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Sunday, April 19, 2009

Page 48: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Sunday, April 19, 2009

Page 49: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Rack::Testrequire"rack/test"

classHomepageTest<Test::Unit::TestCaseincludeRack::Test::MethodsdefappMyApp.newenddeftest_redirect_logged_in_users_to_dashboardauthorize"bryan","secret"get"/"follow_redirect!assert_equal"http://example.org/redirected",last_request.urlassertlast_response.ok?endend

Sunday, April 19, 2009

Page 50: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

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

Sunday, April 19, 2009

Page 51: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Webrat adapters

• Rails

• Merb

• Sinatra

• Selenium

• Mechanize

Sunday, April 19, 2009

Page 52: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Webrat adapters

• Rack::Test

• Selenium

• Mechanize

Sunday, April 19, 2009

Page 53: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

Sunday, April 19, 2009

Page 54: Webrat: Rails Acceptance Testing Evolved

Webrat brynary.com@brynary #webrathttp://bit.ly/wbrt-ggrc

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/

Thanks

Sunday, April 19, 2009


Recommended