Permalink
Browse files

Cleanup Rails provided helpers

1. Clean up screenshot helper

Updates documentation to be clearer and separates the concerns of saving
the image, setting the image path, and displaying the image.

2. Remove Rails provided assertions for selectors

This was moved upstream to Capybara and is no longer necessary to be
included in Rails

3. Remove form helper

The form helper is pretty specific to Basecamp's needs and may not be
helpful outside of Rails.
  • Loading branch information...
eileencodes committed Dec 3, 2016
1 parent 970ece2 commit 3f414c22c70d5647d2ee9f16f7e4eec15c829afb
@@ -3,8 +3,6 @@
module ActionSystemTest
module TestHelper # :nodoc:
- include TestHelpers::Assertions
- include TestHelpers::FormHelper
include TestHelpers::ScreenshotHelper
include Capybara::DSL
@@ -2,8 +2,6 @@ module ActionSystemTest
module TestHelpers
extend ActiveSupport::Autoload
- autoload :Assertions
- autoload :FormHelper
autoload :ScreenshotHelper
end
end
@@ -1,40 +0,0 @@
-module ActionSystemTest
- module TestHelpers
- # Assertions for system testing that aren't included by default in Capybara.
- # These are assertions that are useful specifically for Rails applications.
- module Assertions
- # Asserts that all of the provided selectors are present on the given page.
- #
- # assert_all_of_selectors('p', 'td')
- def assert_all_of_selectors(*items)
- options = items.extract_options!
- type = type_for_selector(items)
-
- items.each do |item|
- assert_selector type, item, options
- end
- end
-
- # Asserts that none of the provided selectors are present on the page.
- #
- # assert_none_of_selectors('ul', 'ol')
- def assert_none_of_selectors(*items)
- options = items.extract_options!
- type = type_for_selector(items)
-
- items.each do |item|
- assert_no_selector type, item, options
- end
- end
-
- private
- def type_for_selector(*items)
- if items.first.is_a?(Symbol)
- items.shift
- else
- Capybara.default_selector
- end
- end
- end
- end
-end
@@ -1,58 +0,0 @@
-module ActionSystemTest
- module TestHelpers
- # Form helpers for system testing that aren't included by default in
- # Capybara.
- module FormHelper
- # Finds all provided fields or text areas and fills in with supplied values.
- #
- # fill_in_all_fields('Name' => 'Eileen', 'Job Title' => 'Programmer')
- def fill_in_all_fields(fields)
- fields.each do |name, value|
- fill_in name, with: value
- end
- end
-
- # Locates a checkbox that is present inside a label and checks it. When
- # using styled boxes Selenium may not be able to see the checkbox. This
- # form helper looks inside the checkbox and clicks the label instead of
- # setting the value of the checkbox.
- #
- # click_checkbox_label 'Admin'
- #
- # By default +click_checkbox_label+ looks for checkboxes that are not
- # checked by default. To locate an already checked box and uncheck it
- # set checked to true:
- #
- # click_checkbox_label 'Admin', checked: true
- def click_checkbox_label(name, checked: false)
- field = find_checkbox(name, checked)
- label = find_label_wrapper(field)
- label.click
- end
-
- # In lieu of locating a button and calling +click_on+, +press_enter+ will
- # submit the form via enter. This method will only work for drivers that
- # load a browser like Selenium.
- #
- # test 'Adding a User' do
- # fill_in 'Name', with: 'Arya'
- #
- # press_enter
- #
- # assert_text 'Arya'
- # end
- def press_enter
- page.driver.browser.action.send_keys(:enter).perform
- end
-
- private
- def find_checkbox(name, checked)
- find(:field, name, visible: :all, checked: checked)
- end
-
- def find_label_wrapper(field, location: "./ancestor::label")
- field.find :xpath, location
- end
- end
- end
-end
@@ -13,24 +13,32 @@ module ScreenshotHelper
# ActionSystemTest.driver.supports_screenshots?
# => true
def take_screenshot
+ save_image
puts "[Screenshot]: #{image_path}"
- puts find_image
+ puts display_image
end
- # Takes a screenshot only if the test failed. This is included
- # by default in +teardown+ of system test.
+ # Takes a screenshot of the current page in the browser if the test
+ # failed.
+ #
+ # +take_screenshot+ is included in <tt>system_test_helper.rb</tt> that is
+ # generated with the application. To take screenshots when a test fails
+ # add +take_failed_screenshot+ to the teardown block before clearing any
+ # sessions.
def take_failed_screenshot
take_screenshot unless passed?
end
private
def image_path
- path = "tmp/screenshots/failures_#{method_name}.png"
- page.save_screenshot(Rails.root.join(path))
- path
+ "tmp/screenshots/failures_#{method_name}.png"
+ end
+
+ def save_image
+ page.save_screenshot(Rails.root.join(image_path))
end
- def find_image
+ def display_image
if ENV["CAPYBARA_INLINE_SCREENSHOT"] == "artifact"
"\e]1338;url=artifact://#{image_path}\a"
else
@@ -3,6 +3,62 @@
require "action_dispatch"
require "action_system_test"
+# Set the Rails tests to use the +:rack_test+ driver because
+# we're not testing Capybara or it's drivers, but rather that
+# the methods accept the proper arguments.
+class RoutedRackApp
+ attr_reader :routes
+
+ def initialize(routes, &blk)
+ @routes = routes
+ @stack = ActionDispatch::MiddlewareStack.new(&blk).build(@routes)
+ end
+
+ def call(env)
+ @stack.call(env)
+ end
+end
+
+class ActionSystemTestCase < ActionSystemTest::Base
+ ActionSystemTest.driver = :rack_test
+
+ def self.build_app(routes = nil)
+ RoutedRackApp.new(routes || ActionDispatch::Routing::RouteSet.new)
+ end
+end
+
+class PostsController < ActionController::Base
+ def index
+ render inline: <<HTML
+<html>
+<body>
+ <h1>This</h1>
+ <p title="the title" class="test">Paragraph 1</p>
+ <p title="the others" class="test">Paragraph 2</p>
+</body>
+</html>
+HTML
+ end
+end
+
+CapybaraRoutes = ActionDispatch::Routing::RouteSet.new
+CapybaraRoutes.draw do
+ resources :posts
+end
+
+# Initialize an application
+APP = ActionSystemTestCase.build_app(CapybaraRoutes)
+
+# Initialize an application for Capybara
+RailsApp = ActionSystemTestCase.new(APP)
+
+# Assign Capybara.app to original Rack Application
+Capybara.app = APP
+
+Capybara.add_selector :title_test do
+ xpath { |name| XPath.css(".test")[XPath.attr(:title).is(name.to_s)] }
+end
+
# Skips the current run on Rubinius using Minitest::Assertions#skip
def rubinius_skip(message = "")
skip message if RUBY_ENGINE == "rbx"

0 comments on commit 3f414c2

Please sign in to comment.