Integration TestCucumber + Webrat + Selenium
2010年4月13日星期二
about me
• 呂旺燊 a.k.a tka, tkalu
• http://www.twitter.com/tkalu
• http://www.tkalu.com
• 任職於和多
http://www.handlino.com
2010年4月13日星期二
Ruby Tuesday 前情提要
gugod
#6 Integration Test With Cucumber And Webrat
weijen
#3 如何撰寫 rspec 測試,讓你的程式能穩固地運作Mock Aren't Stub 讀後心得
2010年4月13日星期二
Source Code
http://github.com/tka/it-cws-demo
2010年4月13日星期二
Cucumber 能做什麼
用
一般人類看得懂的語言
來描述要測試的事情
2010年4月13日星期二
Cucumber 看起來像
場景: 未登入使用者開啟首頁 假設 我是一位未登入使用者 當 我開啟了網站首頁 那麼 我應該看到"您好,歡迎光臨"
2010年4月13日星期二
Webrat http://gitrdoc.com/brynary/webrathttp://github.com/brynary/webrat
2010年4月13日星期二
Webrat 寫起來像
visit login_url
fill_in “Login”, :with => “User”
fill_in “Password”, :with => “123456”
click_button “Log in”
http://gitrdoc.com/brynary/webrat/tree/master
2010年4月13日星期二
Cucumber + Webrat 合體技
Given /^我是一位未登入使用者$/ do visit logout_pathend
And /^我開啟了網站首頁$/ do visit root_pathend
Then /^我應該看到"([^\"]*)"$/ do | text |assert_contain text
end2010年4月13日星期二
Cucumber + Webrat
• cucumber -f pretty features/p5.feature
• cucumber -f html \
features/p5.feature > test.html
2010年4月13日星期二
雖然很厲害了, 但是....
不支援 javascript
2010年4月13日星期二
招喚真正的Browser吧
2010年4月13日星期二
Selenium 家族
• Selenium IDE
• elenium Remote Control (RC)
• Selenium Grid
2010年4月13日星期二
Selenium 支援的平台
• Linux
• OS X
• Windows
• FireFox
• Chrome
• Safari
• Opera
• IE 7,8 (6 not in the list)
2010年4月13日星期二
Selenium & Ruby
• gem install selenium-client• require "selenium/client"
2010年4月13日星期二
Selenium 寫起來像@browser = Selenium::Client::Driver.new \ :host => "localhost", :port => 4444, :browser => "*firefox", :url => "http://www.google.com", :timeout_in_second => 60
@[email protected] "/"@browser.type "q", "Ruby Tuesday #11"@browser.click "btnG", :wait_for => :[email protected]_entire_page_screenshot('/Users/tka/1.png','')puts @browser.text?("Ruby Tuesday #11 |Registrano")
http://selenium-client.rubyforge.org/
ruby script/selenium_demo.rb2010年4月13日星期二
Webrat + Selenium 合體技Webrat.configure do |config| config.mode = :selenium config.selenium_server_address = 'localhost' config.application_framework = :externalend
visit 'http://www.google.com'fill_in "q", :with => 'Ruby Tuesday #11'click_button "Google 搜尋"selenium.wait_for_page_to_load(10)assert_contain "Ruby Tuesday #11 | Registrano"save_and_open_screengrab
ruby script/webrat_selenium_demo.rb2010年4月13日星期二
Cucumber + Webrat + Selenium
features/support/env.rb 添加Webrat.configure do |config| config.mode = :rails config.open_error_files = false
if ENV["USE_SELENIUM"] config.mode = :selenium config.selenium_browser_key = ENV["SELENIUM_BROWSER"] || "*firefox" if ENV["SELENIUM_SERVER"] config.selenium_server_address = ENV["SELENIUM_SERVER"] end config.application_address = ENV["SELENIUM_APPLICATION"] || "localhost" config.application_port = ENV["SELENIUM_APPLICATION_PORT"] || "3000" endend
USE_SELENIUM=1 cucumber2010年4月13日星期二
或是使用Cucumber profileconfig/cucumber.yml
selenium: -r features/support/env.rb \ -r features/support/enhanced.rb \ -r features/step_definitions features/enhanced
執行 cucumber -p selenium
http://wiki.github.com/aslakhellesoy/cucumber/setting-up-selenium
2010年4月13日星期二
VirtualBox 簡介
• 類似Vmware
• 提供 VBoxManage cli介面
• OSX 會在 /Applications/VirtualBox.app/Contents/MacOS/VBoxManage
• 可以透過指令喚醒或是關閉
2010年4月13日星期二
世界還是沒有那麼美好
2010年4月13日星期二
webrat-0.7.0/lib/webrat/selenium/location_strategy_javascript/label.js
var labelFor = locatedLabel.getAttribute('for');
// IE--if (labelFor == null && "\v"=="v"){ labelFor=locatedLabel.htmlFor}
if (labelFor == null && locatedLabel.hasChildNodes()) {
2010年4月13日星期二
最終自動化步驟
• cronjob 每天半夜
• 喚醒 VM
• 確認 VM 上的 selenium 啟動
• 執行 cucumber + webrat + selenium
• 檢視測試結果, 有錯誤則寄出通知
2010年4月13日星期二
Q&A
2010年4月13日星期二