Skip to content

Instantly share code, notes, and snippets.

@IdahoEv
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save IdahoEv/6be0a2b1dd55b75474fa to your computer and use it in GitHub Desktop.

Select an option

Save IdahoEv/6be0a2b1dd55b75474fa to your computer and use it in GitHub Desktop.
#backend/spec/support/browser_size.rb
# note, our Rails app is not in the repo root. We have:
# <root>/backend (Rails back-end app)
# <root>/frontend (AngularJS front-end SPA)
module BrowserSize
SIZES = {
:mobile => { :width => 320, :height => 480 },
:small => { :width => 550, :height => 700 },
:medium => { :width => 800, :height => 900 },
:desktop => { :width => 1024, :height => 1024 }
}
RSpec.configure do |config|
config.before(:each) do
if RSpec.current_example.metadata[:js]
BrowserSize.resize_browser_window(SIZES[BrowserSize.current_size])
end
end
end
def self.resize_browser_window(size)
Capybara.current_session.driver.browser.manage.window.resize_to(size[:width], size[:height])
end
def self.current_size
(RSpec.current_example.metadata[:size] || ENV['BROWSER_SIZE'] || :desktop).to_sym
end
end
RSpec.configure do |config|
config.include BrowserSize, :type => :feature
end
namespace :spec do
# ... other stuff ...
desc "Run all feature specs, repeating with each browser width as default"
task :responsivity, [:spec_files] => [:links, 'backend:setup'] do |t, args|
Bundler.with_clean_env do
%w{mobile small medium desktop}.each do |size|
Dir.chdir("backend"){
ENV['BROWSER_SIZE']=size
commands = ["bundle", "exec", "rspec", "-o", "tmp/rspec_#{size}.txt"]
if args[:spec_files]
commands.push(args[:spec_files])
else
commands.push('spec/features')
end
sh *commands rescue true
}
end
end
end
end
#backend/spec/support/session_helpers.rb
module Features
module SessionHelpers
COLLAPSED_MENU_SIZES=[ :mobile, :small ]
def visit_login
visit '/'
expand_menu_if_necessary
within ".session-links" do
click_link "Sign In"
end
end
def expand_menu_if_necessary
# On pages with collapsed menu, click to expand the menu
# before trying to click the sign-in link. I'm doing it this
# way instead of inspecting the page for .session-links because
# the latter triggers a capybara wait, which is much slower.
if COLLAPSED_MENU_SIZES.include?(BrowserSize.current_size)
click_on('Menu')
end
end
def sign_up_with(email, password)
visit '/'
expand_menu_if_necessary
click_on "Sign Up"
fill_in "Email", :with => email
fill_in "Email Confirmation", :with => email
fill_in "Password", :with => password
fill_in "Password Confirmation", :with => password
click_button "Sign Up"
end
def sign_in_with(email, password)
visit_login
fill_in "Email", :with => email
fill_in "Password", :with => password
click_button "Sign In"
end
end
end
require 'spec_helper'
feature "Visitor navigates with the main menu", :js => true, :vcr => {} do
let :page_one do
FactoryGirl.create(:one_column_page)
end
let :page_two do
FactoryGirl.create(:two_column_page)
end
let! :page_link do
FactoryGirl.create(:menu_item, name: 'Page Link', page: page_one)
end
let! :path_link do
FactoryGirl.create(:menu_item_without_page, name: "Path Link", path: '/#/sign-in')
end
context "on a desktop", :size => :desktop do
scenario "visit a path" do
visit '/'
click_on("Path Link")
expect(page).to have_content("Sign In")
expect(URI(current_url).fragment).to eq('/sign-in')
end
scenario "visit a page" do
visit '/'
click_on("Page Link")
expect(page).to have_content(page_one.contents["headline"].body)
expect(page.body).to include(page_one.contents["main"].body)
expect(URI(current_url).fragment).to eq("/pages/" + page_one.url_slug)
end
end
context "on a mobile device", :size => :mobile do
scenario "visit a path" do
visit '/'
click_on("Menu") # Expand the currently-collapsed menu
click_on("Path Link")
expect(page).to have_content("Sign In")
expect(URI(current_url).fragment).to eq('/sign-in')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment