This article explains how to perform API testing with Cypress, specifically focusing on adding or modifying entries in Pricefx PP tables using Pricefx APIs instead of the graphical user interface (GUI). The article provides step-by-step instructions, including adding a command to the Cypress commands.js file and using a JSON file to specify the necessary parameters for the API request. It also explains how to find the table ID for the PP table and the format of the JSON file containing the data to be inserted. The article concludes with an example of how to use the command in a Cypress test case.
While using Cypress, in order to add/modify entries in PP tables through Pricefx APIs( instead of the guiGUI), we just need to carry out the following steps:
Step 1: Add the following command to commands.js of Cypress:
Code Block |
---|
Cypress.Commands.add('insertDataToPPTable', (file) => { |
...
// read the file which contains details on: the username/partition, password, pp table id(into which data will be inserted) and the json file which contains data to be inserted into the PP table |
...
const readParamsJson = require('../fixtures'+file) |
...
//read the data to be inserted into the selected PP table |
...
const allData = require('../fixtures/'+readParamsJson.fileName) |
...
...
//read the configurations for username/partition and password from cypress.json |
...
...
//looping the json data file to insert the rows one by one |
...
allData.forEach((row) =>{ |
...
...
cy.request({ |
...
url:`${Cypress.env('apiBaseUrl')}`+readParamsJson.username.substr(0, readParamsJson.username.indexOf('/'))+"/lookuptablemanager.add/"+readParamsJson.tableId, |
...
method: "POST", |
...
auth: { |
...
"username": readParamsJson.username, |
...
"password": readParamsJson.password |
...
}, |
...
body:{ |
...
"data": row |
...
}
})
...
} }) .its("status") |
...
.should("eq", 200); |
...
...
});
}); |
Tip |
---|
CODE REVIEW: this is a code snippet from a JavaScript file that defines a custom command using the Cypress testing framework. The custom command is named |
Here's a step-by-step breakdown of what the code does:
The command
insertDataToPPTable
takes afile
argument, which is expected to be the path to a JSON file.It uses
require
to read the contents of the JSON file specified by thefile
argument. This JSON file contains details such as the username/partition, password, PP table ID, and another JSON file name that contains the data to be inserted into the PP table.It then reads the data from another JSON file specified by the
fileName
property in the previously read JSON object. This second JSON file contains the actual data to be inserted into the PP table.The code constructs a POST request to an API endpoint using Cypress's
cy.request
method. The URL for the request is built using theapiBaseUrl
environment variable from Cypress's configuration and includes the username and table ID read from the JSON file.The POST request includes authentication credentials (username and password) and the body of the request contains the data from one row of the JSON data file.
The command loops over each row in the data file, sending a separate POST request for each row.
After sending the POST request, it checks the response status to ensure it is
200
, indicating that the operation was successful.
Info |
---|
NOTE: This custom command seems to be designed to automate the process of populating a PP table with multiple entries |
Step 2: Note that the command takes one string parameter, which contains the relative path (from fixtures folder)to a json file. The json file is expected to be present in the fixtures folder of the cypress project. It should contain details as shown in the sample below:
Code Block |
---|
{ |
...
"username":"<partition>/<loginName>", |
...
"password":"<password>", |
...
"tableId" : 174, |
...
"fileName":"PPTableData/UserHierarchy-Customer/PPData.json" |
...
} |
Tip |
---|
CODE REVIEW: Note the following about the parameters in the file: |
...
|
...
|
...
|
...
Tip |
---|
CODE REVIEW: a JSON response in a web API testing tool, likely from a GET request to a URL that seems to be related to fetching data from a lookup table manager in a Pricefx application environment. |
The JSON response contains several key-value pairs, which include:
"typedid": "IM.LI"
, which may be an identifier for the type of data or the specific lookup table being accessed."label":
...
https://<urlOfTheCustomerApplication>/pricefx/automation/lookuptablemanager.fetch
...
"Di6 - Sales Region Mapping"
, which likely describes the content or purpose of the lookup table, in this case, mapping sales regions."validafter": "2000-08-01"
, which could indicate that the data in the lookup table is valid after the specified date."status": "ACTIVE"
, suggesting that the lookup table is currently active and presumably in use."simulationSet": null
, indicating that there is no simulation set associated with this data at the moment."valuetype": "**szsz**"
, where the value is not clearly defined and could be a placeholder or an internal code."userregedit": "o16 (dit)"
, which might refer to user registration or editing information, possibly indicating who created or last edited the lookup table.
The API response is shown in a pretty-printed format for easier readability, and there are other tabs and options available in the tool, such as Raw, Preview, Visualize, and JSON options for viewing the response. Additionally, the response metadata indicates a successful request with a 200 OK
status, taking 1056 milliseconds to complete, and the size of the response is 475 KB.
Step 3: While sending the above request, “Authorization” should be set as shown in the screenshot below (using appropriate {partition name}/{loginName} and {password})
...
The response “body” will have an array of json elements, one per PP table present in the system. Each element gives details about the PP table including the PP table id as shown in the example below:
Code Block |
---|
{ |
...
"version": 9, |
...
"typedId": "174.LT", |
...
"uniqueName": "UserHierarchy-Customer", |
...
"label": "General - User Hierarchy Customer", |
...
"validAfter": "2000-01-01", |
...
"status": "ACTIVE", |
...
"simulationSet": |
...
null, |
...
"type": "MATRIX", |
...
"valueType": "MATRIX6", |
...
"nodeId": 16, |
...
"userGroupEdit": "SDM_Edit,PBM_Edit,RBMEdit", |
...
"userGroupViewDetails": "SDM_View,PBM_View,RBMView", |
...
"hideWarnings": |
...
false, |
...
"formatType": |
...
null, |
...
"lastUpdateByName": "pfx.jenkins", |
...
"createdByName": "pfx.jenkins", |
...
"numberOfKeyFields": 6, |
...
"createDate": "2020-10-27T07:03:37", |
...
"createdBy": 318, |
...
"lastUpdateDate": "2021-08-02T14:10:48", |
...
"lastUpdateBy": 447, |
...
"id": 174, |
...
"isPlasma": |
...
false } |
Value
Info |
---|
NOTE: the value of the field “id” is |
...
the id of the table whose “uniqueName” is UserHierarchy-Customer |
d) The fileName is the relative path (from fixtures folder) to the file which contains the actual data to be inserted into the PP table identified above. Format of the file for a Matrix6 PP table is as below (multiple rows can be inserted in one go):
Code Block |
---|
[ |
...
{"key1":"SE01", |
...
"key2":"Default", |
...
"key3":"ISO", |
...
"key4":"S62", |
...
"key5":"Test", |
...
"key6":"Admin" |
...
}, |
...
{"key1":"SE02", |
...
"key2":"Default", |
...
"key3":"ISO1", |
...
"key4":"S62", |
...
"key5":"Test", |
...
"key6":"Admin" |
...
} |
...
...
] |
In the
...
Cypress testcase, the command can be called as shown below:
Code Block |
---|
describe('userHierarchyTableTest', () => { |
...
...
it('userHierarchy', () => { |
...
cy.insertDataToPPTable("/PPTableData/UserHierarchy-Product/BasicDetails.json") |
...
cy.insertDataToPPTable("/PPTableData/UserHierarchy-Customer/BasicDetails.json") |
...
}); |
...
}); |