Created
September 15, 2011 15:48
-
-
Save pete-otaqui/1219619 to your computer and use it in GitHub Desktop.
env.rb for cucumber ... with fixtures in a mysql database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
Environment variables you can set: | |
MHT_HOST=domain.com ... the host to test, default is www.mangahigh.com | |
MHT_DRIVER='mechanize' ... default capybara driver to use, valid values are: | |
mechanize, | |
celerity, | |
chrome, | |
culerity, | |
selenium, | |
rack_test | |
MHT_WAIT_TIME=6 ... the number of seconds to wait for things to load | |
MHT_OS='ANY' ... OS to use for remote webdriver, valid values are: | |
ANDROID | |
ANY | |
LINUX | |
MAC | |
UNIX | |
VISTA | |
WINDOWS | |
XP | |
MHT_BROWSER=browser_name ... the browser driver to use, default is firefox, valid values are: | |
firefox | |
iexplore | |
chrome | |
android | |
MHT_BROWSER_VERSION='5' ... browser version to use for remote webdriver, defaults to '5' | |
(nb, if you specify something other than MHT_BROWSER='firefox' you will want to set this too!) | |
MHT_REMOTE_WEBDRIVER=false ... URL of the remote webdriver hub, valid values are: | |
false | |
http://wd.com:80/wd/hub | |
sauce [i.e. use Sauce Labs] | |
MHT_SAUCE_USERNAME='mangahigh' ... Sauce Labs username | |
MHT_SAUCE_ACCESS_KEY='a08f7a7f-97ae-97ad-345a-98f7ae9c78a7' ... Sauce Labs API Key | |
MHT_SAUCE_JOB_NAME='somebody' ... custom job name for Sauce Labs | |
MHT_DB_HOST=127.0.0.1 ... the database host - must have empty MangaHigh and bdauth dbs, user:root, no password | |
MHT_ENV_NAME='env' ... the environment name for MH2. If not supplied will be extracted from MHT_HOST | |
You have 3 options for setting environment variables (higher numbers below take precedence): | |
1. System level (i.e. in "Computer -> Properties" on Windows, or in the terminal in Linux/OS X) | |
2. Config level (i.e. by setting up a profile in cucumber.yml) | |
3. Run level (i.e. by running "$ cucumber MHT_BROWSER=chrome") | |
START A GRID "HUB" (on http://hubhost/) : | |
java -jar selenium-server-standalone-2.5.0.jar -role hub -p 4444 | |
* See selenium wiki Grid 2 page on google code http://bit.ly/jGcROd | |
* You can see the grid's status, and that of connected nodes, at http://hubhost:4444/grid/console | |
START A GRID "NODE": | |
java -jar selenium-server-standalone-2.5.0.jar -role webdriver -hub http://hubhost:4444/grid/register -browser browserName=firefox,version=6,maxInstances=5 -p 5555 | |
* you can supply multiple "-browser ...." args to add more browsers | |
* you can use "-url http://nodehost:5555/wd/hub" if you are having trouble with virtual IPs or anything | |
USE A "GRID" with capybara: | |
cucumber MHT_DRIVER=webdriver MHT_REMOTE_WEBDRIVER=http://hubhost:4444/wd/hub/ MHT_BROWSER=firefox MHT_BROWSER_VERSION=6 | |
* you might also need to supply the correct MHT_OS which must be from the list here: http://bit.ly/o1dmYA | |
=end | |
########### Global Requirements | |
#require 'capybara/mechanize' | |
require 'capybara' | |
require 'capybara/cucumber' | |
require 'rspec' | |
require 'mysql' | |
require 'httpclient' | |
########### Get Configuration | |
config = { | |
'mht_host' => ENV['MHT_HOST'] || 'www.mangahigh.com', | |
'mht_driver' => ENV['MHT_DRIVER'] || 'selenium', | |
'mht_wait_time' => ENV['MHT_WAIT_TIME'] || 12, | |
'mht_os' => ENV['MHT_OS'] || 'ANY', | |
'mht_browser' => ENV['MHT_BROWSER'] || 'firefox', | |
'mht_browser_version' => ENV['MHT_BROWSER_VERSION'] || '5', | |
'mht_remote_webdriver' => ENV['MHT_REMOTE_WEBDRIVER'] || 'http://127.0.0.1:4444/wd/hub', | |
'mht_sauce_username' => ENV['MHT_SAUCE_USERNAME'] || 'mangahigh', | |
'mht_sauce_access_key' => ENV['MHT_SAUCE_ACCESS_KEY'] || '076f006b-7e3e-4032-b1fd-668f10275964', | |
'mht_sauce_job_name' => ENV['MHT_SAUCE_JOB_NAME'] || 'mangahigh', | |
'mht_db_host' => ENV['MHT_DB_HOST'] || '127.0.0.1', | |
'mht_env_name' => ENV['MHT_ENV_NAME'] || nil | |
} | |
# extract 'gustaf' from 'cucumber.gustaf.int.mangahigh.com', or 'alec' from 'alec.int.mangahigh.com' | |
if ( config['mht_env_name'] == nil ) then | |
bits = config['mht_host'].split | |
if ( bits[0] == 'cucumber' ) then | |
env_name = bits[1] | |
else | |
env_name = bits[0] | |
end | |
config['mht_env_name'] = env_name | |
end | |
@config = config | |
########### Configure Capybara | |
# Create a "chrome" driver | |
Capybara.register_driver :chrome do |app| | |
Capybara::Selenium::Driver.new(app, :browser => :chrome) | |
end | |
# Use the "chrome" driver if we specified "chrome" through the browser name | |
if ( config['mht_browser'] == 'chrome' && config['mht_driver'] == 'selenium' ) then | |
config['mht_driver'] = 'chrome' | |
end | |
if ( config['mht_driver'] == 'webkit' ) | |
require 'capybara-webkit' | |
end | |
if config['mht_remote_webdriver'] then | |
Capybara.register_driver :webdriver do |app| | |
## are we providing a remote webdriver URL or using Sauce Labs? | |
if config['mht_remote_webdriver'] == 'sauce' then | |
un = config['mht_sauce_username'] | |
ak = config['mht_sauce_access_key'] | |
remote_url = 'http://'+un+':'+ak+'@ondemand.saucelabs.com:80/wd/hub' | |
else | |
remote_url = config['mht_remote_webdriver'] | |
end | |
## create the options for the new Remote Capabilities object | |
capabilities_opts = { | |
:platform => config['mht_os'], | |
:version => config['mht_browser_version'], | |
:javascript_enabled => true, | |
:css_selectors_enabled => true, | |
:name => config['mht_sauce_job_name'] | |
} | |
capabilities_opts[:browser_name] = config['mht_browser'] if config['mht_browser'] | |
capabilities_opts[:version] = config['mht_browser_version'] if config['mht_browser_version'] | |
# make the new Remote Capabilities object | |
capabilities = Selenium::WebDriver::Remote::Capabilities.new(capabilities_opts) | |
## make the actual client | |
client = Selenium::WebDriver::Remote::Http::Default.new | |
## make the opts for the new driver | |
opts = { | |
:url => remote_url, | |
:desired_capabilities => capabilities, | |
:http_client => client, | |
:browser => :remote | |
} | |
## make the new driver | |
Capybara::Selenium::Driver.new(app,opts) | |
end | |
end | |
Capybara.app_host = 'http://' + config['mht_host'] | |
Capybara.default_driver = config['mht_driver'].to_sym | |
Capybara.javascript_driver = config['mht_driver'].to_sym | |
Capybara.default_wait_time = config['mht_wait_time'] | |
########### Fixtures | |
class Fixtures | |
def initialize | |
end | |
attr_accessor :config | |
attr_accessor :db_host | |
attr_accessor :db_user | |
attr_accessor :db_pass | |
attr_accessor :db_name | |
def start | |
cleanSql | |
loadFixture('global.sql') | |
buildMh2('cucumber') | |
end | |
def getDbh(database=nil) | |
database = self.db_name unless database | |
Mysql.real_connect(self.db_host, self.db_user, self.db_pass, database) | |
end | |
def loadFixture(filename, database=nil) | |
dbh = getDbh(database) | |
fixture = File.new(File.dirname(__FILE__) + '/fixtures/' + filename, 'r') | |
while ( statement = fixture.gets ) | |
statement.strip! | |
dbh.query(statement) if statement != '' | |
end | |
end | |
def buildMh2(env) | |
host = self.config['mht_host'] | |
puts "MH2 rebuild on #{host} [env=#{env}]" | |
client = HTTPClient.new | |
response = client.get('http://'+host+'/mh2/build.php?env='+env) | |
puts "MH2 rebuild complete" | |
end | |
def cleanSql | |
loadFixture('clean.sql') | |
end | |
def finish | |
cleanSql | |
buildMh2(self.config['mht_env_name']) | |
end | |
end | |
@fixtures = Fixtures.new | |
@fixtures.config = config | |
@fixtures.db_host = config['mht_db_host'] | |
@fixtures.db_user = 'root' | |
@fixtures.db_pass = '' | |
@fixtures.db_name = 'MangaHigh_cucumber' | |
@fixtures.start | |
########### Cucumber World | |
class TestserverWorld | |
include RSpec::Expectations | |
include RSpec::Matchers | |
end | |
World do | |
TestserverWorld.new | |
@fixtures | |
end | |
at_exit do | |
@fixtures.finish | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment