简体   繁体   English

如何使用capybara-webkit在capybara中设置cookie

[英]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. 我将capybara与capybara-webkit一起用于测试,但是我需要设置一些特定的cookie。

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 我确实在这里找到一种方法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. 但是我真的不知道在这种方法中我应该为“ cookie”参数使用哪种格式。

If I have a cookie in json like this: 如果我在json中有这样的cookie:

[
    {
        "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? 如何将以上内容修改为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 . set_cookie方法期望由RFC 2109指定的Cookie头有效值。

The simplest value would be cookie_name=cookie_value . 最简单的值是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=/ cookie_name=value; domain=example.com; path=/ . cookie_name=value; domain=example.com; path=/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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