Showing posts with label TestNG. Show all posts
Showing posts with label TestNG. Show all posts

Friday, March 12, 2021

Introduction to GraphQL - Part 1

Introduction to GraphQL


GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data. GraphQL isn't tied to any specific database or storage engine and is instead backed by your existing code and data.

A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type. For example, a GraphQL service that tells us who the logged in user is (me) as well as that user's name might look something like this:

type Query {
  me: User
}
 
type User {
  id: ID
  name: String
}

Functions for each field for each type:

function Query_me(request) {
  return request.auth.user;
}
 
function User_name(user) {
  return user.getName();
}

The running service can receive GraphQl query to validate and execute. A received query is first check it refers to the types and fields defined and then runs the provided function to produce result.

Query:

{
  me {
    name
  }
}

Result:
 
{
  "me": {
    "name": "Vishal Patil"
  }
}

To understand further - GraphQL over HTTP
Check out the diagram below, to get a sense of how GraphQL is typically used in your stack:


As you can see we only need a single query in order to get information it need. We don’t need to burden the sever with multiple requests.

GraphQL client-server flow:
  • Note that the GraphQL query is not really JSON; it looks like the shape of the JSON you want. So when we make a 'POST' request to send our GraphQL query to the server, it is sent as a "string" by the client.
  • The server gets the JSON object and extracts the query string. As per the GraphQL syntax and the graph data model (GraphQL schema), the server processes and validates the GraphQL query.
  • Just like a typical API server, the GraphQL API server then makes calls to a database or other services to fetch the data that the client requested.
  • The server then takes the data and returns it to the client in a JSON object.

GraphQL Schema
  • GraphQL user schema to describe the shape of data
  • Schema defines a hierarchy of types with fields that are populated from backend
  • Schema specifies which queries or mutations available for client to execute against the data
  • Schema defines the collection of types and relationship between those types

In the below example, every book has an author and every author has a list of books.

type Book {
    title: String
    author: Author
}

type Author {
    name: String
    books: [Book]
}


Supported Types

Every type definition in GraphQL schema belong to one of the following categories

  • Scalar types
  • Object types
  • The Query type
  • The Mutation type
  • Input types
Scalar Types

Scalar types are similar to primitive data types in any programming language
  • Int: A signed 32‐bit integer.
  • Float: A signed double-precision floating-point value.
  • String: A UTF‐8 character sequence.
  • Boolean: true or false.
  • ID (Serialized as String): The ID scalar type represents a unique identifier

Object Type

Most of the types we define in a GraphQL schema are object types. An object type contains a collection of fields, each of which can be either a scalar type or another object type.

The two types of allowed operations in GraphQL are queries and mutations. 
  • Queries – operates like a GET
  • Mutations – operates like POST/PATCH OR UPDATE/DELETE

Queries
Queries return only the data user specify. To form a query user must specify fields within fields (nested fields)

Query Overview
The Query defines all of the top-level entry points for queries that clients execute against data. Query is interactive in nature. User can limit or add more variables to a query at any time to get the desired result

#syntax 1

query {
  JSON objects to return
}

#Example:

query Employees {
  Employees {
    name
    Employers {
      name
    }
  }
}



Mutations
To form a mutation  three things are needed 
  • Mutation Name
    • Type of modification
  • Input Object
    • Data want to sent to the server. Normally it passed as an argument to the mutation name
  • Payload Object
    • Data return from the server. Passed as a body to the mutation.
Mutation Overview
Mutations are structured like this:

type MutationResponse { 
  status: String!
}

type PassengerData {
  name: String! 
  age: String! 
  address: String!
}

type Mutation {
  createPassenger(passenger: String ): MutationResponse 
}

 #String is nothing but PassengerData

# Query in Frontend looks like:
mutation PassengerMutation($passenger: PassengerData! ) {
  createPassenger(passenger: $passenger) { 
    status
  }
}


    Note: The scalar type “!” mark indicates the field is required.


Working with variables:

Variables can make queries more dynamic and powerful, and they can reduce complexity when passing mutation input objects.
 
Three ways using  a variable
  • Define outside of the operation in a object
  • Pass the variable to the operation as an argument
  • Use with in operations.

Examples:


type MutationResponse { 
  status: String!
}

type PassengerData {
  name: String! 
  age: String! 
  address: String!
}


To understand more about GraphQL please refer the below links

  • https://graphql.org/learn/ 
  • https://www.tutorialspoint.com/graphql
  • https://developer.github.com/v4/guides/intro-to-graphql/

Thursday, January 21, 2021

Test Execution with Docker and Container

In this blog, we will see what is docker and container and how it helps in automation script execution.


What is a Docker?

Docker is a computer program that performs operating-system-level virtualization.

