-
Notifications
You must be signed in to change notification settings - Fork 21.9k
WIP: Capybara Integration with Rails (AKA System Tests) #26703
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
Conversation
@eileencodes We should also look for https://github.com/amatsuda/database_rewinder as an alternative to DatabaseCleaner. |
This is excellent! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @eileencodes, I ran through the documentation and commented on some grammar issues.
This PR looks great, and I'm excited for it to be a part of the default Test Suite!
actionpack/lib/system_test_case.rb
Outdated
|
||
module Rails | ||
# System tests are similar to Integration tests in that they incorporate multiple | ||
# controllers and actions, but can be used to similate a real user experience. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similate -> simulate
# | ||
# By default Rails supports Capybara with the Selenium Driver. Rails provides | ||
# configuration setup for using the selenium driver with Capybara. | ||
# Additionally Rails can be used as a layer between Capybara and it's other |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's -> its
# The drivers Capybara supports are: +:rack_test+, +:selenium+, +:webkit+, | ||
# and +:poltergeist+. | ||
# | ||
# Rails provides it's own defaults for Capybara with the Selenium driver |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's -> its
# directly. | ||
# | ||
# To set your system tests to use one of Capybara's default drivers add | ||
# the following to yur Rails' configuration test environment: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"to use one of Capybara's default drivers add" -> "to use one of Capybara's default drivers, add"
line 18: yur -> your
# JavaScript testing and doesn't require a server. | ||
# | ||
# The +:poltergeist+ and +:webkit+ drivers are headless, but require some | ||
# extra environment setup. Because the defalt server for Rails is Puma, each |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defalt -> default
# of the Capybara drivers will default to using Puma. Changing the configuration | ||
# to use Webrick is possible by initalizing a new driver object. | ||
# | ||
# The default settings for the <tt>CapybaraDrvier</tt> are: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CapybaraDrvier -> CapybaraDriver
# selenium-webdriver gem is required by this driver. | ||
# | ||
# The <tt>RailsSeleniumDriver</tt> is useful for real browser testing and | ||
# support Chrome and Firefox. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
support -> supports
# @screen_size=[ 1400, 1400 ] | ||
# > | ||
# | ||
# The settings for the <tt>RailsSeleniumDriver</tt> can be changed in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"in the Rails'" -> "in the Rails" OR "in Rail's"
I assume that this follow's Capybara's opinionated policy of making it hard to access request/response objects and that we should use integration tests for those? |
actionpack/lib/system_test_case.rb
Outdated
require "system_testing/test_helper" | ||
require "system_testing/driver_adapter" | ||
|
||
module Rails |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're shipping this under the actionpack
gem, should we use the ActionPack
base module? 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I explained why I chose this name in the message above and I don't want to move it around 100 times before we have a consensus on name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually don't see any dependencies for ActionPack in this patch, so I think we could consider making it a complete stand-alone gem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dhh Rails::SystemTestCase
inherits from ActionDispatch::IntegrationTest
so we don't have to rewrite all the routing and url helpers handling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I missed that. Then going with ActionDispatch::SystemTest probably makes sense. But agree no sense in making any changes until we really know if we love that name!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like ActionDispatch::SystemTest
# Rails' configuration file. | ||
def driver=(driver) | ||
@driver = DriverAdapters.lookup(driver) | ||
@driver.call |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be more explicit about what call
does, in regard to setting up / registering the adapter? 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling your personal @driver
gets you a smooth getaway 😎
# | Selenium | Firefox | Yes | | ||
# | Webkit | Headless w/ QtWebKit | Yes | | ||
# | Poltergeist | Headless w/ PhantomJS | Yes | | ||
module DriverAdapters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to provide a "base" class to inherit from, so people know which methods they need to implement when creating adapters? 😬
end | ||
|
||
def supports_screenshots? | ||
if @name == :rack_test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use a conditional to return a boolean.
def supports_screenshots?
@name == :rack_test
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but it would be @name != :rack_test
. (However, it may have been written as is with the intention that more cases / logic would be added.)
Is there a specific reason for defaulting to selenium instead of a headless driver such as poltergeist or capybara-webkit? |
I agree with @sgrif, IMHO in real world apps people will switch to headless driver right away, so maybe capybara-webkit/poltergeist is better default here 😄 |
@sgrif and @dixpac I chose the Selenium driver as default because the purpose of adding system testing to Rails is for the initial setup to be absolutely zero. Capybara does that through their default, Rack Test, but it's not really a useful demonstration of the merits of system testing since it doesn't support JavaScript testing. The selenium driver is one that Rails can take on all of the setup without any extra requirements from the programmer. If there's a compelling reason to change the default it's as simple as swapping out the pre-set default and ensuring that Rails takes on the setup for that. @pixeltrix Since this PR doesn't change how Capybara works, but rather provides a layer between Capybara and Rails.Currently, no, the request object isn't defined, but |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks sweet! Especially looking forward to the DatabaseCleaner
work around 😁
actionpack/lib/system_test_case.rb
Outdated
# fill_in 'Name', with: 'Arya' | ||
# click_on 'Create User' | ||
# | ||
# assert_text 'Arya' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
woof! 🐕
actionpack/lib/system_test_case.rb
Outdated
# click_on 'New User' | ||
# | ||
# fill_in 'Name', with: 'Arya' | ||
# click_on 'Create User' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we do anything to make the Capybara helpers work better with Rails' integration helpers?
I haven't used Capybara much, but from what I can remember you can't call visit
and then use assert_response :success
for instance.
Pointing this out because the system test inherits from ActionDispatch::IntegrationTest
so users might think our built in assertions would work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's a great idea for the future, but right now there's still a ton left to do on this PR and I'm not exactly sure how to expose it just yet. I agree it would be great to add that in once we have this PR in master.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, let's revisit some other time 👍
# Rails' configuration file. | ||
def driver=(driver) | ||
@driver = DriverAdapters.lookup(driver) | ||
@driver.call |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling your personal @driver
gets you a smooth getaway 😎
# | Rails' Selenium | Chrome | Yes | | ||
# | Rack Test | No JS Support | No | | ||
# | Selenium | Firefox | Yes | | ||
# | Webkit | Headless w/ QtWebKit | Yes | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it's spelled WebKit
.
# | ||
# config.system_testing.driver = SystemTesting::DriverAdapters::CapybaraDriver.new( | ||
# name: :webkit, | ||
# puma: :webrick |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
server: :webrick
private | ||
def type_for_selector(*items) | ||
if items.first.is_a?(Symbol) | ||
items.shift |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we want to shift this arg? What kind of arguments can we see in this method?
# | ||
# assert_none_of_selectors('ul', 'ol') | ||
def assert_none_of_selectors(*items) | ||
options = items.extract_options! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can use a double splat here:
def assert_none_of_selectors(*items, **options)
# ...
end
# 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto re double splat.
def image_path | ||
path = "tmp/screenshots/failures_#{method_name}.png" | ||
page.save_screenshot(Rails.root.join(path)) | ||
path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Find it a little odd that image_path
both returns a path and saves an image. I mean, wouldn't the two calls to image_path
in find_image
save two more images?
assert_equal true, Rails::SystemTestCase.driver.supports_screenshots? | ||
|
||
Rails::SystemTestCase.driver = :poltergeist | ||
assert_equal true, Rails::SystemTestCase.driver.supports_screenshots? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we want this to return booleans over our generally preferred falsey values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was an oversight, I've fixed this now.
2d7a1e8
to
e94ab08
Compare
dcc96aa
to
3b9f07f
Compare
# name: :webkit, | ||
# server: :webrick | ||
# ) | ||
class CapybaraDriver |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[sorry, just reposting this comment since it got accidentally marked as "outdated" thanks to the great work being done here :)]
Does it make sense to provide a "base" class to inherit from, so people know which methods they need to implement when creating adapters? 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @maclover7 thanks. I know there are a lot of comments to address and getting around to them as I can. In the future, no need to re-comment.
I don't think adding a base class makes sense becasue the Rails provided adapter takes different arguments than the capybara provided adapters. It might make sense as this project evolves but I'll tackle that when that happens.
@@ -0,0 +1,31 @@ | |||
require "system_testing/driver_adapters" | |||
|
|||
module SystemTesting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not this class be in one namespacing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels to me that this should be a new gem. It have its own namespace, its own railtie it can be skipped and I don't think we should add capybara as dependency of actionpack. I'd create a new gem, inside the rails repository and add it as dependency of Rails.
@port = port | ||
end | ||
|
||
def call |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call it start
?
# config.system_testing.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( | ||
# server: :webrick, | ||
# port: 28100, | ||
# screen_size: [ 800, 800 ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our style guide doesn't put spaces inside array literals
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it is the other way around. I'll make rubocop point that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no support for that in rubocop 😢 .
|
||
attr_reader :browser, :server, :port, :screen_size | ||
|
||
def initialize(browser: :chrome, server: :puma, port: 28100, screen_size: [ 1400,1400 ]) # :nodoc: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing space after the ,
and remove the space inside the array literal
options.driver ||= Rails::SystemTestCase.default_driver | ||
|
||
ActiveSupport.on_load(:system_testing) do | ||
options.each { |k,v| send("#{k}=", v) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
space after the ,
@rafaelfranca David and I talked about the naming etc this week and decided on creating a new gem inside Rails, so I'll be doing that soon. Thanks for the feedback! |
3b9f07f
to
7128509
Compare
7128509
to
1c8b89f
Compare
|
||
s.license = "MIT" | ||
|
||
s.author = ["Eileen Uchitell", "David Heinemeier Hansson"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I'd throw an e
on there, but I'll let you be the judge of how to spell your name 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that, still working on a bunch of stuff. I'll let you know when I'm ready for more review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good 👍
# === Driver Features | ||
# | ||
# | | Default Browser | Supports Screenshots? | | ||
# |-----------------|-----------------------|-----------------------| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: saw another table where we put spaces between the rows and pipes, e.g.: ---- | ----
. Not sure if we have a preferred style.
@@ -0,0 +1,15 @@ | |||
module ActionSystemTest | |||
# Returns the version of the currently loaded Action Cable as a <tt>Gem::Version</tt>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's be cable cutters: Action System Test
, I presume 😁
require_relative "gem_version" | ||
|
||
module ActionSystemTest | ||
# Returns the version of the currently loaded Action Cable as a <tt>Gem::Version</tt> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cable :)
puts "before: " + has_user_table.to_s | ||
end | ||
|
||
task :after_hook do | ||
has_user_table = ActiveRecord::Base.connection.table_exists?('users') | ||
has_user_table = ActiveRecord::Base.connection.data_source_exists?('users') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this change related to system testing?
41ff098
to
ab55f1e
Compare
# click_checkbox_label 'Admin', checked: true | ||
def click_checkbox_label(name, checked: false) | ||
field = find_checkbox(name, checked) | ||
label = find_label_wrapper(field) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Capybara provides a selector - https://github.com/jnicklas/capybara/blob/master/lib/capybara/selector.rb#L432 - for the label that wraps an input or where the id/for attributes match. so this could be find(:label, for: field)
end | ||
|
||
private | ||
def find_checkbox(name, checked) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Capybara provides a :checkbox selector - https://github.com/jnicklas/capybara/blob/master/lib/capybara/selector.rb#L298 - which would limit this to only checkboxes if thats whats desired
This adds the required guides for how to write and use system tests in your application.
This is a major rewrite of what existed previously. After discussing this feature with DHH I realized that I was looking at the setup all wrong. I had originally mentally broken it into "what Rails wants" and "what Capybara already has". What happened after looking at it from DHH's angle was that I saw there was no reason to group settings by Driver but instead the following groups: - There will always be a `Driver` - This can selenium, poltergeist, or capybara webkit. Capybara already provides all of these and there's no reason to break them into a category of Rails' usese Selenium like this and Capybara uses it like that. - Only Selenium drivers care about `Browser` - Because of this it was weird to set it only in the Rails end. - Therefore only `Browser`, and not `Driver` cares about `screen_size`. - Puma is the default `Server` in Rails - Therefore there's no reason to explictly support Webkit Once I looked at it from this angle I was able to abstract all the settings away from grouping the drivers with their options. Now all the driver, server, and browser settings are abstracted away and not part of the public facing API. This means there's no requirement to initialize new classes to change the default settings and the public options API is much smaller. All of Rails preferred defaults are still there (selenium with port 21800 using the chrome browser with a screen size of 1400x1400) but changing these no longer requires initializing a new class or understanding which driver you're using underneath (rails defaults or capybaras defaults respectively). Rails opinions are now simple defaults instead of doing a them versus us setup with Drivers and explicit options. Changing the defaults is simple. Call `driven_by` with different settings to change the defaults which will on their own initialize new classes and change the default settings. Use poltergeist with port 3000 for Puma ``` driven_by :poltergeist, on: 3000 ``` Use selenium with the Chrome browser and a screen size of 800x800 ``` driven_by :selenium, using: :firefox, screen_size: [ 800, 800 ] ``` The entire setup of how browser and drivers interact with each other are abstracted away and the only required argument is the driver name.
Update the documentation after rewriting a majority of the functionality for system testing.
I've renamed the server to `rails_puma` so that it doesn't override Capybara's default puma server. I've also removed the hard port setting. Users can simply use `Capybara.server_port` writer to set the port if they require that.
* Move system tests back into Action Pack * Rename `ActionSystemTest` to `ActionDispatch::SystemTestCase` * Remove private base module and only make file for public `SystemTestCase` class, name private module `SystemTesting` * Rename `ActionSystemTestCase` to `ApplicationSystemTestCase` * Update corresponding documentation and guides * Delete old `ActionSystemTest` files
This serves as self documentation so users know how to change the driver.
We only want the file name to include the word `failures` if it failed, not any time the user wants to take a screenshot during a test run.
* Override integration test default host Integration tests automatically set the default host to 'http://example.com'. This works fine for integration tests because they are not real browser sessions, but doesn't work fine for system tests because they are real browser sessions. We can override this by setting the `host!` in `before_setup. The `Capybara.always_include_port` will allow the test to look at `127.0.0.1:port capybara picks` and properly redirect the test. Any application can override this by setting the `host!` in their system test helper. Generally though, applications are going to be using localhost. In this commit I also moved the setup and teardown into their own module for tidiness. * Move teardown settings into system test case These configuration options can be put into the system test case file instead of the generated system tests helper file. This is an implementation detail and therefore shouldn't be generated with the template.
This renames the system test helper file to be application system test case to match what the rest of Rails does. In the future we should consider changing the test_helper to match.
c8c6aa4
to
2d61c5d
Compare
Yeah. Listed at the end is fine. Or even doing a puts when this method is triggered can work. Agree, not a merge blocker.
… On Feb 20, 2017, at 21:03, Eileen M. Uchitelle ***@***.***> wrote:
@eileencodes commented on this pull request.
In actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb:
> + def take_screenshot
+ save_image
+ puts "[Screenshot]: #{image_path}"
+ puts display_image
+ end
+
+ # Takes a screenshot of the current page in the browser if the test
+ # failed.
+ #
+ # +take_failed_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
+ # sessions.
+ def take_failed_screenshot
+ take_screenshot unless passed?
+ end
I think that would be super cool, but we'd have to hook into Capybara to do that. Maybe I can find a way to include it underneath? I'll have to look at the test runner, and since this won't change the public API it's not a merge blocker.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
There were some grammar issues and incorrect information in the system tests documentation.
@eileencodes yeeees, so cool! ❤️ |
Though I like this, I would like to understand if/whether its easy to separate out the Unit tests from the functional tests as a separate |
@vraravam having a separate environment isn't something we'd do by default but you should be able to copy |
@pixeltrix - that's how I have been proceeding till now on all my rails codebases. But, I was trying to formalize the existence of such an env from the core framework itself. :) |
@vraravam if that's the case tacking it on the end of a PR with lots of comments is pretty much the worst place to discuss it - you'd be better off discussing it here: https://groups.google.com/forum/#!forum/rubyonrails-core |
Anyone have an example/instructions how to set this up with rspec-rails? |
@xanview I haven't tried setting it up with Rspec but this basically builds a simple shim between Rails and Capybara to make setup easier. I think you should be able to make the class the same way in Rails and then inherit from that in your test files. If you're having issues with Rspec and suspect it's a Rails bug please open an issue. If you're having issues and suspect it's how you're doing the setup in your app you can ask on the Rails mailing list instead. |
FWIW, in case there is anyone confused that the PR description reads that an (IMHO it's probably all for the good since otherwise people (like me) might try to get it working on earlier versions of Rails, and, after reading the code and related PRs, that would be quite the rabbithole, so I'm glad it wasn't made its own gem and saved me from putting time into that :)) cc @eileencodes |
Yes @bf4 creating a gem specifically for system tests didn't make sense in the end and it was moved back inside Action Pack and named Since this PR was open for 3 months I wouldn't really use it to determine how system tests work based on the comments. Please refer to documentation or the code itself as it is in the 5-1-stable /master branch and not this PR. A lot has changed since I merged it as well. |
Update: This PR changed a lot over the course of being open. Please refer to the documentation or code in master / 5-1-stable to use system tests
I'm really excited to open this initial PR for bringing system/acceptance test support to Rails through Capybara.
The goal of this PR is for Rails to take on all the setup that is needed in applications to allow Capybara and make system testing default in Rails applications.
Why is the name Rails::SystemTestCase?I chose this name because this was the name DHH indicated he preferred in the Rails' Basecamp long ago. I'm totally open to changing the name but ultimately decided to put it in ActionPack with the Rails namespace so I could get onto writing code and stop worrying about the name 😉The test framework has been moved it it's own gem under the Rails name and is now ActionSystemTest
Because the gem expects a Module and our test frameworks expect a Class for inheritance I've made the gem and module name
ActionSystemTest
and the class for inheriting test code fromActionSystemTestCase
. Open to other names as well but the Module and Class names can't be the same else Ruby gets confused.How do I add System tests to my application?
By default new applications include a base system test much like integration tests. Gemfiles generated for new applications will include Capybara and the Selenium driver.
Capybara's selenium driver requires some setup by the programmer, so I've added a layer between Capybara and Rails so that Rails' takes on that work called
RailsSeleniumDriver
. The default settings are as follows:The reason I have chosen Chrome as the default browser is because Firefox doesn't work out of the box. Selenium and the current version of Firefox don't play nicely together so I've set up the driver to default to Chrome, but it can easily be switched to using Firefox. I'd also like to provide support for Safari.
Instead of requiring the programmer to set up the port, browser, server, etc Rails handles that setup and frees up the programmer to work on writing their tests.
The Rails drivers allow the default configuration to be changed by initializing a new driver:
What if I don't want to use Selenium?
If the programmer wants to use one of Capybara's default drivers rather than the provided defaults in Rails Selenium configuration they can easily change that in the test environment. I've provided a shim in Rails so setting the Capybara driver is simple. I've named the class
CapybaraDriver
and it gives access to the 4 Capybara drivers:poltergeist
,webrick
,selenium
(with no setup), andrack_test
:Each of the Capybara default drivers that requires a server defaults to Puma. Any of the settings can be changed by initializing a new
CapbyaraDriver
object:What if I'm making a new application and don't want system testing at all?
It's easy to skip system testing files in the app generator or the scaffold generator by running with the
--skip-system-test
flag.What if I already use Capybara and don't want to use Rails?
Because Rails provides a specific test case name to inherit from you can easily just completely skip the Rails version of system testing and use Capybara directly.
I want to try it out!
I made a test app where you can try out system tests in Rails w/ Selenium! https://github.com/eileencodes/system_testing_app
Cool! What's next?
Below is a list of what is done and what's left to do. Once we sort out naming and other issues I'll finish up the items below.
What's done?
RailsSeleniumDriver
being the default.Future work
cc/ @dhh @georgeclaghorn