In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to carry out Postman automation interface testing, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Background description
There is a project to use postman for interface testing. The parameters required for the interface are:
Appid: application ID
Sign: request signature, which needs to be calculated using HMACSHA1 encryption algorithm. The signature string is: {appid} ${url} ${stamp}
Stamp: this is a timestamp
Option: business parameter
The question is how to dynamically build a signature (sign) based on parameters when an Postman initiates a request?
CryptoJS in postman's script library supports encryption of various algorithms, including HMACSHA1, and signature algorithms are available.
The difficulty is to get the path parameter in url, which can start to fix a path value when initiating a request, and how to get the path value when automated testing needs to be performed.
Create a GET request
The basic usage of postman will not be introduced. First, create a GET request. Various dynamic parameters are configured in URL.
{{variable name}}: syntax for postman referencing environment variables
{{$guid}}: the postman predefined environment variable is used to get a guid value
Build signatures in pre-request scripts
Pre-request scripts is a javascript execution environment that executes before the request is sent; it can be used as a js, but some js libraries do not support it.
The next step is to get the signature dynamically.
1. Fixed value configured in appid environment variable
2. Obtain the stamp timestamp:
/ / get unix time getUnixTime:function () {return Math.round (new Date () .getTime () / 1000);}
3. The url value can be obtained through request.url and then parsed out of the url:
/ / get the path part of url getUrlRelativePath:function (url) {var arrUrl = url.split ("/ /"); var start = arrUrl [1] .indexOf ("/"); var end=arrUrl [1] .indexOf ("?"); var relUrl = arrUrl [1] .substring (start,end); / / stop omitted to intercept all characters console.log (relUrl) from the beginning to the end of start; return relUrl;}
4. Construct a signature string and encrypt it with a secret key.
The encryption algorithm library provided by postman may not support all of them, and sometimes you need to go to the background to get a signature.
Var host=pm.environment.get ("host"); var text=encodeURIComponent (plain); pm.sendRequest (host+ "/ FaceIn/ToHmacsha1?plain=" + text+ "& secret=" + sercret, function (err, response) {var json=response.json (); / / url encoding pm.environment.set ("sign", encodeURIComponent (json.result);})
The signature string is best encoded with URL.
Legacy: when you get a signature from the background, the string responsejson () cannot be parsed at first!
5. Use eval to inject the defined variable postmanUtil into the global variable and then call
Eval (environment.postmanUtil); postmanUtil.setLsdzSign ()
The result is shown in the figure:
The code is as follows:
Var postmanUtil= {/ / get unix time getUnixTime:function () {return Math.round (new Date (). GetTime () / 1000);}, / / get the path part of url getUrlRelativePath:function (url) {var arrUrl = url.split ("/ /"); var start = arrUrl [1] .indexOf ("/"); var end=arrUrl [1] .indexOf ("?"); var relUrl = arrUrl [1] .substring (start,end) / / stop omitted to intercept all characters from the beginning of start to the end of console.log (relUrl); return relUrl;}, / / signature setLsdzSign:function () {var appid=pm.environment.get ("appid"); var sercret=pm.environment.get ("appsercret"); / / timestamp var time=postmanUtil.getUnixTime (); pm.environment.set ("stamp", time); / / the path part of the address that gets the current address var path= postmanUtil.getUrlRelativePath (request.url) Console.log (path); var url=path; var plain=appid+ "$" + url.toLowerCase () + "$" + time; var hmac = CryptoJS.HmacSHA1 (plain, sercret) .toString (CryptoJS.enc.Base64); / / get the signature. CryptoJS.HmacSHA1 cannot satisfy that the signature algorithm can only var host=pm.environment.get ("host"); var text=encodeURIComponent (plain); pm.sendRequest (host+ "/ FaceIn/ToHmacsha1?plain=" + text+ "& secret=" + sercret, function (err, response) {var json=response.json () / / signature containing + and other special characters requires url encoding pm.environment.set ("sign", encodeURIComponent (json.result);});}} eval (environment.postmanUtil); postmanUtil.setLsdzSign (); script is written in environment variables
Write the above code in Pre-request Script, even if a single interface is OK, even if many interfaces only need a copy of Copy.
In case the script needs to be changed, the trouble comes, you need to go to each requested Pre-request Script window to change, how to solve it?
It can be solved by setting the postmanUtil in ENVIRONMENT as follows:
In fact, postmanUtil is put into the environment variable, the rest has not changed, as long as the maintenance of the value of the environment variable on the OK, do not have to change one by one.
If you look at the pre-request script code, it's much easier:
The usage of postman console
I don't know whether the environment variable has been obtained successfully, or if you want to check the value of a variable, postman also provides a convenient console to view. Under the menu View, Show Postman Console can open the following console:
The figure shows the results of console.log (sercret) and sendRequest ()
Collection Runner automates API testing
Create a test case for the interface
For the result that returns html, it passes as long as the test body contains a certain value.
For the returned Json result, as long as Code is 0, it is passed.
There are commonly used script shortcut operations on the right side of the window, which can be generated when selected, which is very convenient.
Select and run the automated interface test
Click Runner in the upper left corner of the home page to enter, select the previously built interface, select a good environment, and click the Run xxx interface to run the script test.
Test result
You can see that the 2 interfaces have successfully returned the predetermined results.
After reading the above, have you mastered how to test the Postman automation interface? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.