Search This Blog

Breaking

Tuesday, 16 March 2021

March 16, 2021

API Automation with restQA tool

Recently, I came across a new open-source tool restQA which is available now for API Automation. 

I found this tool very useful and easy to adopt. The good thing with this tool is that it gives a true flavor of BDD

In API Automation. Practically, with this tool, We can start writing our automation parallel with API development, and even BA's and PO's can also contribute to writing feature files.


Let me briefly explain some of the features which I liked about the tool:


  1. Plain English-like syntax for API requests makes API Automation scenarios more readable. Anyone can now write and understand the API automation from the feature file.
  2. Support of 50+ already implemented step definitions
  3. Very quick to install and quick to use
  4.  Easy integration with other plugins like Jenkin, slack, etc
  5.  Inbuilt reporting


Though the tool is still evolving, and it might be that you will see some crucial steps missing, still it gives hope to use an open-source BDD tool in API Automation.


Let me share my experience with the tool while automating a simple CRUD scenario:


Software Required:

  • NodeJs 
  • Npm or Yarn 

Steps Followed:

1. Install NodeJS and NPM in machine. In My machine, I already have this softwares. So I just corss checked. :)

deepakmathpal@Deepaks-MacBook-Air restqaTest % node --version

v12.18.3


deepakmathpal@Deepaks-MacBook-Air restqaTest % npm -version

6.14.6


2. Created a folder and inside that, installed the project with the below command (globally):


npm install @restqa/restqa -g


3. Initialize your first project:


restqa init -y



That's It !! The project is ready to use now. You can now import this folder into your Visual studio code. 

And now you can start writing your own feature file with the help of predefined steps:


You can get all the predefined implemented steps with the help of the below commands:


restqa steps given

restqa steps when

restqa steps then







 


Now, you can start automating your use cases.


Feature: Validate CRUD Behaviour


Background: 

Given I have the api gateway hosted on "https://jsonplaceholder.typicode.com"


Scenario: Get all post

  And I have the path "posts"

  And I have the method "GET"

  When I run the API

  Then the response time is under 1000 ms


  Scenario: create a new post

    And I have the path "posts"

    And the payload: 

    """

    {"title":"foo","body":"bar","userId":1}


    """

    And I have the method "POST"

    When I run the API

    Then I print the request

    And I print the response

    And I should receive a response with the status 201

    And the response should not be empty array  

    And the response body at "userId" should equal 1

    And the response body at "title" should equal "foo"

    And the response body at "body" should equal "bar"

    And the response body at "id" should equal 101 








Once the scenario is complete, you can run all your feature file scenarios:

restqa run 

The execution will start, and a report link will generate at the end of the execution.





When you open the link, a report comes like this:










If you want to run only your feature file scenario, you can run it with the below command:

restqa run tests/integration/CRUDScenario.feature 

Your report will now show only specified feature file scenarios:










That's it for now. I will keep updating when I will see more features from this project.

More detail of the project can be found from: https://docs.restqa.io/

Till then Happy Learning.

Wednesday, 3 March 2021

March 03, 2021

HashMap vs LikedHashMap ->How it make difference?

 So there is a standard problem that is given at most of the interview problems. It is the Printing frequency of characters from the given string. 

So when I was asked this question in one of the interviews, I immediately created a hashmap. 

And with the help of that, I find the occurrence of each character.

String s = "deepakmathpal";

char[] charArray = s.toCharArray();

HashMap<Character, Integer> basemap = new HashMap<>();

for(char c:charArray) {

if(basemap.containsKey(c)) {

basemap.put(c, basemap.get(c)+1);

} else {

basemap.put(c, basemap.get(c)+1);

  }

}

basemap.forEach((key,value) -> {

System.out.print(key + ": " );

System.out.print(value);

});





This printed the frequency as:

p: 2

a: 3

d: 1

t: 1

e: 2

h: 1

k: 1

l: 1

m: 1


Did you notice one thing here? Though the output is printing occurrence, however, it is not following the order. If it has followed the order, then below should be the output.

d: 1

e: 2

p: 2

a: 3

k: 1

m: 1

t: 1

h: 1

l: 1

The reason behind this is because the hashmap does not adhere to the insertion order. If you need to process items maintaining the insertion order, you should create a linked hashmap.


So instead of this line: 

HashMap<Character, Integer> basemap = new HashMap<>();


if we use:

HashMap<Character, Integer> basemap = new LinkedHashMap<>();

We will get the output following the order as well.

This will help us if we need to find out the first repeating character from the string:

System.out.println("----------------Printing first recurring-------------------------------------------------" );

String value = basemap.entrySet().stream().filter(entry -> entry.getValue()>1).findFirst().toString();

System.out.println(value );


Check out the difference by running this program with hashmap and linked hashmap.

Till then, happy learning.


March 03, 2021

Reverse each word in a sentence

 If we need to reverse each word in a sentence, we can use the split feature in String to first get each word from the string. Later on that word, we can perform the reverse operation.

This question is asked a lot of time at beginner-level automation interviews.

So here are the steps:

1.     First split the sentence using a split feature of string. It will return an array of strings.

2.   Create a StringBuilder object.

2.    Now use a for loop. and inside that for loop, use convert string to StringBuilder.

3.   Apply StringBuilder class built-in reverse feature.





For Example:

String s = "TestUser Loves Coding";

1. String words = s.split("\\s");

2. StringBuilder sb = new StringBuilder();

3. for(String s: words) {

sb = sb.append(new StringBuilder(w).reverse()).append(" ");

}

4. System.out.println(sb);


Output: resUtseT sevoL gnidoC