Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yihyang/8ec25abc82f416391067af2d4c8660c0 to your computer and use it in GitHub Desktop.
Save yihyang/8ec25abc82f416391067af2d4c8660c0 to your computer and use it in GitHub Desktop.
Helpers for Responsive feature Specs in Capybara
The responsive helpers are to help the developer with writing responsive specs.
All you have to do is, add `responsive_helper.rb` to the spec support directory.
And tag the specs with :mobile, :tablet for mobile and tablet size respectively.
If you don't use any tag, it would render the desktop size.
# spec/support/feature/responsive_helpers.rb
module Feature
module ResponsiveHelpers
shared_context 'for mobile view', shared_context: :mobile do
before { resize_window_to_mobile }
after { resize_window_to_default }
end
shared_context 'for tablet view', shared_context: :tablet do
before { resize_window_to_tablet }
after { resize_window_to_default }
end
def resize_window_to_mobile
resize_window_by([375, 667])
end
def resize_window_to_tablet
resize_window_by([768, 1024])
end
def resize_window_to_default
resize_window_by([1280, 1024])
end
def resize_window_by(size)
Capybara.current_session.driver.browser.manage.window.resize_to(size[0], size[1])
end
end
end
RSpec.configure do |config|
config.include Feature::ResponsiveHelpers, type: :feature
config.include_context 'for mobile view', mobile: true
config.include_context 'for tablet view', tablet: true
end
require 'rails_helper'
feature 'header is responsive', :js do
background { visit root_path }
scenario 'In desktop view, header does not display mobile components' do
assert_desktop_components_present
end
scenario 'In tablet view, header displays the mobile components', :tablet do
assert_tablet_components_present
end
scenario 'In mobile view, header displays the mobile components', :mobile do
assert_mobile_components_present
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment