Recording APIs from ANY Automation Framework with SOAtest Smart API Test Generator
Today marks the 3 year anniversary where Parasoft released the Smart API Test Generator add-on for SOAtest, which was SOAtest 9.10.5. So in honor of this anniversary, I decided to publish a write-up on how users can record JSON/HTTP-based API traffic from any automation framework.
As you may know, SOAtest Smart API Test Generator facilitates manually recording API traffic with our Chrome Extension as you go through a UI test in Chrome. One of Parasoft Selenic's features allows users to playback their library of Selenium tests while recording API traffic and sending it to SOAtest for test creation as well.
But what if your pre-existing library of UI tests are not using Selenium, or what if they're written in a language or framework not supported by Parasoft Selenic? What if you're interested in recording RESTful API traffic between your mobile device tests from Appium or some other mobile testing framework? If your automation framework allows funneling the browser's, or mobile app's, http traffic through a proxy endpoint, then you're in luck!
You will want the ability to play back your library of UI or mobile tests to automate the recording and API test creation workflow to assist with a Lean UI Testing mindset. With modern software projects whose UIs are built on top of APIs, it is not desirable to perform the majority of your functional testing at the UI layer since UI tests take so long to execute and come with a high maintenance cost. There are productivity gains to be had, but who has the time to spend in the browser's developer tools, or worse log files, trying to reverse engineer the complex sequences of API calls the UI is making and keeping track of the data relationships between those calls? It can be done, but it takes a long time! That is, until SOAtest's Smart API Test Generator came along.
For this write-up, I will be using a JUnit-based Selenium test as my "UI test" that I want to record from without using Parasoft Selenic. The Selenium API to set the browser's proxy settings is available in all the languages supported by Selenium, so these steps will easily translate to Selenium in C# or another language. To familiarize yourself with the process, you may also want to review the SOAtest documentation that covers the Parasoft Recorder. To do what I'm about to cover in this post, you must have:
- The Parasoft Recorder installed
- SOAtest Desktop installed with a SOAtest Desktop license that includes the Advanced Test Generation Desktop license feature.
Let's get started!
To inject the Parasoft Recorder workflow in our Selenium test, we need to modify the setup and teardown methods of our JUnit test class and then execute the JUnit test as we normally would.
Instead of showing you code that interacts with the Parasoft Recorder REST API from the JUnit test class, I am instead going to show you SOAtest REST Clients and a JSON Data Bank to better illustrate what you want to do from your setup and teardown methods using a Java HTTP client and JSON parser.
JUnit Setup Method
Below is a screenshot of how your setup method could look, just replace the commented lines with what I describe below using the SOAtest UI as a visual aid.
Make an HTTP POST request to the Parasoft Recorder REST API's /v1/sessions resource that starts a new recording session.
Note: that you will want to configure the payload of this POST request with JSON that tells the Parasoft Recorder how to connect to your SOAtest Desktop. This is so the Parasoft Recorder can transmit the API traffic file to your SOAtest Desktop and facilitate the TST file creation.
Note: Extract the "id" and "port" elements from the JSON response payload as we'll be using these values later.
Note: Your SOAtest Desktop's Server must be on!Initialize ChromeDriver with a ChromeOptions object that has been set with a Proxy that is configured with the proxy port returned by the Parasoft Recorder API. Here's that setup method again:
JUnit Teardown Method
Below is a screenshot of how your teardown method could look, just replace the commented lines with what I describe below using the SOAtest UI as a visual aid.
Make an HTTP PUT request to the Parasoft Recorder REST API's /v1/sessions/{id} resource and change the state to "stopped" in the request JSON payload. The {id} is the unique sessionId you extracted from the API call you made in the setup method.
Make an HTTP POST request to the Parasoft Recorder REST API's /v1/sessions/{id}/tsts resource and specify the TST file name in the request JSON payload. The {id} is the unique sessionId you extracted from the API call you made in the setup method.
Make an HTTP DELETE request to the Parasoft Recorder REST API's /v1/sessions/{id} resource. The {id} is the unique sessionId you extracted from the API call you made in the setup method.
Execute the Selenium test!
(don't judge my Thread.sleep() calls, I do a lot of demonstrations and I can't talk as fast as Selenium plays back )
See the resulting TST file in SOAtest!
And there you have it. With some refactoring of your UI automation script, you can inject the SOAtest Web Proxy to record that valuable API traffic and run it through SOAtest's AI engine that will produce an API test that is properly parameterized and ready to run as a functional API test, or even an API Load & Performance test using SOAtest's Load Test component.
Comments
-
For any Virtualize users reading this, keep in mind that traffic files recorded with Virtualize Message Proxies can also be fed into SOAtest's Smart API Test Generator as long as it's JSON/HTTP-based.
0 -
For a working example of integrating the Parasoft Recorder REST API with a Selenium test project, see the following GitHub repo:
https://github.com/whaaker/PDA-Selenium-RecorderAPI-Example0 -
For selenium-java, consider using the Parasoft Selenic Agent. No changes to your existing Selenium test are required. You just configure a few extra properties for connecting to the Parasoft SOAtest Web Proxy: https://docs.parasoft.com/display/SEL20222/Command+Line#CommandLine-proxyPort
For anything else, PDA-Selenium-RecorderAPI-Example is a proof-of-concept for how to do this in general, including for languages and test frameworks that are not based on Java.
0