railsware / rack_session_access

Rack middleware that provides access to rack.session environment
MIT License
257 stars 29 forks source link

Doesn't work with Selenium #24

Closed brettdonald closed 7 years ago

brettdonald commented 7 years ago

Rack_session_access is working fine when I use the default rack_test driver -- I write tests for individual features without having to go via the login page every time. When using the selenium driver, however, although the calls to page.set_rack_session and page.set_rack_session do complete successfully, my feature test fails because I get redirected to the login page. It's like Capybara is not communicating with Selenium/Firefox.

ayanko commented 7 years ago
brettdonald commented 7 years ago

Hi Andriy,

When I posted that issue on github, I was not seeing a Firefox window appear at all when I ran my test. I had Firefox 53 installed at that stage, along with version 3.4 of the selenium-webdriver gem. I have since downgraded to Firefox 47 and selenium 2.53 — and now a Firefox window appears, which I assume is a step forward, but now I’m getting the following error when I run the spec:

 Selenium::WebDriver::Error::WebDriverError:
   unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055)
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/firefox/launcher.rb:90:in `connect_until_stable'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/firefox/launcher.rb:55:in `block in launch'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/common/socket_lock.rb:43:in `locked'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/firefox/launcher.rb:51:in `launch'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/firefox/bridge.rb:43:in `initialize'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/common/driver.rb:53:in `new'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/common/driver.rb:53:in `for'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver.rb:84:in `for'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/capybara-2.14.0/lib/capybara/selenium/driver.rb:22:in `browser'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/capybara-2.14.0/lib/capybara/selenium/driver.rb:62:in `visit'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/capybara-2.14.0/lib/capybara/session.rb:269:in `visit'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/rack_session_access-0.1.1/lib/rack_session_access/capybara.rb:6:in `set_rack_session'
 # ./spec/support/api_helper.rb:81:in `setup_capybara_session'
 # ./spec/features/hub/wwcc_verifications.rb:89:in `block (2 levels) in <top (required)>'

So, it looks like my troubles could be related to compatibility or configuration, rather than any problem in rack_session_access.

Cheers, Brett

On 25 May 2017, at 3:18 pm, Andriy Yanko notifications@github.com wrote:

What is your authentication engine and version? Can you provide minimal rspec feature example in order to reproduce your problem? — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/railsware/rack_session_access/issues/24#issuecomment-303924965, or mute the thread https://github.com/notifications/unsubscribe-auth/AES7HEqRaDYUYnsmRD7NAUNs06mKWHccks5r9Q8igaJpZM4Nl52O.

brettdonald commented 7 years ago

Okay, I’m now on Firefox 45 ESR, and things are improving for me. I now have connectivity between Selenium and Firefox. I can see the robot working.

I can even see rack_session_access doing things in the browser before the login page is displayed. But I’m not requesting the login page, I’m requesting a different page, hoping that rack_session_access will set up my session so I don’t need to login.

On 25 May 2017, at 3:39 pm, Brett Donald brett.donald@gmail.com wrote:

Hi Andriy,

When I posted that issue on github, I was not seeing a Firefox window appear at all when I ran my test. I had Firefox 53 installed at that stage, along with version 3.4 of the selenium-webdriver gem. I have since downgraded to Firefox 47 and selenium 2.53 — and now a Firefox window appears, which I assume is a step forward, but now I’m getting the following error when I run the spec:

 Selenium::WebDriver::Error::WebDriverError:
   unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055)
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/firefox/launcher.rb:90:in `connect_until_stable'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/firefox/launcher.rb:55:in `block in launch'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/common/socket_lock.rb:43:in `locked'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/firefox/launcher.rb:51:in `launch'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/firefox/bridge.rb:43:in `initialize'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/common/driver.rb:53:in `new'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/common/driver.rb:53:in `for'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver.rb:84:in `for'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/capybara-2.14.0/lib/capybara/selenium/driver.rb:22:in `browser'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/capybara-2.14.0/lib/capybara/selenium/driver.rb:62:in `visit'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/capybara-2.14.0/lib/capybara/session.rb:269:in `visit'
 # /Users/brettdonald/.rvm/gems/ruby-2.3.1@firefly/gems/rack_session_access-0.1.1/lib/rack_session_access/capybara.rb:6:in `set_rack_session'
 # ./spec/support/api_helper.rb:81:in `setup_capybara_session'
 # ./spec/features/hub/wwcc_verifications.rb:89:in `block (2 levels) in <top (required)>'

So, it looks like my troubles could be related to compatibility or configuration, rather than any problem in rack_session_access.

Cheers, Brett

On 25 May 2017, at 3:18 pm, Andriy Yanko <notifications@github.com mailto:notifications@github.com> wrote:

What is your authentication engine and version? Can you provide minimal rspec feature example in order to reproduce your problem? — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/railsware/rack_session_access/issues/24#issuecomment-303924965, or mute the thread https://github.com/notifications/unsubscribe-auth/AES7HEqRaDYUYnsmRD7NAUNs06mKWHccks5r9Q8igaJpZM4Nl52O.

ayanko commented 7 years ago

So what is your problem?

This gem only manages your rack session. So if you correctly set session key and value used by your authentication engine (devise,authlogic, custom) then you can access protected url as well.

brettdonald commented 7 years ago

I guess I want to know if rack_session_access only works with rack_test driver, or whether it also works with the selenium driver and firefox?

On 25 May 2017, at 9:53 pm, Andriy Yanko notifications@github.com wrote:

So what is your problem?

This gem only manages your rack session. So if you correctly set session key and value used by your authentication engine (devise,authlogic, custom) then you can access protected url as well.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

ayanko commented 7 years ago

Its driver agnostic! It means with any driver that supports HTTP. rack-test, capybara-webkit, selenium (any browser)

ayanko commented 7 years ago

Any update?

brettdonald commented 7 years ago

Hi Andriy,

Thanks for following up. I’m stuck. It doesn’t work and I’m not sure how to troubleshoot because I’m new to Rails and RSpec and Capybara and Rack and everything.

So here’s what I do know:

Rails establishes user sessions by default, assigns random session IDs to them, and sends this information in a cookie to the user’s browser. When a user logs in normally, our app stores the user ID of the logged-in user in the session using an assignment like this: session[:current_user_id] = @user.id For pages that our app wants only to be accessible to logged in users, we check that the session contains a user ID, and if not, we redirect the user to the login page. If our authentication service is not working (which happens sometimes), I can bypass authentication by editing the app code to assign a user ID to the session. RSpec is a framework for automating tests Capybara is a framework for automating feature tests, simulating a real user accessing the application via a web browser. We are planning to write many feature tests, and when we run the suite of tests, we don’t want every one of those tests to have to authenticate first, before the test is run. We only need to test authentication once, not dozens of times. Because Capybara runs “outside” of Rails I can’t modify the session the same way I could if I was in Rails This is where rack_session_access comes in. It allows me to say page.set_rack_session(current_user_id: @user.id) For feature tests which don’t involve Javascript, we can use the default Capybara/RackTest setup, and rack_session_access does the job, setting up the session so we don’t have to authenticate. We can test just the page we want to test, without having to worry about logging in. Beautiful. But for feature tests which DO involve Javascript, which of course will be most of them, we set “:js => true” which switches Capybara over to Selenium. Selenium starts Firefox and we see a couple of pages, involving rack_session, then instead of seeing the page we are trying to test, we are seeing the login page. Refer screen shots below (I hope github can handle embedded images).

Note that our application actually uses “contact_id” instead of “user_id”. Don’t ask. It appears from the second screen shot that rack_session_access has actually done what we asked it to, which was to set the contact_id attribute. But we still end up on the login page instead of the page we are trying to test.

So if you have any advice on how I can debug this, I would love to have some help!

Cheers, Brett

On 31 May 2017, at 11:38 pm, Andriy Yanko notifications@github.com wrote:

Any update?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/railsware/rack_session_access/issues/24#issuecomment-305189224, or mute the thread https://github.com/notifications/unsubscribe-auth/AES7HKC57qdSMoy3gTmJOSjmVQTUz5ynks5r_W1tgaJpZM4Nl52O.

ayanko commented 7 years ago

Make sure you understand how it works:

jplethier commented 7 years ago

I am having some similar issue here, I am setting session[:current_user_id] using rack_session_access gem methods(set_rack_session(current_user_id: 1)), but on my controllers the session does not have this value neither has this key set.

I am using a custom authentication engine, just accessing session on controller and gets the current_user_id(session[:current_user_id]), and I am trying to use this gem on a feature tests helper setting the session. The gem method returns true, so I expected to work, but when controllers receive the request the session is not setup.

jplethier commented 7 years ago

Sorry, I found what was going wrong with my tests. It was because I use subdomains, so I had to set the correct the capybara host before calls set_rack_session. Fixing this all is working now.

ayanko commented 7 years ago

The gem method returns true, so I expected to work, but when controllers receive the request the session is not setup.

This gems is only for acceptance (spec/features) testing. For spec/controllers or spec/requests) you should use helper provided by your authentication engine.

jplethier commented 7 years ago

I am using this for spec features

ayanko commented 7 years ago

So... What is the issue?

jplethier commented 7 years ago

Like I said before, in my case the issue was that I am working with multiple domains, so I was setting a session before setting the domain, and then when I access the page in feature tests using the domain, the session for this domain was not setup. I fixed this setting the capybara host to correct domain before using this gem, this solved my issue.

ayanko commented 7 years ago

No actual problem provided.