Request Specã§Javascriptã®ãããã¼ã¸ããã¹ãããæ¹æ³ã Capybara + Selenium, Capybara + Akephalos ãªã©
å æ¥ãªãªã¼ã¹ãã 萌えトーク ã§ã¯End-to-Endãã¹ãã«ã¯ Request Spec + Capybara ã使ã£ã¦ãã¾ãããJavascriptã®é¨åã¯é©å½ãªãã«ãã¼ã¡ã½ããã使ãJavascriptãåä½ãããã«ãã¹ããã¾ããããCapybara ã®ãã©ã¤ãã¼ã selenium ã Akephalos (HTMLUnit)ã«å ¥ãæ¿ããäºã§å®éã«Javascriptãåãã㦠End-to-Endãã¹ã ãè¡ãäºãåºæ¥ã¾ãã
åçã¯ãWorld's Largest Rodent Born at San Diego Zoo! - ZooBorns ãã
ãã¹ã対象
Scaffoldãä½ã£ãä¸è¦§ãã¼ã¸ã«ãjQuery ã使ãShowãã¯ãªãã¯ããå ´åshowãã¼ã¸ã«é·ç§»ãããä¸è¦§ç»é¢ã®ä¸ã®æ¹ã®
- app/view/index.html.erb
<h1>Listing todos</h1> <table> <tr> <th>Due</th> <th>Task</th> <th></th> <th></th> <th></th> </tr> <% @todos.each_with_index do |todo, ix| %> <tr> <td><%= todo.due %></td> <td><%= todo.task %></td> <td><%= link_to 'Show', todo, :class=>'show_detail', :remote => true %></td> <td><%= link_to 'Edit', edit_todo_path(todo) %></td> <td><%= link_to 'Destroy', todo, :confirm => 'Are you sure?', :method => :delete %></td> </tr> <% end %> </table> <br /> <%= link_to 'New Todo', new_todo_path %> <div id="detail_area" style="margin-top:20px; border:solid 1px; width:200px"> 詳細 </div> <%= javascript_tag do %> $(".show_detail").bind("ajax:success", function(data, statusHtml, xhr) { $("#detail_area").html(statusHtml); }); <% end %>
- app/controllers/todos_controller.rb
ã³ã³ããã¼ã©ã¼ã® showã¡ã½ãã㯠layout ã使ããªãããã«ãã¾ãã
def show @todo = Todo.find(params[:id]) respond_to do |format| format.html { render :layout => false} format.xml { render :xml => @todo } end end
- app/views/layouts/application.html.erb
layout ã«ã¯ããã¡ãã jQuery ãæå®ãã¾ãã
<!DOCTYPE html> <html> <head> <title>Todo3</title> <%= stylesheet_link_tag :all %> <%= javascript_include_tag "jquery.min", "rails" %> <%= csrf_meta_tag %> </head> <body> <%= yield %> </body> </html>
Selenium
Capybaraã«ã¯Seleniumç¨ã®ãã©ã¤ãã¼ãå èµããã¦ãã¦ãè¨å®ãå¤æ´ããã ã㧠Selenium ã使ã£ã¦ãã¹ããè¡ãã¾ãããåç¥ã®ããã«Selenuium ã¯å®éã®ãã©ã¦ã¶ã¼(ããã©ã«ãã¯Firefox)ãèµ·åããã©ã¦ã¶ã¼ä¸ã§ãã¹ããå®è¡ããã¾ãã®ã§ããã¹ãã«ã¯æéãæããã¾ãã
- spec/requests/show_feature_spec.rb
ãã¹ãã¯ä»¥ä¸ã®æ§ã«ã表示ã¨ãªã¢ div#detail_area ã«Todo表示ããæååãå¨ãããæ¤è¨¼ãã¦ãã¾ãã
ããã§ãdescribe ã« :js => true ã¨ãªãã·ã§ã³ãæå®ããã¦ããã®ã¯ãã®ãã¹ã㯠Javascript ã使ããã¦ããäºãããããã¦ãã¦ããã®ãªãã·ã§ã³ããªããã¹ãã¯é常㮠rack_test ã§å®è¡ããã¾ãã
require 'spec_helper' describe "Todo詳細ã®è¡¨ç¤º", :js => true do fixtures :todos it "showãã¯ãªãã¯ããã¨listãã¼ã¸ä¸é¨ã«è©³ç´°ãã表示ã§ãã" do visit list_page page.find("table tr:eq(2) a:eq(1)").click page.should have_css "#detail_area", :text=>"2011-02-18" page.should have_css "#detail_area", :text=>"æã¡åãã" end end
- spec/spec_helper.rb
ä¸ã§æ¸ãã :js => true ãæå¹ã«ãã¦ããã®ã¯ spec_helper.rb ã«æ¸ãã以ä¸ã®è¨å®ã§ãããã®è¨å®ã§ :js => true ã®ãããã¹ãã®åã«ã¯ãã¹ããè¡ããã©ã¤ãã¼ã Selenium ã«åæ¿ãçµäºãããããã©ã«ã(rack_test)ã«æ»ãã¦ãã¾ãã
ããã RSpec.configure do |config| ããã config.before(:all, :js => true) do Capybara.current_driver = :selenium end config.after(:all, :js => true) do Capybara.use_default_driver end ããã end
Akephalos
Seleniumã使ã£ããã¹ãã¯å®éã®ãã©ã¦ã¶ã¼ã使ãã®ã§ããã¹ãã¨ãã¦å®ç§ã§ããæéãããããç»é¢ã®ãªã(Xãèµ·åãã¦ãªãLinuxç)ãµã¼ãã¼çã§ã¯å®è¡ã§ãã¾ãã(ä½ãæãããã®ããªï¼)ã
Akephalos 㯠Java製ã®Webã¢ããªãã¹ãç¨ã®ãã¬ã¼ã ã¯ã¼ã¯ HTMLUnit ã§Javaã§å®è£
ãããJavascriptã¤ã³ã¿ããªã¿ã¼ Rhino ã使ãJavascriptãå«ããã¹ããè¡ãã Capybaraç¨ãã©ã¤ãã¼ã§ãã HTMLUnitãRhino㯠Javaã§æ¸ããã¦ãã¾ããããããã¨ã¯Drbã使ãéä¿¡ãã¦ããããã§ãã
ãã¹ãã®ã³ã¼ã spec/requests/show_feature_spec.rb ã¯selenuim ã¨åãã§ãããakephalos ã®ã¤ã³ã¹ãã¼ã«ã¨ãã©ã¤ãã¼ã :akephalos ã«ããå¿ è¦ãããã¾ãã
- Gemfile
group :test, :development do gem "rspec-rails", ">= 2.5.0" gem 'email_spec' gem 'capybara' gem 'spork' gem 'akephalos' end
- spec/spec_helper.rb
require 'akephalos' ããã RSpec.configure do |config| ããã config.before(:all, :js => true) do Capybara.current_driver = :akephalos end config.after(:all, :js => true) do Capybara.use_default_driver end ããã end
Akephalos ã«ãããã¹ã㯠javaã® HTMLUnit/Rhinoã®èµ·åã«æéãããããä»åã®ãµã³ãã«ã§ã¯ Selenium ããæéãããã£ã¦ãã¾ãã¾ããã HTMLUnit/Rhinoãèµ·åãã¦ãã¾ãã°ãã¹ãèªä½ã¯ Selenuim ããæ©ãããã§ãã
Cucumber+Capybara+akephalosを使ってみた - ryog on rails ã«Cucumber + Selenium 㨠Cucumber + Akephalos ã®å®è¡æéã®æ¯è¼ãããã¾ãã
env.js
Capybaraã§Javascriptãåãã MRI(CRuby) ã§ä½¿ãããã®ã¨ã㦠env.js ã¨ãã Rhino+JavaScript+Rubyã®ãã©ã¤ãã¼ãããã¾ãããä»åã®ãµã³ãã«ã§ã¯åãã¾ããã§ããã ã¾ã Safari/Chromeã®ã¨ã³ã¸ã³ WebKitã使ã£ã capybara-webkit ãªã©ãããããã§ãããã®ã¸ãã¯ããããã楽ãã¿ã§ããï¼
ä»åã WEB+DB PRESS Vol.61ã® ãRails 3ãã¹ãæåç·ããããã¸ãå½¹ã«ãã¡ã¾ããããã¡ãã jnicklas/capybara - GitHub ãã
- ä½è : 西岡ç¥å¼¥,濱ç°ç« å¾,横山彰å,æµæ¬éç,ããã¯,uupaa,å¡ä¸å¿å¤«,ã¯ã¾ã¡ã2,大沢åå®,ä¸å³¶è¡,ç¢éãã,ä¸å³¶æ,浦å¶å太,è§ç°ç´è¡,ä½ã æ¨ä¸,åäºé¾å¤ªé,æ·±çºè±å¤ªé,岩永賢æ,é«æ©å¥ä¸,æ´ç°åå¿,äºä¸èª ä¸é,大谷å¼å,è»éæ·³ä¹,åæ ,å¢äºä¿ä¹,WEB+DB PRESSç·¨éé¨
- åºç社/ã¡ã¼ã«ã¼: æè¡è©è«ç¤¾
- çºå£²æ¥: 2011/02/24
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 37人 ã¯ãªãã¯: 2,058å
- ãã®ååãå«ãããã° (38件) ãè¦ã