Saturday, July 17, 2021

A-Z About Selectors | What are the locator in selenium??

In simple Term locator is some unique attribute which we can use to identify the Web Element on a web page in order to perform an action on it.

Any Web Element on a page is created using HTML attribute ( name , type , id , etc ) . we can use their attribute to perform an action on it.



Image by: https://www.besanttechnologies.com/



1) Id locator:

 ID is the most common locator used in selenium. Syntax to locate web element using ID is

For example, we can locate 'signup' button of facebook signup page as bellow



driver.findElement(By.id("u_3_s_1I");


2) Name:
We can locate a web element using Name attribute also. syntax for locating web element using name is 




driver.findElement(By.name("firstname");


3)Class name : To locate a web element using class name





driver.findElement(By.className("inputtext _58mg _5dba _2ph-");

4) TAG name :



driver.findElement(By.tagName("input");

5) Link Text , Partial Link Text : 



driver.findElement(By.linkText("terms"));

The only difference between link text and partial link text is if we use partial link text it will find link which contains a keyword while link text will find the link which contain the excat keyword. so if we have a link for which text is changing by some part of text is not changing we can use the partial link text.

driver.findElement(By.partialLinkText("terms"));

6) CSS locator : CSS locator is fastest locator in selenium as compare to any other locator



input[name="firstname"]

7) X-path locator :


//button[@type='submit']
X-Path and CSS locator are very broad topic , i wil create a another blog for it in which i will cover X-PATH axes.

Sunday, April 11, 2021

How to automate POST request using Rest Assured | Rest API testing | API Testing

 To Automate an POST request using Rest Assured first we need to setup and Project in maven.

if  you have any difficulty in that section please refer this post to setup and maven project.

Now, first we need to Setup baseURI and than we need Request Object.


		RestAssured.baseURI="https://reqres.in/api/";
		
		//creating request object
		
		RequestSpecification request=RestAssured.given();
		

Since it is an POST request, we need to create a body parameter which we will send as a request payload along with some request header.


	JSONObject param= new JSONObject();
		
		param.put("QA","stock market");
		
		//adding header to the request
		
		request.header("Content-Type","application/json");


Here, i have only 1 body parameter  in the request so i have added only 1 parameter (QA,Stock market)

Now, we need to send this body parameter to the request in JSON format. 

//sending body param to the request
		
		request.body(param.toJSONString());


Now, to Fetch the response 

//fetching response
		
		Response response=request.request(Method.POST,"users");
		
		//printing response
		
		System.out.println("Response data is ==>>"+response.getBody().asString());
		

With this response object we can perform many validation, for example to validate any key(QA) value which need to be present in the response 


//verifying user details
		
		String job=response.jsonPath().get("QA");
		
		Assert.assertEquals(job,"stock market");
		


This is how we can validate POST request with Rest Assured.


I hope i was able to help you :) Happy learning

Rest API Automation | Rest API | How to automate API using Rest Assured with JAVA wih GET method

 API in simple words is bridge between 2 applications. an simple example of an API is when we book an rail ticket from any platform (Amazon or Paytm) we have noticed that after entering the user details it redirect us to the IRCTC website. so here that process which negivate us to the IRCTC is done threw the API.

There are  types of API:

1) SOAP API

2) Rest API

now, to validate any API there are many tools available in market like postman, or swagger. but here i am showing you an example how can we automate Rest API using Rest Assured.

To  create a setup there is 3 steps which we need to follow

1) Create and Maven Project (File > New > Others > Maven project)


Step 2 : After creating Maven project. go to the pom.xml file and add these dependencies in it

Add these dependencies in pom.xml file in dependencies tag

password for this file : 11223344

Step 3 : After creating maven project and adding these dependencies in project, create an TestNG class.

Now, after creating setup first thing we need to do is to setup and base URI of the API

@Test
	
	public void getData()
	{
		
		RestAssured.baseURI="https://restcountries.eu/rest/v2/alpha/";

here i have added a dummy API which you can use too.

After creating the baseURI, now we need to create 2 objects: Request object and Response object

//creating request object
		
	RequestSpecification request = RestAssured.given();
	
	//creating response object
	
	Response response = request.request(Method.GET,"ind");

while creating response object we need to select a method and the path of the API.

Now, we have created the request object and response object now we can fetch the response.

	String responsedata=response.getBody().asString();
	
	System.out.println("Request data is==> "+responsedata);
	
	int status_code=response.getStatusCode();
	
	System.out.println("response code is==>"+status_code);

here, we can play with the response object and perform any validation which we need.


I hope i am able to help you with this. please share with your friends and collogues as well :)