Run Basic API Request
Run Basic API Request

How to Run Basic API Request Using Ruby and Watir

Watir know as Web Application Testing tool in Ruby used for automating tests for web applications. If you want to run basic API request using Ruby or Watir, you will need to use an additional libraries such as HTTParty or RestClient, since Watir is primarily designed for web browser automation. REST API in Ruby is not so complex we can easily learn this in few minutes, so now lets start our main course.

Here’s a general guideline on how to run basic API request using Ruby language

To Run Basic API Request Using Watir / Ruby, Start by installing the necessary gems for both Watir and required other libraries. You can also use bundler to manage your gem dependencies.

Installing Required Ruby Gems

First Install following gems using Terminal or command line

gem install watir
gem install httparty   # or gem install rest-client

Import Libraries

Before Writing any script, you need to import require libraries you’ll be using

require 'watir'
require 'httparty'    # or require 'rest-client'

Initialise Watir

Setup Watir to interact with browser.

browser = Watir::Browser.new(:chrome)  # Or :firefox, :safari, etc.

Make API Calls

You can use chosen library (RestClient or HTTParty) to make API calls. Let’s see simple example to understand it.

Call API with HTTParty

response = HTTParty.get('https://api.example.com/testdata')
puts response.code      # HTTP status code
puts response.body      # Response body

And here’s an example of RestClient

response = RestClient.get('https://api.example.com/testdata')
puts response.code      # HTTP status code
puts response.body      # Response body

Clean Up

After making API calls successfully, don’t forgot to close the browser if you no longer need it.

browser.close

Now lets see some proper example to manage all these professionally in OOP approach (POST Request using Ruby)

Create common utils for post request (You can create separate ruby file for this) : Here you can modify post body as per your requirements. This method will prevent you from writing same code multiple times.

def post_request(url, query_param = {}, headers = {}, body = {}, redirects = true)
	log("API Request URL: #{url}")
	response = HTTParty.post(url, query: query_param, headers: headers, body: body, follow_redirects: redirects)
	log("Response: #{response}")
	response
end

Create separate method in your test script file to manage your code. This will help you to write clean and manageable code.

def post_create_user(query_param = {})
  api_url = "Your API Here"
  response = post_request(api_url, query_param, nil, nil)
  response
end

Now Create test method to utilise above logics and perform some assertions.

def check_create_user
	test_data = 	{
		table_name: 'users',
		user_name: 'testautomationlabs',
		password: 'iloveautomation',
		user_role: 'admin',
		user_bio: 'Hello World, This is my awesome bio'
	}
	response = post_create_user(test_data)
	# Assert the status code
	expected_status_code = 200
	if response.code == expected_status_code
		puts "API call successful: Status code is #{expected_status_code}"
	else
		puts "API call failed: Expected status code #{expected_status_code}, but got #{response.code}"
	end
end
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments