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
API for cookie manipulation? #327
Comments
I would like to see this too. I used to be able to use this to get the cookies:
But that seems to be broken in 1.0.0.beta1? Edit: Got it working by using |
And one that is standard across drivers... |
https://github.com/nruth/show_me_the_cookies is an attempt at this, but driver support varies. |
dmarkow thanks for the pointer re: racktest cookie jar, saved me half an hour or so. I've updated the gem/project mentioned to work with edge capy but had some strange behaviour from selenium to investigate. Contributions welcome if anyone thinks a gem is a good fit for this. I think it'd be better in core but on the mailing list there was little/no response to a post about it. Maybe a pull request would get more attention? |
I think both pull requests or list posts are good ways to Jonas's attention. Just don't be surprised if he takes a few days to get back -- he seems to work in spurts. ;-) Pull requests are good for working code that's ready to be merged. (And, this being open source, a pull request is generally a lot more effective than writing a feature request and waiting for someone else to implement it.) On the other hand, to start a discussion, the mailing list is the better place -- you can paste a diff (or link to GitHub) to discuss specific work-in-progress or proof-of-concept code. |
Really delayed comment on this: I don't think we'll do this for 1.0 but maybe for 1.1 it would be okay. I'm a bit worried though, because it goes a little bit against the user centric nature of Capybara. I guess it would be convenient for some use cases though, could someone write up an example where this feature would be useful? |
My use case was testing "Remember me" functionality provided by Devise. |
Malicious and "curious" users are uses too. It's quite easy for a user to On Thu, May 19, 2011 at 12:42 PM, nruth <
|
Have added a 'query only' possible api as a pull request, see pull #357. May be many good reasons why this is not a good approach so feel free to comment negatively...no hard feelings! |
This is hugely useful for testing affiliate cookie functionality:
This is an integration spec and right where Capybara is most useful. |
+1 for this, I'm using it in a similar scenario to Mike and have to hack quite a bit to get it working |
+1 Ive been hacking around these issues as well. |
Thanks for the get_cookie gist, but did anyone manage to do the set_cookie ? |
I don't think so I guess it's easy to do once you get hold of the cookie representation (or right api method for the driver) but haven't seen anything used. |
mmm, too bad. |
+1 |
I tried using this in a spec (the driver in this case is :rack_test):
But after much debugging discovered that the
Although an API might be slightly more readable, I think it might be better to just widely document that this second form is the way to test cookies. Details about my adventure debugging this one can be found here: |
+1 I have several Rails apps that shares a single sign on solution using cookies. Since the authentication function is tested elsewhere, I don't need to test it again in my app. I just need to set the cookie with the token that I need to log in and and go right into my app. Right now I have to write a little fake login screen just so I can get RSpec/Capabara requests tests to work. I tried:
But it's using get instead of visit so I can't do things like:
Having the ability to view and setting the cookies would be a great option. I checked out show_me_the_cookies gem but it doesn't have a way to set it. |
@lsj256go You should be able to adapt this piece of code I use to make login faster in tests to your problem: class ApplicationController
prepend_before_filter :quick_login_in_tests
private
def quick_login_in_tests
if user_id = params[:login_user_in_tests]
sign_in User.find(user_id)
end
end
end I have this in features/support/quick_login.rb here I sign the user in to an app using Devise but you should be able to set a cookie just as well. |
Here's another use case. We're writing cucumber integration tests for a PHP-based web app. We want to get code coverage reports for our cucumber/capybara tests. To do this, our code coverage tool needs us to set a header or cookie with a session ID that will stay the same over multiple requests. If we put this in a header, we might miss coverage for some ajax calls, but if we put it in a cookie it will work even for ajax calls. |
@betelgeuse Thanks for the tip. I actually did something similar, except that I set up a specific session controller and enabled only for test. That way no one can just put in a parameter and get access to your system in production. For example, if I visit http://myapp.com/login/username, the system will login as that user. But it only works when RAILS_ENV=test. |
+1 We need a standard cookie api. |
+1 |
Let's see some pull requests if people want this ;) Though I suspect that changes need to happen in RackTest if Capybara wants to implement this in a non-silly way. |
No pull requests yet. I'm closing this issue. I don't need it. I'm not going to fix it. Though if someone can provide a decent pull request I'd be happy to merge it. |
Would it make sense/be possible to support an API for cookie manipulation?
I don't know if it would work for all drivers... but it would certainly work for Rack, Selenium, and Webkit.
The text was updated successfully, but these errors were encountered: