Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spinach-rails or spinach gem? #189

Open
c1505 opened this issue Aug 7, 2016 · 4 comments
Open

spinach-rails or spinach gem? #189

c1505 opened this issue Aug 7, 2016 · 4 comments

Comments

@c1505
Copy link

c1505 commented Aug 7, 2016

I am just starting to look at using spinach for rails projects and it isn't clear to me if I should be using spinach-rails or not for rails 4 and up.

It doesn't look like spinach-rails is being actively maintained. I see that spinach-rails is recommended in the readme for rails 3 projects, but I don't see any indication for other rails versions.

Thanks for your help.

@ywen
Copy link
Collaborator

ywen commented Aug 7, 2016

I do not use spinach-rails. Just spinach

My typical env.rb fiel looks like this:

require "webmock"
include WebMock::API
WebMock.disable_net_connect!(allow_localhost: true)
require "./config/environment"
require "rspec/rails"
require "spinach/capybara"
require "capybara-screenshot/spinach"
require "ci/reporter/spinach"

Dir[Rails.root.join("features/support/**/*.rb")].each do |f|
  require f
end

require Rails.root.join("features/steps/common_steps/base")

Capybara.javascript_driver = :selenium
Capybara.default_max_wait_time = (ENV["CAPYBARA_WAIT_TIME"] || 15).to_i
Capybara.wait_on_first_by_default = true

require "database_cleaner"

Spinach.hooks.before_run do
  DatabaseCleaner.clean_with(:truncation)
end

Spinach.hooks.on_tag("javascript") do
  ::Capybara.current_driver = ::Capybara.javascript_driver
  Capybara.page.driver.browser.manage.window.maximize
end

Spinach.hooks.before_scenario do
  DatabaseCleaner.strategy = :truncation
  DatabaseCleaner.start
end

Spinach.hooks.after_scenario do
  DatabaseCleaner.clean
  `rm -rf ~/.mozilla`
end

Instead of extend my step files from Spinach::FeatureSteps directly I have an intermediate class Base to do some further common setup, an example:

Dir[Rails.root.join("features/steps/common_steps/**/*.rb")].each do |f|
  require f
end

module CommonSteps
  class Base < Spinach::FeatureSteps
    include Login

    def parent_element(element)
      element.first(:xpath, ".//..")
    end

    def js_errors
      page.driver.browser.manage.logs.get(:browser)
    end

    def position_of(parent_element:, text:)
      parent_element["innerHTML"].index(text)
    end
  end
end

I hope this helps you to get started with spinach. Enjoy

@c1505
Copy link
Author

c1505 commented Aug 7, 2016

Thanks @ywen . This is helpful.

@dgarwood
Copy link

@c1505 realize I'm a bit late to the party on this, but since this is still open, thought I'd provide an update for anyone else that might be wondering the same thing.

I've included gem 'spinach-rails' in a rails 5.2 project under the group :development, :test heading, and it's working well. after bundle install it allows running rails g spinach which creates the feature/support/env.rb file. Mine currently looks like the following:

ENV['RAILS_ENV'] = 'test'
require './config/environment'
require 'rspec/rails'

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Spinach.config.save_and_open_page_on_failure = true

One of the nice things about this with rails 5.2 is that I no longer have to include Capybara (it's there by default in Rails 5.2) and I also do not need DatabaseCleaner (rails 5.2 also removed the need for this). Obviously, I'm using RSpec, and the spec/support line provides all the RSpec helpers to Spinach as well.

@dgarwood
Copy link

dgarwood commented Sep 3, 2018

discovered some additional issues with my previous config. I ended up with this as my feature/support/env.rb file:

# Help Spinach find the spec directory for autoloading
$LOAD_PATH.unshift(::File.expand_path('../../spec', __dir__))
require './config/environment'
# Most of the desired config is already in rspec
require 'rails_helper'
require 'database_cleaner'
require 'timecop'

DatabaseCleaner.strategy = :truncation

Spinach.hooks.before_scenario do
  ::Capybara.current_driver = :selenium_chrome_headless
  DatabaseCleaner.clean
end

Spinach.hooks.after_scenario do
  Timecop.return
end

# Spinach.config.save_and_open_page_on_failure = true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants