Skip to content

Instantly share code, notes, and snippets.

@whitneyimura
Created October 14, 2013 22:14
Show Gist options
  • Save whitneyimura/6983148 to your computer and use it in GitHub Desktop.
Save whitneyimura/6983148 to your computer and use it in GitHub Desktop.
Web env.rb file
require 'selenium-webdriver'
require 'capybara'
require 'rspec'
require 'launchy'
require 'active_support'
require 'active_support/inflector'
require 'headless'
require 'ruby-debug'
require 'webdriver-user-agent'
case ENV['BROWSER']
when 'ff', 'Firefox'
browser = Selenium::WebDriver.for :firefox
browser_name = 'Firefox'
when 'chrome'
browser = Selenium::WebDriver.for :chrome
browser_name = 'Chrome'
when 'debug'
debug_profile = Selenium::WebDriver::Firefox::Profile.new
debug_profile.add_extension "firebug-1.9.1-fx.xpi"
browser = Selenium::WebDriver.for :firefox, :profile => debug_profile
browser_name = 'Firefox (Firebug)'
when 'mobile'
mobile_profile = Selenium::WebDriver::Firefox::Profile.new
mobile_profile['general.useragent.override'] = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)
AppleWebKit/420+ (KHTML, like Gecko) Version/3.0
Mobile/1A535b Safari/419.3"
browser = Selenium::WebDriver.for :firefox, :profile => mobile_profile
browser_name = 'Mobile'
when 'headless'
headless_profile = Headless.new
headless.start
browser = Selenium::WebDriver.for :firefox, :profile => headless_profile
browser_name = 'Firefox'
else
browser = Selenium::WebDriver.for :firefox
browser_name = 'Firefox'
end
URLS = { 'live' => "google.com", 'dev' => "dev.google.com"}
if URLS[ENV['URL']].nil?
environment = 'dev'
url = URLS['dev']
else
environment = ENV['URL'].upcase
url = URLS[ENV['URL']]
end
if ENV['CLIENT'].nil?
client = 'whitney@google.com/password'
else
client = ENV['CLIENT']
end
puts "Browser " + browser_name
puts "URL " + url
puts "Environment: " + environment
puts "Client: " + client
test_env = { :browser => browser,
:browser_name => browser_name,
:url => url,
:env => environment,
:client => client,
:login => nil }
Before do
@test_env = test_env
@browser = browser
end
Capybara.use_default_driver
After do |scenario|
take_screenshot(@browser, scenario)
end
def take_screenshot(browser, scenario)
if scenario.failed?
scenario_name = scenario.name.gsub /[^\w\-]/, ' '
time = Time.now.strftime("%Y-%m-%d %H%M")
screenshot_path = './failed_png/' + time + ' - ' + scenario_name + '.png'
else
scenario_name = scenario.name.gsub /[^\w\-]/, ' '
time = Time.now.strftime("%Y-%m-%d %H%M")
screenshot_path = './success_png/' + time + ' - ' + scenario_name + '.png'
end
browser.save_screenshot(screenshot_path)
end
at_exit do
if ENV['KEEP_OPEN'] != 'false' || ENV['KEEP_OPEN'] != 'no'
browser.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment