How to add cookies with ruby Capybara

1 min


637
637 points
How to add cookies with ruby Capybara
How to add cookies with ruby Capybara

How to add cookies with ruby Capybara

Way back I wrote an article how to add cookies with selenium webdriver , many people asked How to add cookies with ruby Capybara. So here we go … Before we get started lets just have a quick recap about what is a http cookie :

An HTTP cookie (also called web cookieInternet cookiebrowser cookie, or simply cookie) is a small piece of data sent from a website and stored on the user’s computer by the user’s web browser while the user is browsing

The questions is , why do you need to even touch this part ? Well image you have a cookie banner on the website you are testing and thats annoyingly following your selenium tests everywhere around and sometimes you get element is not clickable at point error . Given the fact that most of our tests doesn’t really care about that cookie banner you can actually pre-set a cookie value before you start your tests and you get read if it.

    def add_cookie(name, value, domain = '.example.com')
      Capybara.current_session.driver.browser.manage.add_cookie(name: name, value: value, domain: domain)
    end

And how to use the method :

add_cookie('cookie_policy_acceptance', '1')

Happy testing!


Like it? Share with your friends!

637
637 points

What's Your Reaction?

cute cute
0
cute
scary scary
0
scary
wtf wtf
0
wtf
geek geek
0
geek
WIN WIN
0
WIN
Love Love
0
Love
OMG OMG
0
OMG
Angry Angry
0
Angry
Lol Lol
0
Lol
Test engineer

0 Comments

Your email address will not be published. Required fields are marked *

Choose A Format
Trivia quiz
Story