接口自动化实现图片上传(selenium/RF)

最近做自动化碰到一个问题:

就是带图片上传的不知道怎么实现自动化:整理了下实现如下:

 

上传图片postman 结果请求如下,上传图片后返回一个图片地址:

post请求 body 是form-data 而不是json

 

fiddler抓取如下,

 


selenium实现如下:【也可以参考悠悠的带token验证登录的:https://www.cnblogs.com/yoyoketang/p/7990127.html】

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import os
#需要将需要上传的图片文件放到当前文件路径才能读取
def fileUpload(apiurl,fileName,targetid):
# url ="http://gateway-test.myscrm.cn/panther-third/v1/file/img/upload"
url =apiurl
pwd= os.getcwd() #取当前文件夹路径
filepath = os.path.join(pwd,fileName) #将当前文件夹下面的文件拼接成可以读取的文件路径
f1 ={
"target": (None,targetid),
# "file": ("logo3x.png", open("C:/Users/zhengw/Desktop/otherFile/logo3x.png", "rb"), "image/png")
"file": ("logo3x.png", open(filepath, "rb"), "image/png")
}
r1 = requests.post(url, files=f1)
print filepath
print r1.content

# if __name__ == "__main__":
# apiurl = "http://gateway-test.myscrm.cn/panther-third/v1/file/img/upload"
# fileName ="logo3x.png"
# targetid = "8"
# fileUpload(apiurl,fileName,targetid)


RF实现上传

 

 

*** Settings ***
Library RequestsLibrary
Library requests
Resource ../Basics/基础关键字.txt
Library ../config/sc.py
Library C:/Python27/Lib/site-packages/ykpy/sc.py

*** Test Cases ***
1
[Documentation] \#${targetid}
[Setup] # 上传类型id
${file} Evaluate ("logo3x.png", open("C:/Users/zhengw/Desktop/otherFile/logo3x.png","rb"), "image/png")
${target} Evaluate (None,u"8")
Comment ${rrrr} Evaluate os.getcwd()
Comment Log Many asdsadasdsa ${rrrr}
&{data} Create Dictionary file=${file} target=${target}
Create Session api http://gateway-test.myscrm.cn/panther-third
${request} Post Request api /v1/file/img/upload files=&{data}
log ${request.status_code}
log ${request.content}

 

 

转载于:https://www.cnblogs.com/tDayUp/p/9708830.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值