使用请求库进行验收测试(python)

时间:2013-04-09 20:14:00

标签: python httprequest python-requests acceptance-testing

我在使用请求库编写登录功能验收测试时遇到问题。登录是使用post方法实现的,我无法直接更改查询字符串并操纵URL。我想要做的是手动提交帖子数据,并让页面重定向到用户成功登录时会定期访问的页面。我想检索该页面的html并检查登录成功,如何我会这样做吗?

此方法失败:

data={'email': 'example@example.com', 'password': 'example'}

login_url="examplepage.com/signin"

request_data = requests.post(login_url, data, allow_redirects=True)

print request_data.content

这是resp.headers返回的内容:

{'content-length': '1124', 'date': 'Sun, 14 Apr 2013 16:12:51 GMT', 'set-cookie': 'session="+udnwfCkuAuFGp9QKOiU1YS2X1s=?_fresh=STAwCi4=&_id=Uyc3XHhkZVx4Y2J3T1x4YjBceDkxeFx4MWNceGFhXHhkNFx4OTMhKVx4MGJyJwpwMQou&csrf=Uyc2Mzk0YjVjZjMzMGZkMTNkY2NiM2EzZTZkYzgyMjhkY2IwOWQ2NmM3JwpwMQou"; Path=/; HttpOnly', 'content-type': 'text/html; charset=utf-8', 'connection': 'keep-alive', 'server': 'Werkzeug/0.8.3 Python/2.7.3'}

resp.content只返回登录页面的html:

    <title>Web App</title>

  </head>
  <body>
    <div>Web App: <a href="/home">Home</a>

            | <a href="/signin">Login</a>
            | <a href="/register">Register</a>

    </div>
    <hr>




<h1>Sign in</h1>


<form action="" method=post name="signin">
    <div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value="20130414164251##fd7e82d47974518d098b41cecf2a4452f890317f"></div>
  <dl>

  <dt><label for="email">Email Address</label>
  <dd><input id="email" name="email" type="text" value="example@example.com">

  </dd>


  <dt><label for="password">Password</label>
  <dd><input id="password" name="password" type="password" value="">

  </dd>

  </dl>
  <p><input type="submit" value="Sign in">
</form>

<a href="/login"> Click here to sign in using your Google, Yahoo, AOL, Flickr, or another OpenID account. </a>


  </body>
</html>

3 个答案:

答案 0 :(得分:2)

如果您想登录某个页面,我建议您创建会话,尽管有独立请求。如果您正确发送数据您应该检索一些会话cookie。我会检查它以确保您已登录。当脚本登录并获取cookie时,它还应检索包含目标页面的Location标头。尝试这样的事情:

import requests
data={'email': 'example@example.com', 'password': 'example'}
login_url="examplepage.com/signin"
s = requests.session()
resp = s.post(login_url,data)
#now s.cookies should contain session cookie if properly logged in
target = s.get(resp.headers['Location'])

但我认为问题可能与会话有关。

答案 1 :(得分:0)

我的解决方案是在应用的CSRF_ENABLED文件中将config.py变量从 True 更改为 False 。一旦我更改了此设置,上面的代码实际上有效。顺便说一下,我正在Heroku上主持。

答案 2 :(得分:0)

我这样做了:

    if auth == True:
        # fetch the login page in order to get the csrf token
        cookieHandler = urllib2.HTTPCookieProcessor()
        opener = urllib2.build_opener(urllib2.HTTPSHandler(), cookieHandler)
        urllib2.install_opener(opener)

        login_url = URL
        login_page = opener.open(login_url)

        # attempt to get the csrf token from the cookie jar
        csrf_cookie = None
        for cookie in cookieHandler.cookiejar:
            if cookie.name == 'csrftoken':
                 csrf_cookie = cookie
                 break
        if not cookie:
            raise IOError("No csrf cookie found")

        # login using the usr, pwd, and csrf token
        login_data = urllib.urlencode(dict(
            username = username, password = password,
            csrfmiddlewaretoken = csrf_cookie.value))

        req = urllib2.Request(login_url, login_data)
        resp = urllib2.urlopen(req)
        contents = resp.read()

    target_download = target_url
    status_code = urllib2.urlopen(target_download).getcode()
    our_document = urllib2.urlopen(target_download).read()
    return status_code, our_document