12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- *** Settings ***
- Library HttpLibrary.HTTP
- Library RequestsLibrary
- Library Collections
- Library DateTime
- Library CustomLibrary
- *** Keywords ***
- PostRequest
- [Arguments] ${host} ${path} ${datas} ${params}=${EMPTY} ${headers}=None ${cookies}=None ${timeout}=30
- #处理请求
- ${header_dict} Create Dictionary Content-Type=application/json
- Run Keyword If ${headers}==${None} Log 没有添加自定义header
- ... ELSE Run Keyword AddHeader ${headers} ${header_dict}
- #处理cookies
- ${cookie_dict} Create Dictionary
- Run Keyword If ${cookies}==${None} Log 没有添加cookie信息
- ... ELSE Run Keyword AddCookies ${cookies} ${cookie_dict}
- #创建session
- Create Session example_robotframework ${host} timeout=${timeout} cookies=${cookie_dict}
- #发起post请求
- ${resp} POST On Session example_robotframework ${path} json=${datas} headers=${header_dict} params=${params}
- [Return] ${resp}
- AddHeader
- [Arguments] ${dict1} ${dict2}
- [Documentation] *遍历字典变量dict,并将dict1中的值添加到dict2中*
- Log 在请求体中添加自定义header
- ${items} Get Dictionary Items ${dict1}
- FOR ${index} ${key} ${value} IN ENUMERATE @{items}
- Set To Dictionary ${dict2} ${key}=${value}
- AddCookies
- [Arguments] ${cookies} ${cookiedict}
- [Documentation] *处理cookie*
- @{cookielist}= Split String ${cookies} ;
- FOR ${cookie} IN @{cookielist} #用;分割cookie
- Run Keyword If '${cookie}'=='${None}' Exit For Loop #如果Cookie为None,则跳出循环体
- ${cookie_split}= Split String ${cookie} =
- Set To Dictionary ${cookiedict} ${cookie_split[0]}=${cookie_split[1]}
- PutRequest
- [Arguments] ${host} ${path} ${datas} ${params}=${EMPTY} ${headers}=None ${cookies}=None ${timeout}=30
- #处理请求
- ${header_dict} Create Dictionary Content-Type=application/json
- Run Keyword If ${headers}==${None} Log 没有添加自定义header
- ... ELSE Run Keyword AddHeader ${headers} ${header_dict}
- #处理cookies
- ${cookie_dict} Create Dictionary
- Run Keyword If ${cookies}==${None} Log 没有添加cookie信息
- ... ELSE Run Keyword AddCookies ${cookies} ${cookie_dict}
- #创建session
- Create Session example_robotframework ${host} timeout=${timeout} cookies=${cookie_dict}
- #发起put请求
- ${resp} PUT On Session example_robotframework ${path} json=${datas} headers=${header_dict} params=${params}
- [Return] ${resp}
- DeleteRequest
- [Arguments] ${host} ${path} ${params}=${EMPTY} ${headers}=None ${cookies}=None ${timeout}=30
- #处理请求
- ${header_dict} Create Dictionary Content-Type=application/json
- Run Keyword If ${headers}==${None} Log 没有添加自定义header
- ... ELSE Run Keyword AddHeader ${headers} ${header_dict}
- #处理cookies
- ${cookie_dict} Create Dictionary
- Run Keyword If ${cookies}==${None} Log 没有添加cookie信息
- ... ELSE Run Keyword AddCookies ${cookies} ${cookie_dict}
- #创建session
- Create Session example_robotframework ${host} timeout=${timeout} cookies=${cookie_dict}
- #发起delete请求
- ${resp} DELETE On Session example_robotframework ${path} headers=${header_dict} params=${params}
- [Return] ${resp}
- GetRequest
- [Arguments] ${host} ${path} ${datas} ${params} ${headers}=None ${cookies}=None ${timeout}=30
- #处理请求
- ${header_dict} Create Dictionary Content-Type=application/json
- Run Keyword If ${headers}==${None} Log 没有添加自定义header
- ... ELSE Run Keyword AddHeader ${headers} ${header_dict}
- #处理cookie
- ${cookie_dict} Create Dictionary
- Run Keyword If ${cookies}==${None} Log 没有添加cookie信息
- ... ELSE Run Keyword AddCookies ${cookies} ${cookie_dict}
- #创建session
- Create Session example_robotframework ${host} timeout=${timeout} cookies=${cookie_dict}
- #发起GET请求
- ${resp} RequestsLibrary.Get_Request example_robotframework ${path} headers=${header_dict} params=${params}
- [Return] ${resp}
|