简体   繁体   中英

How to set cookies in capybara with capybara-webkit

I'm using capybara with capybara-webkit for testing, but I need to set some specific cookies.

I'm so confused by it's document, and the source code.

I did find a method here https://github.com/thoughtbot/capybara-webkit/blob/e6e2351a15cabf620152eb938e5cacb514fe1529/lib/capybara/webkit/browser.rb#L177

But I really don't know what format should I use for the 'cookie' param in this method.

If I have a cookie in json like this:

[
    {
        "domain": ".github.com",
        "expirationDate": 1453495731,
        "hostOnly": false,
        "httpOnly": false,
        "name": "__utma",
        "path": "/",
        "secure": false,
        "session": false,
        "storeId": "0",
        "value": "58162108.1841781874.1390418256.1390423639.1390423639.1",
        "id": 1
    },
    {
        "domain": ".github.com",
        "expirationDate": 1406191731,
        "hostOnly": false,
        "httpOnly": false,
        "name": "__utmz",
        "path": "/",
        "secure": false,
        "session": false,
        "storeId": "0",
        "value": "58162108.1390423639.1.1.utmcsr=developer.github.com|utmccn=(referral)|utmcmd=referral|utmcct=/",
        "id": 3
    },
    ... ...
]

How to modify the above into the correct acceptable string format for capybara-webkit? Can anyone give me an example?

The set_cookie method expects a valid value for the Cookie header as specified by RFC 2109 .

The simplest value would be cookie_name=cookie_value . You can add other properties with a string like cookie_name=value; domain=example.com; path=/ cookie_name=value; domain=example.com; path=/ cookie_name=value; domain=example.com; path=/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM