YApi project management
New Project
-
Click
+
New Project in the upper right corner to enter the new project page: -
Complete the project information, specify the group to which the project belongs, and click [+Create project]:
Note: Public and private permissions can be set after the project is created.
-
View the created project:
-
Click Tab at the top of the project page
设置
to enter the project settings panel, which covers all configurations of the project:
Modify item icon
Click the project icon on the [Project Configuration] page to modify the icon and background color:
project migration
Project migration to different groups is supported in YApi:
Migration permission: Only the administrator and the owner of the project have the permission to modify the location. The project owner mainly includes the person who created the project, the team leader in the project, the person who created the group, and the team leader in the group.
Note: The priority of owner permission judgment is project permission > group permission
project copy
-
This function is launched in version v1.3.12. The project cloning function can copy all the interfaces of the project to a new project, as shown in the figure below, click the icon in the red box to use.
Note: If you have permission to create new projects under this group, you also have permission to copy projects
-
Click the copy button in the upper left corner of the figure below, write the name of the copied project in the pop-up window and click OK to complete the project copy:
Note: YApi supports the project copy function, but it cannot copy the test collection list in the project.
delete item
Click the delete button below:
Enter a project name to delete:
Configuration Environment
环境配置
One can add the actual environment of the interface under the project for interface testing . Here, a global header is added, and the global header value can be set in the project. The bullet layer is also added in the selection environment select of the interface running page 环境配置
.
v1.3.21 adds a new global variable, the user can define the name and value of the global variable in the environment list, and the global variable defined under the current environment variable can be accessed through { { global.token }} in the interface operation or test collection:
request configuration
pre-script, modify the parameters of the request and the returned response data by customizing the js script.
Example request parameters
Taking jquery ajax as an example, suppose the current request parameters are:
{
url: '/api/user?id=1',
method: 'POST',
headers: {
xxx: 'xxx'
},
data: {
type: 1
}
}
Then the public variable context contains the following properties:
context = {
pathname: '/api/user',
query: {
id: 1
},
requestHeader: {
xxx: 'xxx'
},
method: 'POST',
requestBody: {
type:1
}
}
Suppose we need to add a public token parameter to the url of a group of interfaces, we can write the following custom script:
context.query.token = context.utils.md5(context.pathname + 'salt');
return data example
After the above sample request is completed, assuming responseData={a:1} is returned, the public variable context contains the following properties:
context = {
pathname: '/api/user',
query: {
id: 1
},
requestHeader: {
xxx: 'xxx'
},
method: 'POST',
requestBody: {
type:1
},
responseData: {
a:1
},
responseHeader: {
content-type: 'application/json'
...
}
}
Suppose we need to modify the value of the response data responseData a to 2, we can fill in the following custom script:
context.responseData.a = 2;
(v1.3.16+ new) context.href and context.hostname
(v1.3.17+ new) context.caseId unique key value of the test case
storage
storage.setItem is compatible with browsers and servers, and is a persistent data storage that will not be lost. The usage is similar to localStorage. There are two APIs in storage, namely setItem and getItem
storage.setItem('xxx', 'token-----xxxxx')
context.query.token = storage.getItem('xxx')
Tool function
context.utils = {
_ //underscore 函数,详细 API 查看官网 http://underscorejs.org/
CryptoJS // crypto-js(v1.3.21+新增), 详细用法看 https://github.com/brix/crypto-js
base64 //转换字符串为 base64 编码
md5 //转换字符串为 md5 编码
sha1 //转换字符串为 sha1 编码
sha224 //转换字符串为 sha224 编码
sha256 //转换字符串为 sha256 编码
sha384 //转换字符串为 sha384 编码
sha512 //转换字符串为 sha512 编码
unbase64 //转换 base64 编码为字符串
axios // axios 库,可用于 api 请求,官网 https://github.com/axios/axios
}
CryptoJS specific usage
var data = [{
id: 1 }, {
id: 2 }];
// Encrypt
var ciphertext = context.utils.CryptoJS.AES.encrypt(JSON.stringify(data), 'secret key 123');
// Decrypt
var bytes = context.utils.CryptoJS.AES.decrypt(ciphertext.toString(), 'secret key 123');
var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
console.log('decryptedData', decryptedData);
Asynchronous processing (v1.3.13+ support)
Processing request parameters, or returning data, may also involve asynchronous processing, such as ajax requests. YApi supports asynchronous processing in v1.3.13.
context.promise = new Promise(function(resolve) {
var api = context.utils.axios.get('http://yapi.local.qunar.com:3000/api/user/status');
api.then(function(result) {
//...
console.log(result.data);
resolve();
});
});
Promise can also be used to set the interface delay
context.promise = new Promise(function(resolve) {
setTimeout(function() {
console.log('delay 1000ms');
resolve('ok');
}, 1000);
});
The method of use is to context
add promise
parameters in and return a Promise. Children's shoes who are not familiar with Promise can check the relevant usage, and ajax requests can use context.utils.axios
the library.
Don't forget after the processing is complete
resolve()
, otherwise it will always be in the pending state
token
Each project has a unique identification token, and users can use this token value to request openapi.
global mock
v1.3.21 adds a new global mock setting, which is convenient for users to globally set public mock data at the project level. For details on how to use mock scripts, see Custom Mock Scripts
Mock priority description
When requesting Mock data, rule matching priority: Mock expectation > custom mock script > project global mock script > normal mock.
If the Mock data is matched before, the Mock will not be returned later.