Created
January 14, 2014 16:18
-
-
Save dblooman/8420946 to your computer and use it in GitHub Desktop.
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
| $:.unshift(File.expand_path("./ruby")) | |
| require 'capybara' | |
| require 'capybara/cucumber' | |
| require 'selenium/webdriver' | |
| require 'capybara/poltergeist' | |
| require 'capybara/mechanize/cucumber' | |
| require 'restclient' | |
| require "anmo" | |
| require "awesome_print" | |
| require "candy" | |
| require "random_words" | |
| require "forge" | |
| require 'test/unit/assertions' | |
| require 'pry' | |
| World(Test::Unit::Assertions) | |
| if Forge.running_on_forge_ci? | |
| DEFAULT_HOST = "http://pal.int.bbc.co.uk" | |
| Anmo.server = "http://on.responsivenews.co.uk:9999" | |
| else | |
| DEFAULT_HOST = "http://pal.sandbox.dev.bbc.co.uk" | |
| Anmo.server = "http://192.168.192.1:9456" | |
| end | |
| # if Forge.running_on_forge_ci? | |
| # if Anmo.running? == false | |
| # raise "Anmo doesn't seem to be running at #{Anmo.server}!" | |
| # end | |
| # server_version = Anmo.server_version | |
| # if Anmo.server_version != Anmo::VERSION | |
| # raise "Anmo server version (#{server_version}) and client version (#{Anmo::VERSION})do not match." | |
| # end | |
| # else | |
| # @anmo = fork do | |
| # Anmo.launch_server 9456 | |
| # end | |
| # end | |
| # at_exit do | |
| # if @anmo | |
| # Process.kill("KILL", @anmo) | |
| # end | |
| # end | |
| Capybara.default_wait_time = 20 | |
| Capybara.default_host = DEFAULT_HOST | |
| Capybara.app = Capybara.app_host = DEFAULT_HOST | |
| Capybara.configure do |config| | |
| config.match = :prefer_exact | |
| config.ignore_hidden_elements = false | |
| end | |
| Capybara.run_server = false | |
| Capybara.current_driver = :mechanize | |
| class Capybara::Mechanize::Browser < Capybara::RackTest::Browser | |
| def default_user_agent | |
| "cucumber" | |
| end | |
| end | |
| Capybara.register_driver :mechanize_with_test_user_agent do |app| | |
| driver = Capybara::Mechanize::Driver.new(app) | |
| driver.browser.agent.user_agent = "cucumber" | |
| driver | |
| end | |
| Capybara.register_driver :selenium do |app| | |
| Capybara::Selenium::Driver.new(app, :browser => :chrome, :args => ["--user-agent=cucumber"]) | |
| end | |
| Capybara.register_driver :mechanize_with_test_user_agent_and_adverts do |app| | |
| driver = Capybara::Mechanize::Driver.new(app) | |
| driver.browser.agent.user_agent = "cucumber" | |
| driver.browser.agent.request_headers = {"X-Advert-Override" => "true"} | |
| driver | |
| end | |
| Capybara.register_driver :poltergeist do |app| | |
| driver = Capybara::Poltergeist::Driver.new(app, { | |
| :inspector => true, | |
| :timeout => 60, | |
| :js_errors => false | |
| }) | |
| driver | |
| end | |
| Capybara.default_driver = :mechanize_with_test_user_agent | |
| def setup_poltergeist | |
| Capybara.current_driver = :poltergeist | |
| page.driver.headers = { | |
| "User-Agent" => "cucumber" | |
| } | |
| # the following code is required to prevent some JS code causing cukes to fail | |
| # i.e. our grid overlay javascript sets up 'keyup' event bindings | |
| # for example if you press the 'a' key then the grid overlay visibility is toggled | |
| # but when Capybara/Poltergeist fills in a form field it causes the grid to toggle on, | |
| # and that then breaks further tests because Capybara/Poltergeist can't click on the page elements | |
| page.driver.set_cookie('poltergeist', 'is running') | |
| end | |
| Before "@javascript" do | |
| setup_poltergeist | |
| end | |
| Before "@device_desktop" do |scenario| | |
| setup_poltergeist | |
| set_device('desktop') | |
| end | |
| Before "@multi_device" do |scenario| | |
| setup_poltergeist | |
| set_device(scenario.to_hash['device']) | |
| end | |
| Before "@multi_device_debug" do |scenario| | |
| Capybara.current_driver = :selenium | |
| set_device(scenario.to_hash['device']) | |
| end | |
| def set_device(device) | |
| case device | |
| #Height is set to 800 across the board, but should be changed | |
| #to something more appropriate when our site reacts to it | |
| when "feature phone" | |
| Capybara.current_driver = nil | |
| when "smart phone portrait" | |
| resize_browser(320, 800) | |
| when "smart phone landscape" | |
| resize_browser(480, 800) | |
| when "tablet portrait" | |
| resize_browser(640, 800) | |
| when "tablet landscape" | |
| resize_browser(768, 800) | |
| when "desktop" | |
| resize_browser(1280, 1024) | |
| when "desktop no javascript" | |
| Capybara.current_driver = nil | |
| end | |
| end | |
| def resize_browser(w, h) | |
| if Capybara.current_driver == :selenium | |
| page.driver.browser.manage.window.resize_to(w, h) | |
| else | |
| page.driver.resize(w, h) | |
| end | |
| end | |
| Before "@debug" do | |
| Capybara.current_driver = :selenium | |
| end | |
| After "@javascript" do | |
| begin | |
| Capybara.drivers[:poltergeist].call().quit | |
| rescue | |
| end | |
| Capybara.current_driver = nil | |
| end | |
| After "@debug, @multi_device, @multi_device_debug" do | |
| Capybara.current_driver = nil | |
| end | |
| World(Capybara) | |
| Before do | |
| candy_repository.clean_up_candy_objects | |
| @random_token = rand(10000) | |
| if ENV['smoke_environment'] | |
| env = ENV['smoke_environment'] | |
| else | |
| env = "sandbox.dev" unless Forge.running_on_forge_ci? | |
| end | |
| @smoker = Smoker.new env | |
| @query_params = {} | |
| end | |
| Before('@varnish') do | |
| @smoker.varnish = true | |
| end | |
| After do | |
| candy_repository.clean_up_candy_objects | |
| Capybara.default_host = DEFAULT_HOST | |
| end | |
| # Swap the port number if the test is annotated with Varnish, and | |
| # put it back to :80 afterwards | |
| Around('@varnish') do |scenario, block| | |
| default_host = Capybara.default_host | |
| # on sandbox varnish is listening on port :6081, on other environments it's www.<env> port :80 | |
| if (default_host.include?('sandbox')) then | |
| Capybara.default_host = Capybara.default_host.gsub(/(:\d+)$/, ':6081') | |
| # Add :6081 if previous default_host didn't specify port | |
| Capybara.default_host += Capybara.default_host.match(/:6081$/) ? '' : ':6081'; | |
| else | |
| Capybara.default_host = Capybara.default_host.gsub(/pal\./, 'www.') | |
| end | |
| block.call | |
| Capybara.default_host = default_host | |
| end | |
| module CandyRepositoryMethods | |
| def candy_repository | |
| @candy_repository ||= CandyRepository.new | |
| end | |
| def save object | |
| candy_repository.save object | |
| end | |
| def save_correspondent_stories path, data | |
| candy_repository.save_correspondent_stories path, data, 200 | |
| end | |
| def save_data_with_path path, data | |
| candy_repository.save_data path, data, 200 | |
| end | |
| end | |
| World(CandyRepositoryMethods) | |
| module WebHosted | |
| def host | |
| Capybara.default_host | |
| end | |
| def ichef_host | |
| if host.to_s.include? "sandbox" | |
| return "ichef.sandbox.dev.bbc.co.uk" | |
| else | |
| return "ichef.int.bbci.co.uk" | |
| end | |
| end | |
| end | |
| World(WebHosted) | |
| module Certs | |
| def Certs.ssl_client_cert | |
| OpenSSL::X509::Certificate.new(Certs.pem) | |
| end | |
| def Certs.ssl_client_key | |
| OpenSSL::PKey::RSA.new(Certs.pem) | |
| end | |
| def Certs.pem | |
| pems = ["/etc/pki/LabUK.pem", "../vosbox-dependencies/certificate.pem", "scripts/certs/certificate.pem", "dev.bbc.co.uk.pem"] | |
| pem = pems.select do |pm| | |
| File.exists?(File.expand_path(pm)) | |
| end.first | |
| File.read(pem) | |
| end | |
| end | |
| module Capybara | |
| class Session | |
| alias :_visit :visit | |
| def visit(url, opts = {}) | |
| opts[:query_params] ||= {} | |
| if opts[:cache_busting] != false | |
| opts[:query_params]['cb'] = Time.now.to_i | |
| opts[:query_params]['i_hate_caching'] = rand(10000) | |
| end | |
| params = opts[:query_params].map{|k,v| [k, "=", v]}.map(&:join).join("&") | |
| uri = URI.parse(url) | |
| query = "#{uri.query}&#{params}".gsub(/^&/, '') | |
| url = URI::Generic.build( | |
| :scheme => uri.scheme, | |
| :host => uri.host, | |
| :port => uri.port, | |
| :path => uri.path, | |
| :query => query, | |
| :fragment => uri.fragment | |
| ).to_s | |
| _visit url | |
| raise "PHP #{$&} in response!" if /(?:Notice|Warning|Fatal error):/.match(body) | |
| end | |
| end | |
| end | |
| World(Certs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment