-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Integrate with ActionDispatch::SystemTest #1813
Conversation
Great work! Any estimate on when this will be merged? |
@elsurudo we'd gladly accept help on this, in the mean time our |
This should JustWork™ in Rails 5.1+. I’m tracking rspec/rspec-rails#1813 so We can eventually integrate system specs (the now “official” Rails way) instead of feature specs.
38d951e
to
3bc329d
Compare
This is now ready for user testing. Please let me know if you'd like to use this and give me some feedback |
97eb91f
to
6b68f6a
Compare
@JonRowe would you be able to provide review here? |
I tried this out on a Rails 5.1 project and got things mostly working. Here was my general process:
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :rack_test
end
config.before(:each, type: :system, js: true) do
driven_by :headless_chrome # a driver I define elsewhere
end
end ResultsIt kinda worked... I had a couple of problems.
The first problem stems from the use of I also encountered what I consider to be an unfortunate, if minor, annoyance. I like my test output to be as clean as possible -- just the test status indicators with no output. System tests seem to muck this up quite a bit. See: The first bit of output is from puma starting up, presumably when RSpec hits its first system test. I'm glad to see it doesn't log all requests, but it'd sure be awesome if we could suppress this output somehow. Then you see my test failures. The screenshot writes a log in-line. The "RSpec-friendly" way to do this would be to include that output with the detailed failure message. I'm guessing that these annoyances are mostly out of RSpec's control and have to do with how system tests are implemented in Rails. Is there something we could contribute back to Rails to make the output play nicer? It seems to be that if this outputs during minitest runs, it'd be equally annoying for folks like me? Maybe I'm just too much of a neat freak? |
Poltergeist is no longer maintained Rails 5.1 introduces ActionDispatch::SystemTestCase which should allow our feature tests to be speed up when this is done: rspec/rspec-rails#1813
Poltergeist is no longer maintained Rails 5.1 introduces ActionDispatch::SystemTestCase which should allow our feature tests to be speed up when this is done: rspec/rspec-rails#1813
Poltergeist is no longer maintained Rails 5.1 introduces ActionDispatch::SystemTestCase which should allow our feature tests to be speed up when this is done: rspec/rspec-rails#1813
Poltergeist is no longer maintained Rails 5.1 introduces ActionDispatch::SystemTestCase which should allow our feature tests to be speed up when this is done: rspec/rspec-rails#1813
I've followed the steps @derekprior describes in his comment above. Things work smoothly, besides the custom
The driver isn't actually switched to |
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.
Nice to see how simple this is, few minor comments.
if /5(\.|-)1/ === RAILS_VERSION || "master" == RAILS_VERSION | ||
gem 'capybara', '~> 2.13', :require => false | ||
else | ||
gem 'capybara', '~> 2.2.0', :require => false |
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 doesn't bundler automatically work this out?
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.
The pin to 2.2.0
prevents us from bumping further than the highest 2.2.x
rails needs at least 2.13
. However, we pin to the earliest version that we support, so this is why this is conditionaled like this.
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.
👍
when /stable$/ | ||
gem_list = %w[rails railties actionmailer actionpack activerecord activesupport] | ||
gem_list << 'activejob' if version > '4-1-stable' | ||
gem_list << 'actionview' if version > '4-0-stable' | ||
if RUBY_VERSION >= "1.9.3" | ||
gem_list << 'puma' if version > '5-0-stable' |
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.
5-0
or 5-1
@@ -20,6 +20,7 @@ | |||
gsub_file 'Gemfile', /^.*\bgem 'rails.*$/, '' | |||
gsub_file "Gemfile", /.*web-console.*/, '' | |||
gsub_file "Gemfile", /.*debugger.*/, '' | |||
gsub_file "Gemfile", /.*puma.*/, "" |
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.
Will every generated app now need puma, shouldn't this be in a guard statement?
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 deletes the generated puma requirement from the default rails generator, replacing it with ours.
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.
Copy
the default `driven_by(:selenium)` from Rails. If you want to override this | ||
behaviour you can call `driven_by` manually in a 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.
Excess white space, my old nemesis, we meet again.
We encourage you to familiarse yourself with their documentation. | ||
|
||
RSpec **does not** use your `ApplicationSystemTestCase` helper. Instead it uses | ||
the default `driven_by(:selenium)` from Rails. If you want to override this |
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.
Might it be worth us overriding this and defaulting to 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.
Strongly disagree, selenium is the default in Rails and so I think it's better to mirror that default.
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.
Your call, just a suggestion
This is motivated by our usage of system test in RSpec. Puma lazily boots the first time a system test is used, but this causes some unfortunate output to appear in the middle of the user's green dots. An example of this can be seen in @derekprior's comment [here](rspec/rspec-rails#1813 (comment)). There are alternatives in RSpec where we attempt to intercept the puma boot and prevent the output from being made there, but that would involve some monkey patching. This seems like a cleaner solution.
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'm happy if you're happy @samphippen merge on 📗
|
||
System specs are RSpec's wrapper around Rails' own | ||
[system tests](http://guides.rubyonrails.org/testing.html#system-testing). | ||
We encourage you to familiarse yourself with their documentation. |
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.
s/familiarse/familiarise/
A fresh Rails 5.1 app was generated with: ``` rails new . -d postgresql --skip-action-mailer --skip-spring --skip-listen --skip-coffee --skip-turbolinks --skip-test --skip-bundle --skip-keeps ``` Updated configuration files were added for the following: - Code Climate - EditorConfig - Rspec - Rubocop - Travis CI For now, the app will use Rspec feature specs instead of the new Rails 5.1 system tests pending resolution of this rspec/rspec-rails PR: rspec/rspec-rails#1813
A fresh Rails 5.1 app was generated with: ``` rails new . -d postgresql --skip-action-mailer --skip-spring --skip-listen --skip-coffee --skip-turbolinks --skip-test --skip-bundle --skip-keeps ``` Updated configuration files were added for the following: - Code Climate - EditorConfig - Rspec - Rubocop - Travis CI For now, the app will use Rspec feature specs instead of the new Rails 5.1 system tests pending resolution of this rspec/rspec-rails PR: rspec/rspec-rails#1813
A fresh Rails 5.1 app was generated with: ``` rails new . -d postgresql --skip-action-mailer --skip-spring --skip-listen --skip-coffee --skip-turbolinks --skip-test --skip-bundle --skip-keeps ``` Updated configuration files were added for the following: - Code Climate - EditorConfig - Rspec - Rubocop - Travis CI For now, the app will use Rspec feature specs instead of the new Rails 5.1 system tests pending resolution of this rspec/rspec-rails PR: rspec/rspec-rails#1813
Poltergeist is no longer maintained Rails 5.1 introduces ActionDispatch::SystemTestCase which should allow our feature tests to be speed up when this is done: rspec/rspec-rails#1813
Poltergeist is no longer maintained Rails 5.1 introduces ActionDispatch::SystemTestCase which should allow our feature tests to be speed up when this is done: rspec/rspec-rails#1813
I'm having trouble with selenium-driven tests not seeing records in the database. Consider the following:
This test passes when RSpec.configure is set to Using Rails 5.1.4, RSpec 3.7.0, selenium-webdriver 3.6 and capybara 2.15.4, and |
@moveson Prior to Rails 5.1 that would have been caused by running transactional testing, and not having database_cleaner installed. In Rails 5.1 however the DB connection is shared between threads in testing mode, thereby allowing transaction mode (and obviating the need for database_cleaner in most cases). Since you're using 5.1 your issue would tend to imply whatever server you're using with Capybara isn't running in process (so the DB connection can be shared). What server are you using? If puma, check the output produced for something like "clustered mode" and fix it to run in single mode. |
@twalpole I'm using puma in dev and production. I don't see the familiar puma output on startup in my RSpec log. How can I tell what server is running in the test environment? In any case, to be safe, I changed my |
@moveson You actually need workers 0 to ensure in process mode - there's a PR that was accepted into the next Rails 5.1 release to try and ensure it gets set that way - rails/rails@bc02a01 . You should be able to get the Puma output back by setting |
@twalpole Setting workers to 0 and threads to 0,1 did the trick. Thank you! Do you happen to know an elegant way to set these values in the test environment without affecting my settings in dev/prod? |
@moveson - You could monkey patch in the rails change I linked above (or run from the 5-1-stable branch), or configure a different config file for puma --- Puma reads it's config from a number of possible places, try |
Got it. Again, thanks! |
I'm currently having some trouble get this running with a dockerized application (while the same application works fine without docker involved). In the docker case the tests are driven_by a selenium remote driver, with a chrome standalone docker container. So far the interaction between the two containers (one with the web application, one with the browser) works (as I can tell from the created screenshots), except for stubbing of authentication (the session is just not logged in via login_as). I already tried adjusting puma settings in the docker container (worked fine for the non-docker-case), but the symptoms of the problem are quiet different (with more then one threads the authentication problem occurs in a number of cases only, in this case it fails on every try). Anybody an idea? Could it be that the in other places suggested disabling of |
@aptituz You don't fully define your If you're referring to that If, instead, you are running the tests on the docker instance and letting Capybara start the AUT on the docker instance all in one process (but the browser is in a different docker instance) then you should be fine, and probably just have a configuration issue. |
@twalpole You are right. Thanks that you ask for clarification. I'm referring to the Warden provided |
I just wanted to drop a note that I got it running eventually. So, thanks again for your help. For some reasons I don't remember I had a |
I have a problem with system tests: RSpec.describe "Authentification", type: :system, js: true do
let(:valid_attributes) { attributes_for(:user) }
before :each do
@user = User.create!(valid_attributes)
visit '/users/login'
end
it "Enable second factor auth" do
sign_in_with(valid_attributes[:email], valid_attributes[:password])
@user.update! direct_otp: "111111" # on js: true, the user is not updated on the browser database
activate_second_factor(@user.role.name, @user.direct_otp)
expect(page).to have_content 'Se ha activado correctamente el segundo factor de autentificación.'
end
end This example only works when javascript is disabled, when I add Any suggestion on why this is happening? Rails 5.1.4 |
@Xosmond Assuming you're using Rails 5.1+, if the app code isn't seeing DB changes made in the test code it's usually that you have the app running in a separate process rather than separate thread. Turn off the silencing of puma that rspec-rails does by default (it's a bad default option for beginners trying to troubleshoot) and see if the output puma then generates tells you it's running in 'single mode' (as opposed to cluster mode). If it doesn't you need to check where/how your puma is getting it's config from and make sure it's set to run with 0 processes and some number >= 1 threads. |
@twalpole I have turn off the silencing and I got:
Thats why I can login with the user created via code, but the app is not seeing the later change (user.update!) done by the test code on the But if I do that change on the RSpec.describe "Authentification", type: :system, js: true do
let(:valid_attributes) { attributes_for(:user) }
before :each do
@user = User.create!(valid_attributes)
@user.update! direct_otp: "111111"
visit '/users/login'
end
it "Enable second factor auth" do
sign_in_with(valid_attributes[:email], valid_attributes[:password])
activate_second_factor(@user.role.name, @user.direct_otp)
expect(page).to have_content 'Se ha activado correctamente el segundo factor de autentificación.'
end
end Something happens after the before macro. |
@Xosmond Ok, since it is running in single mode it's probably a timing issue with the way your tests are written (or possible you aren't actually running in transaction mode). This really isn't the right place for that discussion. You should post over on stack overflow, add a |
@twalpole I'm using |
Poltergeist is no longer maintained Rails 5.1 introduces ActionDispatch::SystemTestCase which should allow our feature tests to be speed up when this is done: rspec/rspec-rails#1813
Poltergeist is no longer maintained Rails 5.1 introduces ActionDispatch::SystemTestCase which should allow our feature tests to be speed up when this is done: rspec/rspec-rails#1813
Poltergeist is no longer maintained Rails 5.1 introduces ActionDispatch::SystemTestCase which should allow our feature tests to be speed up when this is done: rspec/rspec-rails#1813
Poltergeist is no longer maintained Rails 5.1 introduces ActionDispatch::SystemTestCase which should allow our feature tests to be speed up when this is done: rspec/rspec-rails#1813
TODO:
infer_spec_type_from_file_location