Selenium WebDriver – how to Test REST API

Tram Ho

Overview :

This question is asked many times, how to test REST API using Selenium Webdriver, you can see this question on stackOverflow, new test automation people sometimes understand that selenium only automate for application UI. web, but if you want to perform setup data or clean data for UI tests using selenium, there are many ways to do that including adding a few libraries, which is what we’ll see in this article. this.

If you only need to test APIs, I recommend that you use: JMETER

So in this article, see how to test rest api in your selenium framework.

REST API Testing

Rest API testing is not very difficult to compare with UI testing, most APIs do one of the requests: GET / POST / PUT / PATCH / DELETE

  • GET is used to retrieve information from the back end and display it on the UI
  • POST is used to add information to the back end
  • PUT is used to update / replace any existing information
  • PATCH is an update
  • DELETE is used to delete information from the back end

Assuming that you are using the testNG / Junit framework and testing the UI of the application using selenium – now you want to include the test API in your framework too, follow the section below.

Dependencies

Add dependencies to maven:

Unirest is a simple HTTP request library and so is Jtwig

Sample application

Please see this application https://github.com/dsternlicht/RESTool you can clone about, install and run on your computer as well as other applications, it implements get requets API to retrieve the contacts list. displayed in the application

Get the contacts list

When you access the home page, the list of contacts will display:

You can view a few API GET requests that have been sent to get the list of contacts by pressing F12 on chrome browser, Chrome DevTools will display.

You can see the return value in JSON format as below:

You can retrieve, add, edit, delete contact information using GET / POST / PUT / DELETE requests.

GET Request

You can manually execute the GET request above using Unirest as shown here.

This can be used to perform simple comparisons in the frameWork test. For example, below the sample code confirms that all data in the API response is displayed in the UI.

POST Request:

When we need to add a new contact, we use the POST request in the following format:

If you want to send the request yourself and you don’t want hard code in the JSON file, we use the following JTwig pattern:

Now we can read the above template and change the value as shown below:

Now we can use Unirest and send JSON above to create contacts

EDIT Request:

To edit we need to use the PUT request

This is the edit contact id

DELETE Request:

Use this API to delete the data we submit

Conclude

By using Unirest in your current framwork / page object, you can interact with the application’s REST APIs and you can also use those APIs to quickly set up data in your application to verify. Real quick function.

Reference links:

http://www.vinsguru.com/selenium-webdriver-how-to-test-rest-api/

Share the news now

Source : Viblo