-
-
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
Can't access example's driver in before hook for system tests #2550
Comments
I guess this is due to the hook execution order, similar to #2526. I suggest giving the |
I found a workaround by overriding the module RSpec::Rails::SystemExampleGroup
def initialize(*args, &blk)
super(*args, &blk)
self.class.before do
# this gets executed twice, the first time the driver is incorrect but the second time the driver of the example block
puts page.driver
end
end I know this is not ideal, but maybe it helps understaing what's going on, I'm not familiar on how rspec works. |
I tried with the include_context trick from the other issue and it didn't work. Also tried with a |
I created a sample app with the 3 methods I tried (before hook, shared context before hook, monkeypatched initialzie method) https://github.com/arielj/HooksPageDriver/ the details are on the README file and inside spec/rails_helper.rb as a comment |
A quick non-systematic solution would be to explicitly set the driver in a sibling hook: RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :selenium_headless # I'm on Firefix, just :selenium should work for you
end
config.before(:each, type: :system) do
puts "before hook: #{page.driver}"
end Normally, the driver is set here, but it's possible to set it before this runs. |
Great, that workaround is much cleaner than the hacky monkeypatch that worked for me. I changed it to: config.before(:each, type: :system) do
driven_by Capybara.javascript_driver
end I think it can make it easier to maintain. Thanks! |
I'm going to close this as its a hook ordering issue but has a workaround, there are other open issues tracking this (like #2526) |
(I'd also be happy to accept a PR adding this caveat as documentation, e.g. something in the system test feature about how if you need to ensure a certain driver has been set to do this in the hook) |
What Ruby, Rails and RSpec versions are you using?
Ruby version: 2.6.5
Rails version: Rails 6.1.4.1
RSpec version: rspec-core (3.10.1) and rspec-rails (5.0.2)
Observed behaviour
I'm trying to interact with the selenium driver to use the
intercept
feature https://github.com/SeleniumHQ/selenium/blob/selenium-4.0.0/rb/lib/selenium/webdriver/common/driver_extensions/has_network_interception.rb#L63. I want to intercept all the requests done by the browser (my idea is to warn the users if the js code is doing any external request) so I'm trying to setup theintercept
in a before hook.The problem is that
page.driver
is a Capybara::RackTest::Driver in the before hook but it's a Capybara::Selenium::Driver during the actual example code, so I can't really interact with the example's driver before the test.Expected behaviour
I would expect the before(:example) hook to have access to the same
page.driver
of the example.Can you provide an example app?
I don't have an example app, but it's pretty easy to reproduce. With an example that requires javascript (so the driver is not RackTest), runpage.driver
in abefore(:each)
block for that example and then runpage.driver
when already in the example block. The first will show a:EDIT: added a sample app to reproduce this, the README includes the instructions for the 3 methods I tried with RSpec and the config for Minitest too to compare https://github.com/arielj/HooksPageDriver/
Some debugging
I've been doing some debugging and I got to here https://github.com/rspec/rspec-core/blob/3-10-maintenance/lib/rspec/core/example.rb#L262. If I do
@example_group_instance.page.driver
I get the RackTest driver, but when I step inside the next instruction (withstep
in byebug) I'm taken directly inside the example bock, where I can runpage.driver
to get the correct one.I also tried with MiniTest and it works fine using an
after_setup
hook, in that casepage.driver
is the same driver the test uses.The text was updated successfully, but these errors were encountered: