zoukankan      html  css  js  c++  java
  • 【robotframework】使用RequestsLibrary的Get File For Streaming Upload关键字实现文件上传

    昨天组内成员遇到一问题找到我,说使用RequestsLibrary调测上传接口的时候,接口一直提示文件格式错误,她和另外一名同事琢磨了很久,没搞定,于是找到我这边。该同事的描述就是“我使用postman是好的呀,通过关键字上传就不行了”。昨天由于有更高优先级的任务,于是今天上午帮该同事看了一下,经过几次尝试,问题解决,本文主要记录下问题现象以及解决过程。

    最开始编写的用例:

    *** Settings ***
    Library           Collections
    Library           RequestsLibrary
    Library           OperatingSystem
    
    *** Variables ***
    ${BASE_URL}       http://xx.xx.xx.xx
    
    *** Test Cases ***
    文件上传测试-POST
        [Documentation]    文件上传测试-POST
        Create Session    file_upload    ${BASE_URL}    verify=True
        # 打开文件
        ${file_data}=    Get Binary File    D:/test01.xlsx
        # 文件参数
        &{file_parts}=    Create Dictionary
        Set To Dictionary    ${file_parts}    test01.xlsx=${file_data}
        # 文件上传参数
        ${resp}=    Post On Session    file_upload    /basic/file/upload    files=${file_parts}

    执行该用例后,接口报如下错误:

    查看发送的报文,发现如下信息:

    于是将用例中的test01.xlsx=${file_data} 修改为file=${file_data}后,用例如下:

    *** Settings ***
    Library           Collections
    Library           RequestsLibrary
    Library           OperatingSystem
    
    *** Variables ***
    ${BASE_URL}       http://xx.xx.xx.xx
    
    *** Test Cases ***
    文件上传测试-POST
        [Documentation]    文件上传测试-POST
        Create Session    file_upload    ${BASE_URL}    verify=True
        # 打开文件
        ${file_data}=    Get Binary File    D:/test01.xlsx
        # 文件参数
        &{file_parts}=    Create Dictionary
        Set To Dictionary    ${file_parts}    file=${file_data}
        # 文件上传参数
        ${resp}=    Post On Session    file_upload    /basic/file/upload    files=${file_parts}

    执行该用例后,报新的错误:

    查看日志中发送的报文,发现如下信息:

     POST报文中Cotent-Diposition头域中的filename也变成file了,应该是具体的文件名才对

    于是谷歌了一把,在https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254中找到了如下信息:

    于是将原先测试用例中的Get Binary File关键字替换成Get File For Streaming Upload关键字:

    *** Settings ***
    Library           Collections
    Library           RequestsLibrary
    Library           OperatingSystem
    
    *** Variables ***
    ${BASE_URL}       http://106.12.185.21
    
    *** Test Cases ***
    文件上传测试-POST
        [Documentation]    文件上传测试-POST
        Create Session    file_upload    ${BASE_URL}    verify=True
        # 打开文件
        ${file_data}=    Get File For Streaming Upload    D:/test01.xlsx
        # 文件参数
        &{file_parts}=    Create Dictionary
        Set To Dictionary    ${file_parts}    file=${file_data}
        # 文件上传参数
        ${resp}=    Post On Session    file_upload    /basic/file/upload    files=${file_parts}

     再次尝试,问题解决:

     请求报文如下:

    RequestsLibrary库中的Get File For Streaming Upload关键字帮助信息如下:

  • 相关阅读:
    nginx的简介和配置文件实例(一)
    Tomcat基础配置(一)
    redis主从复制以及SSDB主主复制环境部署记录(四)
    redis主从原理介绍(三)
    redis介绍和安装和主从介绍(二)
    celery
    网络-sdn
    djang问题汇总
    django路由url
    django开发环境配置
  • 原文地址:https://www.cnblogs.com/frankzs/p/14817130.html
Copyright © 2011-2022 走看看