-->

Documentation Automation Api Testing Using Cucumber Ruby Httparty

autodika.com


Description:

  • Using BDD Framework  Cucumber Ruby
  • Http Client using HTTParty

Framework & Tools Explanation

Cucumber

What is Cucumber?

Cucumber is a tool that supports Behaviour Driven Development(BDD).
BDD is about discovery, collaboration and examples (and not testing).
Cucumber reads executable specifications written in plain text and validates that the software does what those specifications say. The specifications consists of multiple examples, or scenarios. Example:

Scenario: Get Token Client Credentials
        Given client with basic auth username:"BASIC_AUTH_CLIENT_ID" and password:"BASIC_AUTH_CLIENT_PASSWORD"
        When client sends a POST request to "/oauth/token" with body:
            """
            {
                "grant_type": "client_credentials"
            }
            """
        Then the response status should be "200"
        Then the response body should be match with json schema "auth/schemas/token-client"

 Scenario: Client GET content master data by id
        When client sends a GET request to "<endpoint>"
        Then the response status should be "200"
        When client collects data from response with json path "data.items[0].id" as "id"
        Then client sends a GET request to "<endpoint>/{id}"
        Then the response status should be "200"
        Then the response body should be match with json schema "<schema>"
        Examples:
            | endpoint                | schema                                        |
            | /banks                  | master-data/schemas/banks-id                  |
            | /provinces              | master-data/schemas/provinces-id              |
            | /countries              | master-data/schemas/countries-id              |
            | /religions              | master-data/schemas/religions-id              |
            | /marital-statuses       | master-data/schemas/marital-statuses-id       |
            | /educations             | master-data/schemas/educations-id             |
            | /organization-types     | master-data/schemas/organization-types-id     |

Each scenario is a list of steps for Cucumber to work through. Cucumber verifies that the software conforms with the specification and generates a report indicating ✅ success or ❌ failure for each scenario.

Each scenario has these three ingredients:
  • Given (Context)
  • When (Action)
  • Then (Outcomes)
  • Examples (Formulas Data Test)
In order for Cucumber to understand the scenarios, they must follow some basic syntax rules, called Gherkin.

Gherkin


What is Gherkin?

Gherkin is a set of grammar rules that makes plain text structured enough for Cucumber to understand. The scenario above is written in Gherkin.

Gherkin serves multiple purposes:
  • Unambiguous executable specification
  • Automated testing using Cucumber
  • Document how the system actually behaves
autodika.com



illustration of gherkin
The Cucumber grammar exists in different flavours for many spoken languages so that your team can use the keywords in your own language.
Gherkin documents are stored in .feature text files and are typically versioned in source control alongside the software.

See the Gherkin reference for more details.

Scenario

What is Scenario?
The scenario just tells Cucumber that we will describe an example it can run. Then you see the lines starting with Given, When, and Then.

Example : 

@master-data @squad-shared @retrieve-master-data @positive @TFCD-XXXX
Feature: GET Master Data

    Background: Authorization
        Given client logged in using client_credentials with client_id:"BASIC_AUTH_CLIENT_ID"
 
 Scenario: Client GET content master data by id
        When client sends a GET request to "<endpoint>"
        Then the response status should be "200"
        When client collects data from response with json path "data.items[0].id" as "id"
        Then client sends a GET request to "<endpoint>/{id}"
        Then the response status should be "200"
        Then the response body should be match with json schema "<schema>"
        Examples:
            | endpoint                | schema                                        |
            | /banks                  | master-data/schemas/banks-id                  |
            | /provinces              | master-data/schemas/provinces-id              |
            | /countries              | master-data/schemas/countries-id              |
            | /religions              | master-data/schemas/religions-id              |
            | /marital-statuses       | master-data/schemas/marital-statuses-id       |
            | /educations             | master-data/schemas/educations-id             |     

@master-data → @tagname is used to generate tags in each feature and scenario.

Tagging Standardization @service-name @squad-{name} @feature-name @positive/negative @{jira-tag if any} @other-tag

Background → Background allows you to add some context to the scenarios that follow it. It can contain one or more Given steps, which are run before each scenario, but after before hooks. A Backgorund is placed before first Scenario/Example, at the same level of indentations.

Feature → Feature name

Scenario → Scenario Name

Given → Given is the context of the scenario. We put the system in a certain state, ready for scenario to unfold.

When → When is an action. Something that happens to the system that will cause something else to happen: result.

Then → Then is the result. This is behavior we expect from the system when this action occurs in this context.

And → Using And, But you could make the example more fluidy structured by replacing the successive Given's, When's or Then's with And's and But's

Ex: 

Example: Multiple Givens
  Given one thing
  And another thing
  And yet another thing
  When I open my eyes
  Then I should see something
  But I shouldn't see something else

Examples → Examples is formula data test

Ruby

What is Ruby?
A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. 

What is Step Definitions?
Step definitions connect Gherkin steps to programming code. A step definition carries out the action that should be performed by the step. So step definitions hard-wire the specification to the implementation.


┌────────────┐                 ┌──────────────┐                 ┌───────────┐
   Steps                          Step                                 
 in Gherkin ├──matched with──> Definitions  ├───manipulates──>  System   
                                                                       
└────────────┘                 └──────────────┘                 └───────────┘

Step definitions can be written in many programming languages. Here is an example using ruby:

When(/^client sends a (GET|POST|PUT|DELETE|PATCH) request to "(.*)"(?: with body:)?$/) do |method, endpoint, *args|
  body = args.shift
  body_json = body ? JSON.parse(body) : nil
  endpoint = APIHelper.resolve_variable(self, endpoint)

  url = "#{ENV["URL"]}#{ENV["VERSION"]}#{endpoint}"
  @response = HTTParty.send(
    method.downcase,
    url,
    body: body_json,
    headers: @headers,
  )
end

Httparty

Http Request using Httparty.

Other Tools Recommendations

  • Code Editor: Visual Studio Code
  • Extension: 
- Rufo - Ruby Formater
- Cucumber (Gherkin) Full Support - Cucumber Support snippets

Folder Structure


└── features
    ├── helpers
    │   └── api
    │       └── <file-helper>.rb
    ├── scenarios
    │   └── api
    │       └── <service-name>
    │           └── <scenario_name>.feature
    ├── step_definitions
    │   └── api
    │       ├── api.rb
    │       └── <service-name>
    │           └── <service-name>_steps.rb
    └── support
        └── api
            └── <service-name>
                └── schemas
                    └── <json-file>.json

  • Directory feature file
scenarios/api/<service-name>/<scenario-name>.feature
In this directory we can create feature file based on the service name.

  • Directory step_definitions file
step_definitions/api/api.rb
In this directory for general step_definitions file
step_definitions/api/<service-name>/<file>.rb
In this directory we can create step definitions file based on the service name.
  • Directory JSON schema
support/api/<service-name>/schemas/<file>.json
In this directory we can create JSON schema file based on the service name.

Link References Learn Cucumber Ruby

How to install and Run Test Automation Api Testing with cucumber Ruby 


0 Response to "Documentation Automation Api Testing Using Cucumber Ruby Httparty"

Post a Comment

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel