Capybara & Cucumber | Getting cookies
Step Definitions
Then /^cookies should be set^/ do
Capybara
.current_session # Capybara::Session
.driver # Capybara::RackTest::Driver
.request # Rack::Request
.cookies # { "author" => "me" }
.[]('author').should_not be_nil
end
This works, however, I'm still looking for a less verbose way. Moreover, I'd like to know how to get the session data in a step definition.Updated
To get the session data one should do the following:
Step Definitions
Then /^session data should be set$/ do
cookies = Capybara
.current_session
.driver
.request
.cookies
session_key = Rails
.application
.config
.session_options
.fetch(:key)
session_data = Marshal.load(Base64.decode64(cookies.fetch(session_key)))
session_data['author'].should_not be_nil
end
This is quite verbose too. Rails + Cucumber/Capybara: How to set/retrieve cookies in tests?
So eventually I figured it out after trying a lot of different things.
Given(/^I didn't log out the last time I was on the site$/) do
user = FactoryGirl.create(:user)
visit new_user_session_path
fill_in('user[email]', with: user.email)
fill_in('user[password]', with: 'test123')
click_button('Sign in')
Capybara.current_session.driver.request.cookies.[]('auth_token').should_not be_nil
auth_token_value = Capybara.current_session.driver.request.cookies.[]('auth_token')
Capybara.reset_sessions!
page.driver.browser.set_cookie("auth_token=#{auth_token_value}")
end
When(/^I go to the homepage$/) do
visit root_path
end
Then(/^I should automatically be logged in$/) do
page.should have_content("Logout")
end
UPDATE:Here's what I use in case I'm using Selenium for some of the tests:
if Capybara.current_session.driver.class == Capybara::Selenium::Driver
auth_token = page.driver.browser.manage.cookie_named('auth_token')[:value]
page.driver.browser.manage.delete_all_cookies
page.driver.browser.manage.add_cookie(:name => "auth_token", :value => auth_token)
else
puts "cookies = #{Capybara.current_session.driver.request.cookies}"
Capybara.current_session.driver.request.cookies.[]('auth_token').should_not be_nil
auth_token_value = Capybara.current_session.driver.request.cookies.[]('auth_token')
Capybara.reset_sessions!
page.driver.browser.set_cookie("auth_token=#{auth_token_value}")
end
Cookies within a cucumber test using capybara
here's a nice way to show the content of the cookies while running your feature
https://gist.github.com/484787
How do I get cookies from a capybara-webkit session?
The duplicate question pointed me in the right direction but it was too noisy and I eventually consulted the docs for Selenium::WebDriver::Options
and Selenium::WebDriver::Driver
The following is how we get cookies out.
puts session.driver.browser.manage.all_cookies
I want to delete a particular cookie using Capybara within Cucumber test and don't want to use Webkit / Poltergeist
You need to be on a page where the cookie would be valid in order to interact with the cookies (WebDriver spec limitation). Assuming that https://mylearnwebsite.com/
is the site where the cookie is valid and you're using the selenium driver then
visit "https://mylearnwebsite.com/"
page.driver.browser.manage.delete_cookie 'my_cookie' # this is driver specific
page.refresh # to reload the page without the cookie
...
would delete the cookie. Since you haven't shown what you're actually checking after removing the cookie it's hard to guess what behavior you're looking to actually test, so the code above is just calling refresh
to see the page state without the cookie.Noe: From a general system/feature testing purpose it's usually not valuable to manually interact with cookies, you're generally better off just testing the user behaviors which have a side-effect of modifying cookies.
Getting Net::ReadTimeout: visiting a website in Ruby Capybara Cucumber
Capybara::Selenium::Driver.new( app,browser: :chrome, desired_capabilities: { 'chromeOptions' => { 'useAutomationExtension' => false, 'args' => ['--disable-web-security', '--start-maximized', '--disable-infobars'] } })
Unable to click the rating image via capybara using selenium-cucumber
In order to click on a specific star you'd need to do
find("#user_rating img[alt='2']").hover.click # select star 2
So in a cucumber step that would be something likeThen /^I rate ([^ ]+) with (\d) stars$/ do |id, stars|
find("##{id} img[alt='#{stars}']").hover.click
end
and be called as Then I rate user_rating with 4 stars
Related Topics
How to Tell a Ruby Method to Expect a Specific Parameter Type
How to Resize Image Only If Width Exceeds with Graphics/Image Magick
Ruby/Rails 3.1: Given a Url String, Remove Path
International Chars Using Rspec with Ruby on Rails
What Is 'stringify_Keys' in Rails and How to Solve It When This Error Comes
Starting with Redmine Locally - How Easy Is Migration to Server Later
One or More Params in Model Find Conditions with Ruby on Rails
How to Check If an Object Is Nil in a View in Ruby
Indent Multiline String in Erb
How to Get All Message History from Hipchat for a Room via The API
How to Handle 404 Not Found Errors in Nokogiri
How to Set in a Middleware a Variable Accessible in All My Application
Crontab Not Running Ruby Script
How to Render Blob Images in a Prawn Document