Docker is a tool designed to make it easier to create, deploy and run applications by using containers.

Docker is a software containerization platform, meaning you can build your application, package them along with their dependencies into a container and then these containers can be easily shipped to run on other machines.


What is Container and Image?

An image is a lightweight, standalone, executable package of software that includes everything needed to run an application, system tools, system libraries and settings

Container is the runtime instance of an image.


Difference between Containers and Virtual machines?

Virtualization is the technique of importing a Guest operating system on top of a Host operating system
Containerization is however more efficient because there is no guest operating system here and utilizes a host's operating system, share relevant libraries and resources as and when needed unlike virtual machines
Containers are lightweight and faster than virtual machines.


Why Docker and Container based environment for executing automation scripts?

It’s much quicker to get up and running using the pre-made containers than to try and set Selenium up from scratch. You don’t need to install Java.

You don’t need to install all the necessary browsers. Perhaps you don’t want to install Firefox or want to test with a specific older build of Chrome or one with specific plugin or capabilities?

Selenium can (and likely will) crash on you. Using containers means you can spin up a new Selenium instance when you need it, discard and then start fresh. Alternatively, if you do plan on leaving it running for extended periods of time, if/when it crashes you can just set to reboot.

Team members can use the same container regardless of what operating system they use so there are fewer discrepancies between environments. 

Installing Docker and Containers -

System Requirements

  • Windows 10 64-bit: Pro, Enterprise, or Education (Build 16299 or later).
  • For Windows 10 Home, see Install Docker Desktop on Windows Home.
  • Hyper-V and Containers Windows features must be enabled.
  • The following hardware prerequisites are required to successfully run Client Hyper-V on Windows 10:
  • 64 bit processor
  • 4GB system RAM
  • BIOS-level hardware virtualization support must be enabled in the BIOS settings.
Software Requirements

  • Docker - Download and install from "https://hub.docker.com/editions/community/docker-ce-desktop-windows". Once the docker hub is downloaded and install successfully go the command prompt and run command 
  • For web base test automation you will need to install Docker image for selenium grind. Go to "https://hub.docker.com/" and search for selenium in the search option. Download the selenium selenium/standalone-chrome. Make sure to download the one provided by Selenium. We can also download the same using docker command "docker pull selenium/standalone-chrome:<version#>". This command can be executed when docker is up and running. You can mention command as "docker pull selenium/standalone-chrome:latest" to get the latest version of selenium standalone for chrome Refer https://github.com/SeleniumHQ/docker-selenium 
  • Eclipse or similar IDE with TestNG plugin 
  • Plugin and dependencies for java-maven project


















 

To get Standalone Server Up and Running


   $ docker run -d -p 4444:4444 selenium/standalone-chrome:<v#>
        Note: v# is for version you want to download
  • -d runs the container in the background (detached)
  • -p 4444:4444 maps the local port 4444 to the port 4444 used by    the Selenium Server in the container
  • :latest is tag/version of the image to use
The above will get the latest released image for the Chrome Standalone Server. Alternatively, you can specify a specific version by using the relevant tag. For example:
    $ docker run -d -p 4444:4444 selenium/standalone-chrome:<v#>
        Note: v# is for version you want to download

After the pre-requisites are fulfilled, follow below steps

  • Create dockerUp.bat file to start docker.
  • Add below code in the batch file
     docker-compose -f docker-compose.yaml up >>output.txt
    
All the logs will be stored in output.txt file. Make sure the location of .yaml and .txt file is same
  • Create dockerDown.bat file to stop docker
     docker-compose -f docker-compose.yaml down

Using your preferred browser, go to http://localhost:4444/grid/console to bring up the console and confirm that you do, in fact, have a grid set up and running with single Chrome node.




If you want to add more nodes to your grid you can simply repeat the commands to add individual nodes as needed. For example:

$ docker run -d --link selenium-hub:hub selenium/node-chrome:latest
$ docker run -d --link selenium-hub:hub selenium/node-chrome:latest
$ docker run -d --link selenium-hub:hub selenium/node-chrome:latest

  • Create startDocker.java file. This code will be used to run the dockerUp.bat and dockerDown.bat files from our IDE before with out API or Selenium automation script so the automation scripts can be executed in docker-container.

   startDocker.java 




  • Create stopDocker.java file. This script will be used to run the  dockerDown.bat files from our IDE after our Selenium automation script so the docker and container are close successfully.

 stopDocker.java




Now with TestNG plugin you can run your automation scripts using @before annotation for above stateDocker.java and once the tests are executed you can use the @after annotation for your stopDocker.java to release all the resource utilized in the execution. All these flow can be easily managed / controlled with testng.xml 


Please feel free to utilize and share above contents.