The automatic token of Python interface is passed into the header
(1) Create login request to get token
#Test case class for login screen
# Import logging class for setting logging information
from Logs.Log import Logger
#Import configuration file class to read public data
from Readini.Read import Readini
# Import excel class for reading data in excel
from DataExcel.readExcel import ExcelUtil
#import request package
import requests
import json
from Public.PageObject import SendRequest
#import json package
import unittest
import json
from builtins import str
#Set the parameters related to reading the login
excel = ExcelUtil(Readini().get_ini_value('Excel','exccelini')).dict_data()
def token():
# Set message header information
header=eval(excel[0]['header'])
# set url data
url = excel[0]['Url']
# Set the parameter information
param=excel[0]['payload']
# Convert the set parameter information to json format data
# Set the request type
type=excel[0]['Method']
#Send a post login request
response=SendRequest().send_request(type,url,data=param,header=header)
#Get token data
token=response.json()['data']['access_token']
#Convert the token data to a string format
return str(token)
(2) Create a unittest public initialization class and pass in the token data
from selenium import webdriver
import unittest
#Create unitest initialization public class
from Logs.Log import Logger
log=Logger('Interface automation result').getlog()
from TOKEN.PublicToken import token
import json
class TestBase(unittest.TestCase):
#Interface initialization begins
@classmethod
def setUpClass(cls):
log.info('Interface automation test started')
# Pass in the obtained token as the initialized token data
cls.token=token()
#End of interface initialization
@classmethod
def tearDownClass(cls):
log.info('End of interface automation test')
if __name__ == '__main__':
unittest.main() #Main function for executing a program that has been written
(3) Pass in the token obtained during initialization to the header
Import logging class for setting up logging information
from Logs.Log import Logger
#Import profile class to read public data
from Readini.Read import Readini
#import excel class for reading data in excel
from DataExcel.readExcel import ExcelUtil
# Set the parameters related to reading logins
excel = ExcelUtil(Readini().get_ini_value('Excel','exccelini')).dict_data()
#Set the log type parameter
log = Logger('login interface log execution result').getlog()
# Inherit unittest initialization class
from ChanDao.TestBase import TestBase
#import request package
import requests
from Public.PageObject import SendRequest
import unittest
import json
from Readini.Read import Readini
class Pinlun(TestBase):
def test_1_pinglun_success(self):
'''Login successful'''
# Set message header information
header=eval(Readini().get_ini_value('header','headers'))
#eval(excel[0]['header'])
#add token information like in the headers header
header['Admin-Authorization']=self.token
log.info(header)
# Set the url data
url = 'http://localhost:8090/api/admin/posts/comments'
# log.info('The exit url address is:' + url)
# Set the parameter information
param={'page':'0','size':'10','keyword':'68'}
# Convert the set parameter information to json format data
# log.info(param)
# Set the request type
type='get'
log.info(type)
#Send get login request
response=SendRequest().send_request(type,url,param,header=header)
print(response.json())
#Get the status code of the login response, do the assertion
# self.assertEqual(response.status_code,excel[0]['StatusCode'])
# log.info('Response status code is 200, login successful')
# Set the main function to execute the written login script
if __name__ == '__main__':
unittest.main()
Note:
1. To get the token, you need to call the login interface
2. Pass the token into the setup function of unittest
3. Finally, pass the token into the header