[rspec-users] [cucumber] checking if a user is logged in (Was: webrat+selenium integration problem: record n)
Ben Mabey
ben at benmabey.com
Sun Apr 5 16:30:18 EDT 2009
Mateusz Juraszek wrote:
> Balint Erdi wrote:
>
>> Ben Mabey wrote:
>>
>>
>>> Couple things I'd like to point out. In your enhanced.rb you don't need
>>> to do the Before hook yourself. You can just require
>>> 'database_cleaner/cucumber'. I've updated your gist to use that.
>>> In your plain.rb it seems like you are trying to truncate your database
>>> just once upon startup. If that is the case then the recommended way is
>>> to use the clean_with method like so:
>>> DatabaseCleaner.clean_with :truncation
>>>
>>> Again, I have updated the gist to reflect this.
>>>
>>> -Ben
>>>
>> Hey Ben, thanks a lot.
>>
>> The problem I am experiencing now is that information stored in the
>> session does not seem to be retained between steps (again, only in the
>> case of selenium sessions, plain sesssion work fine). So the login
>> function works fine now but when I go to another page afterwards it
>> throws a big error because the action tries to render something based on
>> the current user.
>>
>> Is the session store I am using relevant? I used the default
>> cookie-based storage and then tried to change to the active-record based
>> one to no avail. My config is at http://gist.github.com/83635
>>
>> Thank you,
>> Balint
>>
>
> hi Ben
>
> I am newbie with cucumber and bdd. I was wondering if you can take a
> look on my problems.
> I have similar problem like Balint. I test my own and clearance features
> with selenium and webrat. I don't have to say that with werbrat
> everything is perfect. When I run features with selenium I get error
> nil.session what is connected with code from clearance features ie:
> <pre>
> Then /^I should not be signed in$/ do
> assert_nil request.session[:user_id]
> end
> </pre>
>
Yuck! I realize that you didn't write this and that Clearance gave you
this step out of the box:
http://github.com/thoughtbot/clearance/blob/fa424b8fe9fd8f151f32a726982a3fd42940b328/generators/clearance_features/templates/features/step_definitions/clearance_steps.rb
It is, IMO, a very bad way to verify if the user is logged in. (I
actually used an example very similar to this in my MWRC
presentation[1]. One of the reasons that makes it bad is that it makes
it difficult to switch from using rails integration session to an
automated browser solution like Selenium.
> in clearance_step file
>
> It seems like selenium has problem with access to "request" object
> That makes my test fail all the time.
>
Yep, that object exists in rails integration sessions, which is what
webrat's :rails mode uses. To avoid this error you will need to specify
behaviour, not implementation. I'm guessing that this step is called
after you have logged out. So, the way I would test this is that I
would test it from the point of view of the user. The user doesn't know
about a session, much less how we are implementing our authentication
system. :) When this user logs out they probably see a message
indicating that they have been logged out though. So, something like
this would be better:
Then /^I should not be signed in$/ do
response.should contain("You have been logged out.")
end
(Note: I'm not sure if clearance says this exactly, but you get the idea...)
Using a step like this will allow you to switch between multiple adapters since you should always have the response object.
> Another problem I met is with waiting for response after "press button"
> method
> All the time I get response with code before button pressed
> It's connected with selenium_session.rb code and require me to change a
> bit
> <pre>
> def click_button(button_text_or_regexp = nil, options = {})
> if button_text_or_regexp.is_a?(Hash) && options == {}
> pattern, options = nil, button_text_or_regexp
> elsif button_text_or_regexp
> pattern = adjust_if_regexp(button_text_or_regexp)
> end
> pattern ||= '*'
> locator = "button=#{pattern}"
>
> selenium.wait_for_element locator, :timeout_in_seconds => 5
> selenium.click locator
> selenium.wait_for_page_to_load(5) ## add this line to get correct
> response
> end
> </pre>
> I don't know why I have to change it
>
Hmmm.. Yeah, I don't know about that. You should probably ask the
webrat mailing list or open up a ticket[2].
-Ben
1.
http://mwrc2009.confreaks.com/14-mar-2009-15-00-bdd-with-cucumber-ben-mabey.html
2. http://wiki.github.com/brynary/webrat/get-in-touch
More information about the rspec-users
mailing list