Search This Blog

Breaking

Thursday, 29 April 2021

April 29, 2021

[Karate API Automation Framework] How to setup project from scratch in Intelij?



Pre-requisite:

  1. Java 1.8 
  2. Maven
  3. IntelliJ or Eclipse
       Karate Version:  0.9.6


       Steps to follow:

  1. Open your IDE. Create a new Maven Project by following the below path->

File- > New-> Project or New Project from launch page




2.    Select “Create from ArcheType” check box and click on “Add Archetype”

Archetype Details: https://mvnrepository.com/artifact/com.intuit.karate/karate-archetype/0.9.6





3.  Fill in below details and click on Ok.

      GroupId: com.intuit.karate

      ArtifactID: karate-archetype

      Version: 0.9.6



        4. You will see karate-archetype added to the list. Select it and click on Next.

       


      5. Give Your project a Name and Location (Or keep it default) and click on Next.





6. Click on Finish and select the window.



7. Let the build run and install all the necessary files.


8. It will set up the default project for you.




9. Now run the default created scenario from the feature file.



10. 
Run it with the help of Junit class.



11. 
Run All scenarios irrespective of tags.



Reference Links:

1. https://mvnrepository.com/artifact/com.intuit.karate/karate-archetype : Karate archetype/ maven repo link

2. https://github.com/intuit/karate : Documentation Link

3. https://www.youtube.com/watch?v=W-af7Cd8cMc -> How to configure Karate


Common Issues:


1. If first time creating any BDD project (feature file), then need to install “cucumber for java” and “Gherkin” plugin. (By default, Gherkin installs with cucumber for java)



    How to Install Plugin explicitly:

  1. https://www.jetbrains.com/help/idea/managing-plugins.html


   2.   While installing gherkin, cucumber for java, installed substep definition plugin also. Because of this, feature files are not running.

Expected: We don’t need to install the substep definition plugin at all.





Wednesday, 28 April 2021

April 28, 2021

[Karate API Automation Framework] [Oracle DB] Database Scenario Validation with Karate Framework

Recently, when we need to validate our APIs along with oracle DB, We could not do it just by adding maven dependency. We followed the below approach:

We first downloaded the oracle JDBC jar and later added it to a folder External Jar in our project.



Then we gave the link to this jar in our pom file like this:

   <dependency>

            <groupId>com.oracle.jdbc</groupId>

            <artifactId>JDBC</artifactId>

            <scope>system</scope>

            <version>1.0</version>

            <systemPath>${basedir}\ExternalJars\ojdbc8.jar</systemPath>

        </dependency>

And later we created a DbUtil java class in our project.


import javax.swing.*;

import java.sql.*;

import java.util.Arrays;

import java.util.Enumeration;

import java.util.List;

import java.util.Map;


public class dbUtil {

public static Connection dbConn = null;

public static String ANSI_RED = "\u001B[31m";


private dbUtil() throws SQLException {

}


public static ResultSet getDataFromDB(String dbQuery) throws Exception {

dbConn = DriverManager.getConnection("jdbc:oracle:thin:@HostName:Port:Service Name", "username", "password");


if (dbConn != null) {

System.out.println("Checking Database.");

}

Statement stmt = dbConn.createStatement();

ResultSet rs = stmt.executeQuery(dbQuery);

return rs;

}


public static List<String> getListFromDB(String dbQuery,String Column) throws Exception

{

List<String> arrayList = new ArrayList<>();

dbConn = DriverManager.getConnection("jdbc:oracle:thin:@HostName:Port:Service Name", "username", "password");

if (dbConn != null) {

System.out.println("Checking Database.");

}

Statement stmt = dbConn.createStatement();

ResultSet rs = stmt.executeQuery(dbQuery);

while (rs.next())

{

arrayList.add(rs.getString(Column));

}

return arrayList;

}

public static String getCountFromDB(String dbQuery) throws Exception

{

ResultSet rs = getDataFromDB(dbQuery);

while (rs.next())

{

return rs.getString(1);

}

return "";

}

}

This code we called from our karate framework feature file:

@DBTest
Feature: Database Connectivity with karate framework
Background:
Given url baseURL
* def helper = Java.type("helper")
* def DbUtils = Java.type("dbUtil")

Scenario: Validate API record count from DB
* def oracleDbCount = DbUtils.getCountFromDB("select count(*) from tableName where condition
And print oracleDbCount
And path "apiPath"
When method get
Then status 200
* def
countFromAPI= response.metadata.count
And print countFromAPI
And assert oracleDbCount == countFromAPI


* def dbData = DbUtils.getListFromDB(<Query>,'<Field>')
And def dbDataList = new ArrayList()
* eval for(var i = 0; i < dbData.length; i++) dbDataList.add(dbData[i])
And print fieldsToCheck
And print dbDataList
And assert dbDataList.length == fieldsToCheck.length
And assert helper.fnCompareTwoList(fieldsToCheck,dbDataList)

Friday, 23 April 2021

April 23, 2021

[Jenkin] -> Showing Junit Test results in Jenkin Build page


If you are using JUnit in your testing framework along with the surefire plugin for generating reports, then there is a beautiful way to show these reports in Jenkin console from your pipeline job. 

You just need to put one line in your script:

 junit  '**/target/surefire-reports/*.xml'


If you are using Karate framework, then this piece of code will solve your purpose:



stage('Build') {

                   try {

    bat '''mvn clean test -DargLine='-Dkarate.env=QA-US' -Dkarate.options="--tags @SmokeAPI" -Dtest=CucumberReport -DfailIfNoTests=false'''

                   }  finally {

        junit  '**/target/surefire-reports/*.xml'

    }

}   


Once you will add this code, your test result will come in this way in Jenkins build console:











Friday, 9 April 2021

April 09, 2021

[Jmeter] -> How to put assertion in response body parameters?

 Recently, we faced a situation wherein our performance test, we need to put an assertion on one of the response body parameter field values. We googled about it but could not find any straightforward answer.

I posted the question in StackOverflow as well but unfortunately could not found the solution from there also. Later, got a solution from one of my colleague who has expertise in Jmeter.

https://stackoverflow.com/questions/66989810/jmeter-less-than-or-greater-than-assertion-in-jmeter-assertion-on-api-respon

So, to put an assertion in the response body parameter, first, you need to find out that value from the response body. So for this purpose, we first added a Post -Processor: JSON -Extractor.




Inside this, we first fetched the value and stored this value inside a JMeter variable:

queryTime = $.metadata.queryTime



Later, we added a JSSR23 assertion to put our assertion condition.


And there we added our code for validation:

String jsonString = vars.get("queryTime");
int length1=jsonString.length();
String Qtime1=jsonString.substring(0,(length1-2));
int time = Qtime1.toInteger()
log.info ("The queryTime is " + time);

if (time>1000)
{
AssertionResult.setFailureMessage("The queryTime is taking more than 1000ms");
AssertionResult.setFailure(true);
}


The same assertion later came in a result like below:


If you need this field value in summary report excel, you will have to add this variable in user.properties file which will be available in the bin folder.