I recently used JMeter to do interface testing, and I frequently encountered the file upload interface, which is handled differently from other general interfaces. I thought I would share it, hoping to give some inspiration to my fellow testers.
The article will be organized around three parts:
1. User scenarios
2. Interface request parameters
3. JMeter script writing steps
4. JMeter script execution
1. User scenarios
There is a new user credential page, fill in the field information, upload the image file, click Submit, the creation is successful.
This step includes two interfaces, one for uploading image files, which returns a path field value. If the path is included in the request parameters of the new certificate interface, the creation will be successful.
The basic idea of implementing the script is to first call the upload interface, use the JSON extractor to extract the returned path field value, and store it in a variable ${get_picture_url}. Then call the new interface and directly assign the value of the path variable to the request parameter attachUrl.
2. Interface request parameters
1. File upload interface
For the file upload class interface, pay attention to the Content-Type of the Request Headers, the value is: multipart/form-data
Request body, parameter is file
Response value, returns the path information of the uploaded file, that is, the value of the data field
2. Create a new user credentials interface
Content-Type of Request Headers, the value is: application/x-www-form-urlencoded
There are 6 parameters in the request body, mainly focusing on attachUrl. This parameter is the value of the data field returned by the upload interface.
Response value. If the new creation is successful, success will be returned.
3. JMeter script writing steps
1) Upload interface script writing
1. Add a thread group: Financial Management
2. Add a simple controller
It doesn't matter whether you add it or not. It's mainly to standardize the script and make it easier to read. It's usually a simple controller making an http request.
3. Add http request
The protocol, server name, request method, request path, etc. are filled in as usual, but Use multipart/form-data for POST must be checked. If there are other parameters, fill them in directly in Parameters as usual. If the interface in the example is not available, it will not be written.
Switch to File Upload and fill in the upload file information
File name: just write the local upload file path (note, when deploying the script to a Linux server, be careful to switch the path)
Parameter name: Fill in the parameter name of the file path. The parameter in the example is file.
MIME type: multipart/form-data
4. Add response assertion
Just fill it in according to the actual interface
5. Add JSON extractor
Names of Created variables: As referenced variable names, multiple variable names are separated by semicolons; customized
JSON Path expessions: The format is . (level name), this example is . (level name), this example is . (level name), this example is .data
Match No.: Which value to take, 0 means random, -1 means taking all array data, 1 means taking a variable value, which is related to the set variable name. In this example, fill in -1 (Note: If you fill in 1, the following When referencing a variable, just use getpictureurl. If you fill in - 1, an array will be returned. When you reference the variable later, use {get_picture_url}. If you fill in -1, an array will be returned. When you reference the variable later, Just use get
picture url
. If you fill in -1, an array will be returned. When referencing variables later, use the form {get_picture_url_1}, ${get_picture_url_2} )
Compute concatenation var(suffix_ALL): All matched values are saved and the default value is empty.
Default Value: The content displayed when the value fails. The default value is empty.
2) Create a new user credentials interface
1. Add a simple controller
It doesn't matter whether you add it or not. It's mainly to standardize the script and make it easier to read. It's usually a simple controller making an http request.
3. Add http request
The protocol, server name, request method, request path, etc. are filled in as usual. There is no need to check Use multipart/form-data for POST and attachUrl parameters. Directly assign the value of the variable ${get_picture_url_1} to attachUrl.
There is no need to fill in any information for File Upload.
4. Add response assertion
Just fill it in according to the actual interface
4. JMeter script execution
When executing the script, in addition to checking the number of results and the successful execution of all interfaces, you also need to go to the page to check whether the uploaded image is successful in the newly created user credentials details.
During the debugging process today, I found that even if the path to the new user credential interface is incorrect, the user credential can be successfully created. However, when viewing the details, the picture is cracked, just like this effect, so even if the script is adjusted, Also check the page to see if the image was actually uploaded successfully.
In this way, a complete interface upload script is successful.
Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:
This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!