Can I re-use the same test functions as a variable to be used by all test tabs in all collections within a workspace.?
Here is an example below of the functions in question. Also see a few values that are being validated by these functions. These work if I add the set of functions to each test tab but I want to use these functions globally on all test tabs in a workspace. I’ve tried using this set of functions at the “Collection Level” test tab but it does not work. It only works if I add them directly to the specific call test tab.
EXAMPLE FUNCTIONS
// Test scripts section to be added to postman collection
// get the json object from the response.
jsonData = pm.response.json();
// method to validate the response code
function validateResponseCode(expectedResponseCode)
{
pm.expect(pm.response.code, 'Response Code').to.equal(expectedResponseCode);
}
function validateQuestion(questionName, expectedValue)
{
var question = jsonData.questions.find(question => question.name == questionName);
validateQuestionWorker(question, questionName, expectedValue, 'Question: [' + questionName + '] Module: [Main] Value Equals:[' + expectedValue + ']');
}
function validateRMQuestion(questionName, moduleName, moduleIndex, expectedValue)
{
var question = jsonData.questions.find(question => question.name == questionName && question.moduleName == moduleName && question.moduleIndex == moduleIndex);
validateQuestionWorker(question, questionName, expectedValue, 'Question: [' + questionName + '] Module: [' + moduleName + '] Index:'+ moduleIndex + ' Value Equals:[' + expectedValue + ']');
}
function validateQuestionWorker(question, questionName, expectedValue, testTitle){
pm.test(testTitle, function(){
pm.expect(question, questionName + ' was not found').not.equal(undefined);
pm.expect(question.value, questionName + ' was found but value is null').not.equal(undefined);
pm.expect(question.value, questionName).to.equal(expectedValue);
});
}
// method to get document status
function validateDocument(docName, expectedStatus)
{
var document = jsonData.documents.find(documents => doc.name == docName);
pm.test('Document:[' + docName + '] Expected Status:[' + expectedStatus + ']', function(){
pm.expect(document, expectedStatus).not.equal(undefined);
pm.expect(document.status, expectedStatus).to.equal(expectedStatus);
});
}
// method to get workflow status
function validateWorkflow(workflowName, expectedStatus)
{
var task = jsonData.workflowTasks.find(task => task.name == workflowName);
pm.test('Workflow:[' + workflowName + '] Expected Status:[' + expectedStatus + ']', function(){
pm.expect(task, workflowName).not.equal(undefined);
pm.expect(task.status, expectedStatus).to.equal(expectedStatus);
});
}
// check context to see if questions are valid
function validateResponseContext(questionsAreValid)
{
pm.test('Validate Response Context', function(){
pm.expect(jsonData.responseContext, 'responseContext.questionsAreValid').not.equal(undefined);
pm.expect(jsonData.responseContext.questionValuesAreValid, 'responseContext.questionsAreValid').not.equal(undefined);
pm.expect(jsonData.responseContext.questionValuesAreValid, 'responseContext.questionsAreValid').equal(questionsAreValid);
});
}
// Test scripts section End #####
EXAMPLE TEST VALUES
/* Test Script For Page Name:‘Event Setup’ Id:‘52000240’ */
validateQuestion(‘IFS.WorkflowXPDLName’, ‘XPDL:Firm - IFS Onboarding:Transaction - Client Onboarding’);
validateQuestion(‘IFS.WorkflowSourceSR’, ‘Firm - IFS Onboarding’);
validateQuestion(‘DomainAccounts_AccountCustodian’, ‘NFS’);
validateQuestion(‘DomainAccounts_AccountRegistration1’, ‘IFS - IRA - Account’);
validateQuestion(‘ProductCommon_TransactionType’, ‘Client Onboarding’);
validateQuestion(‘ProductCommon_CurrentWFStatus’, ‘In Progress’);
validateQuestion(‘DomainCOB_EventName’, ‘IFS - IRA - Account’);