public
Created

  • Download Gist
selenium-on-sauce.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
require "rubygems"
require "test/unit"
gem "selenium-client", ">=1.2.16"
require "selenium/client"
require 'threadify'
 
class ExampleTest < Test::Unit::TestCase
 
#SELENIUM_BROWSERS=all
def browsers
if ENV['SELENIUM_BROWSERS'] == 'all'
browser_json = '{"username": "danmayer",' +
'"access-key": "SAUCE-LABS-ID-KEY-XXX",' +
'VERSION' +
'"job-name": "Caliper, basic walkthrough"}'
#we can run all browsers, but it uses sauce labs minutes
[browser_json.gsub(/VERSION/, '"os": "Windows 2003", "browser": "firefox", "browser-version": "3.0",'),
browser_json.gsub(/VERSION/, '"os": "Windows 2003", "browser": "iexplore", "browser-version": "6.",'),
browser_json.gsub(/VERSION/, '"os": "Windows 2003", "browser": "iexplore", "browser-version": "7.",'),
browser_json.gsub(/VERSION/, '"os": "Windows 2003", "browser": "iexplore", "browser-version": "8.",'),
browser_json.gsub(/VERSION/, '"os": "Windows 2003", "browser": "firefox", "browser-version": "2.",'),
browser_json.gsub(/VERSION/, '"os": "Windows 2003", "browser": "firefox", "browser-version": "3.6.",'),
browser_json.gsub(/VERSION/, '"os": "Windows 2003", "browser": "safari", "browser-version": "3.",'),
browser_json.gsub(/VERSION/, '"os": "Windows 2003", "browser": "safari", "browser-version": "4.",'),
browser_json.gsub(/VERSION/, '"os": "Windows 2003", "browser": "opera", "browser-version": "9.",'),
browser_json.gsub(/VERSION/, '"os": "Windows 2003", "browser": "opera", "browser-version": "10.",'),
browser_json.gsub(/VERSION/, '"os": "Windows 2003", "browser": "googlechrome", "browser-version": "",'),
browser_json.gsub(/VERSION/, '"os": "Linux", "browser": "firefox", "browser-version": "3.0.",')
]
else
["*chrome"]
end
end
#SELENIUM_URL=http://caliper-staging.heroku.com/
def test_url
ENV['SELENIUM_URL'] ? ENV['SELENIUM_URL'] : 'http://localhost:3000'
end
#SELENIUM_RC_URL=saucelabs.com
def selenium_rc_url
ENV['SELENIUM_RC_URL'] ? ENV['SELENIUM_RC_URL'] : 'localhost'
end
def run_in_all_browsers(&block)
if browsers.length > 1
errors = []
browsers.threadify(browsers.length) do |browser_spec|
begin
run_browser(browser_spec, block)
rescue => error
type = browser_spec.match(/browser\": \"(.*)\", /)[1]
version = browser_spec.match(/browser-version\": \"(.*)\",/)[1]
errors << {:browser => type, :version => version, :error => error}
end
end
message = ""
errors.each_with_index do |error, index|
message +="\t[#{index+1}]: #{error[:error].message} occurred in #{error[:browser]}, version #{error[:version]}\n"
end
assert_equal 0, errors.length, "Expected zero failures or errors, but got #{errors.length}\n #{message}"
else
run_browser(browsers[0], block)
end
end
 
def run_browser(browser_spec, block)
browser = Selenium::Client::Driver.new(
:host => selenium_rc_url,
:port => 4444,
:browser => browser_spec,
:url => test_url,
:timeout_in_second => 120)
browser.start_new_browser_session
begin
block.call(browser)
ensure
browser.close_current_browser_session
end
end
 
def test_basic_walkthrough
run_in_all_browsers do |browser|
browser.open '/'
assert_equal 'Hosted Ruby/Rails metrics - Caliper', browser.title
assert browser.text?('Recently Generated Metrics')
browser.click "css=#projects a:contains('Projects')", :wait_for => :page
assert browser.text?('Browse Projects')
browser.click "css=#add-project a:contains('Add Project')", :wait_for => :page
assert browser.text?('Add Project')
browser.type 'repo','git://github.com/sinatra/sinatra.git'
browser.click "css=#submit-project", :wait_for => :page
assert browser.text?('sinatra/sinatra')
browser.wait_for_element "css=#hotspots-summary"
assert browser.text?('View full Hot Spots report')
end
end
def test_generate_new_metrics
run_in_all_browsers do |browser|
browser.open '/'
browser.click "css=#add-project a:contains('Add Project')", :wait_for => :page
assert browser.text?('Add Project')
browser.type 'repo','git://github.com/sinatra/sinatra.git'
browser.click "css=#submit-project", :wait_for => :page
assert browser.text?('sinatra/sinatra')
browser.click "css=#fetch"
browser.wait_for_page
assert browser.text?('sinatra/sinatra')
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.