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
Read More:
- Python + Requests +Excel+Jenkins interface automation
- Python: How to Create an Automatic Recording Program
- Python reads excel and stores it in the list according to the header hash copy.deepcopy
- A summary of a demo development process for Python using the QT5 development interface
- Zlib.error: error – 3 while decompressing: incorrect header check in Python
- Failed to establish a new connection: [winerror 10048] in the requests thread pool, the interface is called circularly to get the datagram error
- Keras import a custom metric model error: unknown metric function: Please ensure this object is passed to`custom_object‘
- Python Error: bash syntax error near unexpected token `newline’
- How to Solve Automatic error keyerror:***‘
- [Solved] Python serializate error: NameError: name ‘JSON’ is not defined
- Opencv-python Install is Stuck Error: running setup.py bdist_wheel for opencv-python
- [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
- Python parsing JSON Error: NameError: name ‘false’ is not defined
- Python+Selenium Error: AttributeError: ‘WebDriver‘ NameError: name ‘By‘ is not defined
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]
- [How to Solve] Python TypeError: ‘int‘ object is not subscriptable
- Python openpyxl excel open zipfile error resolution: zipfile.BadZipFile: File is not a zip file
- How to Solve Python WARNING: Ignoring invalid distribution -ip (e:\python\python_dowmload\lib\site-packages)
- Python error: ‘int’ object is not callable
- Python error collection: NameError: name ‘numpy’ is not defined