程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python interface automation case

編輯:Python

One 、Python And requests Study

The data is stored in a database somewhere else , I want to see data on my computer . How to get data from other devices , Then do some treatment , On my interface ?

Interface is an intermediate bridge , Help you get the data out of the database , By the way, I will return to you .

If you ask me for this data .

1. Who is it ? Who to ask for data ?---- Address interface address .

You have to know who they are . On the phone , Know the phone number of the other party , To dial out the number .

2. The type of thing : Requested method (get,post)

3. What is it about ? Request data .

4. Get an answer . Return the data .

Focus on what ? data . The correctness of the data . A match between the requested data and the returned data .

1) send data .

2) Validation data .

#1. Test data prepared
import requests #http -- get post
# Address ---- You need to apply before using this data .
url="https://api.apishop.net/common/dogFamily/queryDogListByKeyword"
# Dictionaries
req_datas={"apiKey":"w6XRBKH974efb492355fef8073aa1cd3eb819932694d6a5","keyword":" Golden hair "}
#2. Called post request , Send a http request . And get the response result .
# function - Realized the function
res=requests.post(url,req_datas)
print(res)
#3. Get the status code and return data of the response .
# Status code 、 The message header 、 Return the data
# Status code
print(res.status_code)
# Return the data
# print(res.text)
print(res.json())
# Assertion : Assertion status code , Assert a field in the returned data .
#jsonpath ==
# Regular expressions .
############################## Use case two #######################
print("======== Test case 2 =============")
#1. Test data prepared
import requests #http -- get post
# Address
url="https://api.apishop.net/common/dogFamily/queryDogListByKeyword"
# Dictionaries
req_datas={"apiKey":"w6XRBKH974efb492355fef8073aa1cd3eb819932694d6a5","keyword":"###"}
#2. Called post request , Send a http request . And get the response result .
# function - Realized the function
res=requests.post(url,req_datas)
print(res)
#3. Get the status code and return data of the response .
# Status code 、 The message header 、 Return the data
# Status code
print(res.status_code)
# Return the data
# print(res.text)
print(res.json())
#### The process is the same , It's just data (url+ Request data ) Dissimilarity .----- Process and data are separated .
#### Prepare a process , Multiple sets of data .

Running results

summary :

utilize requests Library send requests are divided into :

1. Prepare test data : Interface url Address 、 Request type 、 Request data .

2. call get/post: Which method to call depends on the request type .

3. adopt res.text Get response data .

The process is the same , It's just data (url+ Request data ) Dissimilarity .----- Process and data are separated .

Two 、excel Basic operation and reading test data

Must be 1

Must be 2

The form name in the code should be the same as Excel The names of the forms inside are consistent .

sample.xlsx

File directory structure

python Code in excel The data read out in the default is a string . use eval() This function can convert a string into a dictionary .

But the requested data is json Format ,json There is a... In the format Null, This thing ,python I don't know .python What I know is None. If json There is a... In the string Null,eval() There is no way to Null Turn into None.

There is one json Library to solve this conversion problem ,json Libraries do not require additional installation , Just introduce it directly . If json There is a... In the string Null, Will automatically convert it to None. Can be json The string is converted to python Dictionary .

req_datas=json.loads(datas)


# function : function . Get the test data , send out http request , Get response results .
# Defined function 、 Class cannot be associated with python The self-contained function name of . Because the name is the same , After the code is run, you will find the content under the function you defined , If you can't find it, you're wrong .
import json
import requests
def myRequests(url ,method ,datas):
# Determine the type of request
if method =="get":
res =requests.get(url,datas)
else:
res =requests.post(url,datas)
print(res.text)
'''
The format of the returned data need not be set , By default, a string is returned .
The server returns all strings , Whatever its data format is json、xml、html,
For the client , The first thing you get is the format of the string . Second, I want to turn it into json、xml、html,
Just call the corresponding library conversion .
'''
# ## Use case one
# url1 ="https://api.apishop.net/common/dogFamily/queryDogListByKeyword"
# datas ={"apiKey" :"w6XRBKH974efb492355fef8073aa1cd3eb819932694d6a5","keyword":" Golden hair "}
# method ="post"
# myRequests(url1 ,method ,datas)
#
#
# ## Use case two
# print("======= The test case 2=====")
#
# datas_2 ={"apiKey" :"w6XRBKH974efb492355fef8073aa1cd3eb819932694d6a5","keyword":88}
# method ="post"
# myRequests(url1 ,method ,datas_2)
#
# ## Use case three
# print("========== The test case 3============")
#
# url2="https://api.apishop.net/common/dogFamily/queryDogInfo"
# datas_3 ={"apiKey" :"w6XRBKH974efb492355fef8073aa1cd3eb819932694d6a5","petID":8}
# method_3 ="post"
# myRequests(url2 ,method_3 ,datas_3)
from openpyxl import load_workbook
# Load test file
wb=load_workbook("sample.xlsx")
# Find the form where the test data is located
sh=wb["Sheet1"]
# Get the value of a cell , The second line, the second column
# The second row of data
# url=sh.cell(row=2,column=2).value
# method=sh.cell(row=2,column=3).value
# datas=sh.cell(row=2,column=4).value
# print("url:",url,"method:",method,"datas:",datas)
# myRequests(url, method, datas)
# The third row of data
# url=sh.cell(row=3,column=2).value
# method=sh.cell(row=3,column=3).value
# datas=sh.cell(row=3,column=4).value
# print("url:",url,"method:",method,"datas:",datas)
# myRequests(url, method, datas)
# How many lines of test data
rows=sh.max_row #[1,2,3,4]
#range(4) #[1,2,3]
#range( Starting value , Terminal value ) range(2,5) [2,3,4]
#for Loop implementation traversal --- Traverse line number .
for item in range(2,rows+1):
# Get the line number on the first line
print(item)
# Get the request data for each row 、 Address 、 Request method .
url=sh.cell(row=item,column=2).value
method=sh.cell(row=item,column=3).value
datas=sh.cell(row=item,column=4).value
print("url:",url,"method:",method,"datas:",datas)
# take datas Turn it into a dictionary . The default from the excel What is read out in is a string .
# take json The string is converted to python Dictionaries .
req_datas=json.loads(datas)
# Send an interface request .
myRequests(url, method, req_datas)

Running results


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved