背景:
最近由于项目需要进行接口自动化,自己也很久没用过了,于是自己网上找了一些实例。有一个需要传递cookie的接口:先登录网站,响应头set-cookie返回sessionid,在后续的修改用户等接口中需要带上该sessionid。于是自己网上参考了一些文章,利用requestlibrary跟httplibrary.http这两个库中的关键字写了两种方法,用例有些繁琐,可能思路有些绕弯了,希望有大佬指点一二,感激不尽。
用到的library:
Library RequestsLibrary
Library HttpLibrary.HTTP
Library String
第一种:
思路:
先调用登录接口,然后利用登录的create session,发起查询请求。因为set-cookie返回的内容会保存在客户端,这样客户端发起相关请求的时候就会使用相应的值。这样在一个测试套中只需要执行一次登录,后面直接调用就可以了。
代码:
0-登录
${heards} Create Dictionary Content-Type=application/x-www-form-urlencoded
Create Session login http://127.0.0.1:80 ${heards}
${body} Create Dictionary username byhy password 88888888
${resp} Post Request login api/mgr/signin ${body}
1-查询客户
${params} Create Dictionary action list_customer pagesize 5 pagenum 1
${dd} Get Request login /api/mgr/customers params=${params}
Should Contain ${dd.text} "name": "南京中医院3"
第二种:
思路:
我们可以先把cookie提取出来。具体可以使用httplibrary.http这个库中的Get Response Header关键字。
代码:
4-查询客户
Create Http Context 127.0.0.1:80 http
Set Request Body username=byhy&password=88888888
Set Request Header Content-Type application/x-www-form-urlencoded
HttpLibrary.HTTP.POST /api/mgr/signin
${cookie} Get Response Header Set-Cookie
${a} String.Get Substring ${cookie}[0] 0 42
Log ${a}
${cookie} Create Dictionary Cookie ${a}
Create Session chaxun http://127.0.0.1:80
${params} Create Dictionary action list_customer pagesize 5 pagenum 1
${dd} Get Request chaxun /api/mgr/customers ${cookie} params=${params}
Log ${dd.text}
Should Contain ${dd.text} "name": "南京中医院3"