ServiceNow ATF - Automated Test Framework for Testing REST based Integration
ATF helps automating the test cases at system upgrade for testing the checklist, implementation testing for an application or for testing a real-time integration using REST or server-side scripts.
REST API Explorer
ServiceNow provides an easy way to communicate via HTTP methods with built-in tables – base, core or custom tables. We provide base URL, system parameters, limiting the returned record count, deciding on the format either for JSON or XML etc. We will cover this as a first step in our ATF Test case.
You can explore on this Starting with REST APIs - ServiceNow Integration Level 1
Business Case
Integrating with 3rd party tool (say Workday): Passing values form Workday to ServiceNow to verify using GET operation in CMDB tables and return ‘200 Success - Record Found’ for a matching value or ‘404 Failed - Record Not found’ for incorrect/empty input.
Proposed Solution
Use ServiceNow ATF solution to test if data is found in ServiceNow or not using Assertion of Status Code and Response data. The following steps provide clear understanding!
Steps
1. Create a ServiceNow Record (which says record is already present in Configuration Database table) - populate with below details: We use Apple as manufacturer with sys_id = sys_idOfApple (for convenience)
2. The Workday team has provides/sends a JSON file consisting Manufacturer and Asset tag as an input to ServiceNow REST APIs
Let’s say the file consists of this data: {"manufacturer":"sys_idOfApple","asset_tag":" WXYZ9876"}
3. Success Case
Creating a Test and it's steps in Automated Test Framework Application and add assert steps to validate. Remember to activate the plugin if it's not active by default, verify in Plugins Module.
i. Open Automated Test Framework > Test > Create New > Fill in details > Submit
ii. Create Test Steps within Test > Create New Step > Choose from Categories and follow the same order as given (full list found in Madrid version):
ii.a. Select Send REST Request - Inbound - REST API Explorer > Next
ii.b. Select the table as Application (cmdb_ci_appl) and fill out the details for input (sysparm_query – manufacturer=sys_idOfAppleasset_tag=WXYZ9876) and output (sysparm_fields – choose from select box, here it is just ‘name’). The default HTTP Method will be GET, which is of our interest here and the rest all fields can be the same or modify at the best of the knowledge. Click Send and verify the results in JSON format.
ii.c. Next click on top right corner – Create Automated Test Step > Save > Go to Basic Authentication > Search Box > Create a new profile for authentication (here I have used my admin credentials and provided a name REST ATF Authentication) > Update
ii.d. Create 2nd Step – Choose Assert Status Code from REST category as we did in (1) and give the Status Code as 200. We are just verifying the output Status code here so as the logs show up expected value vs. actual value after the execution.
ii.e. Create 3rd Step - Assert Response JSON Payload Is Valid > Just Submit
ii.f. The 4th Step would be - Assert Response Payload and add the below text in the Response body – {"result":[{"manufacturer":"sysidOfApple","asset_tag":" WXYZ9876"}]}
The final Test Steps looks like this:
4. Failure Case
Creating next Test and its Steps in ATF Application: Follow the same steps as we did in section ‘c’ but with a little variation as shown below:
Choose the input to be wrong, say - sysparm_query: manufacturer=wrong-sys_idOfAppleasset_tag=WXYZ9876
Asset status code must be set to ‘404’, Next step is as same as first case, Assert Response Payload: {“result”:[]} //meaning empty results as the record not found
Results
ATF suite is used at the end if required which is given with same input to the Tests present within it (say Success Test & Failure Test). This way we can check either a success or failure case within a suite. Though the suite result may say 'failed' (as it will be success if and only if all the tests inside it are in success case) but in our above project, as configured one of the test case will be true, not both, which decides the success of failure. This is just a crude way of testing REST APIs in ATF, if someone gets an idea to standardize this please feel free to comment. Let's learn together!
Regards, Akash
https://www.servicenow.com/community/developer-articles/servicenow-atf-automated-test-framework-for-testing-rest-based/ta-p/2330136