REST Assured Automation Resource 1
REST Assured Automation Resource 1
Index
1. Open the Postman app and select the collection you want to run in the
left sidebar.
2. Click on the "Runner" button on the top-right corner of the app.
3. In the Collection Runner window, you can configure the following
options:
● Environment: Select the environment you want to use for running the
collection. An environment is a set of variables that you can use in your
requests to parameterize them.
● Iterations: Define the number of times you want to run the collection.
● Delay: Define the time delay (in milliseconds) between each request.
● Data: If you have any data files associated with your collection, you can
select them here.
4. Click on the "Start Run" button to run the collection.
5. Once the collection run is complete, you can view the results of each
request in the Collection Runner window. You can view the response
data, headers, and status codes for each request.
6. You can also export the results of the collection run as a JSON file or a
CSV file.
Overall, the Collection Runner is a powerful tool that allows you to test and
automate your API requests in a systematic and efficient manner.
4
In this example, the cookie is added to the request header using the cookie()
method, and the response is verified using the statusCode() and body() methods.
import static io.restassured.RestAssured.given;
import Java.io.File;
import io.restassured.response.Response;
System.out.println(response.getStatusCode());
}
}
In this example, we're using the multiPart method to upload a file. The multiPart
method takes two parameters: the name of the form field (file in this case)
and the file object.
import Java.io.FileOutputStream;
5
import Java.io.IOException;
import Java.io.InputStream;
import Java.io.OutputStream;
import io.restassured.response.Response;
int read;
byte[] bytes = new byte[1024];
outputStream.close();
inputStream.close();
}
}
In this example, we're using the Response.getBody() method to get the response
body as an input stream. We then write the input stream to a file using an
output stream. Note that we're using a buffer (bytes) to read and write the file
in chunks of 1024 bytes, which helps to conserve memory.
Image
6
Serialization is like packing your favorite toy into a box so you can give it to
your friend who lives far away. You put it in the box and wrap it up so it
doesn't get damaged, and then send it to your friend. When your friend
receives the box, they can open it up and see your toy just like you packed it.
Deserialization is like when your friend receives the box with your toy inside.
They unwrap it and take the toy out of the box. The toy is just like how you
packed it and they can play with it just like you did.
Similarly, in computers, serialization is like packing data into a format that can
data back into its original form so that it can be used again.
Serialization and deserialization are concepts that are used to convert data structures or
objects into a format that can be easily transmitted over a network or stored in a file, and
bytes, which can then be transmitted or stored. The process of deserialization involves
converting the byte stream back into the original object or data structure.
Rest Assured is a testing library for RESTful web services, and it also supports serialization
and deserialization of objects. Rest Assured uses the ObjectMapper class from the Jackson
To serialize an object using Rest Assured, you can use the given().body() method and pass
the object as an argument. Rest Assured will automatically serialize the object to JSON
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
RestAssured.given()
.contentType(ContentType.JSON)
.body(myObject)
.post("https://example.com/api/myobject");
}
}
In this example, we're serializing the MyObject object and sending it as the
request.
8
Response.getBody().as() method and pass the class of the object you want to
import io.restassured.RestAssured;
import io.restassured.response.Response;
TestNG
TestNG:
https://automationreinvented.blogspot.com/2021/01/top-60-interview-questions-on.h
tml
9
When it comes to designing a TestNG test suite using the testng.xml file,
there are various techniques that can be used to ensure that the tests are
well-organized and easy to manage. Here are some of them:
tests allows you to run only specific tests or groups of tests when
required, reducing the overall test execution time.
2. Using parameters: TestNG allows you to pass parameters to your test
methods using the @Parameters annotation. This can be done in the
testng.xml file by defining the parameter values and passing them to
the test methods. Using parameters allows you to run the same test
with different inputs, reducing the need for duplicate tests.
3. Test dependency: TestNG allows you to define test dependencies using
the dependsOnMethods attribute. This ensures that certain tests are run
10
only after the required tests have passed. Test dependencies help in
ensuring that the tests are executed in a specific order, preventing any
unnecessary failures.
4. Parallel execution: TestNG allows you to run tests in parallel, either at
the method level or the suite level. This can be done using the parallel
attribute in the testng.xml file. Parallel execution allows you to reduce
the overall test execution time by running tests simultaneously.
5. Data providers: TestNG allows you to use data providers to pass data to
your tests. This can be done using the @DataProvider annotation or by
defining the data provider in the testng.xml file. Using data providers
allows you to run the same test with different sets of data, reducing the
need for duplicate tests.
By using these techniques, you can design a TestNG test suite that is
well-organized, easy to manage, and reduces the overall test execution time.
Create a new XML file and define your test suite in it. You can do this by using
the <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> declaration and
defining your suite with the <suite> tag.
1. Inside the <suite> tag, define the classes you want to include in your
suite using the <classes> tag.
2. Within the <classes> tag, define the class you created in step 1 using the
<class> tag.
11
3. Save your XML file and run it using TestNG. You can do this by
right-clicking on the file and selecting "Run as TestNG Suite".
<classes>
</classes>
</test>
</suite>
https://testng.org/doc/documentation-main.html
<groups>
<run>
13
<include name="RegressionSuite"/>
</run>
</groups>
<classes>
</classes>
</test>
</suite>
<classes>
</classes>
</test>
</suite>
<classes>
15
<class name="userManagement.getUsers"/>
</classes>
</test>
<classes>
<class name="userManagement.getPostmanEcho"/>
</classes>
</test>
<classes>
<class name="userManagement.getErgast"/>
</classes>
</test>
</suite>
16
package com.amazon.business.utils;
import org.testng.asserts.SoftAssert;
public SoftAssertionUtil() {
softAssert = new SoftAssert();
}
} catch (AssertionError e) {
softAssert.fail(message);
}
}
softAssertionUtil.assertTrue(page.CURATION_HEADER.isPresent(),
" Header should be present");
softAssertionUtil.assertTrue(page.CURATION_MESSAGE.isPresent(),
" Message should be present");
softAssertionUtil.assertAll();
18
Let's say you have an API endpoint that takes two parameters, id and name,
and returns some data. You want to test this endpoint with different values
for id and name. You can use TestNG parameters to achieve this:
@DataProvider(name = "testdata")
public Object[][] testData() {
return new Object[][] {
{"1", "John"},
{"2", "Jane"},
{"3", "Bob"}
};
}
2. In your test method, use the @Test annotation with the dataProvider
attribute to specify the data provider method and the @Parameters
annotation to specify the parameter names. For example:
@Test(dataProvider = "testdata")
@Parameters({"id", "name"})
public void testEndpoint(String id, String name) {
19
given()
.param("id", id)
.param("name", name)
.when()
.get("/endpoint")
.then()
.statusCode(200);
}
In this example, the testdata data provider method returns three sets of
parameter values: 1 and John, 2 and Jane, and 3 and Bob. The testEndpoint test
method is annotated with @Test(dataProvider = "testdata"), which tells TestNG to
use the testdata data provider method to provide the parameter values. The
@Parameters({"id", "name"}) annotation specifies that the parameter values
When you run this test, TestNG will execute the testEndpoint test method three
times with the different parameter values provided by the testdata data
provider method. This allows you to test the same API endpoint with different
parameter values without duplicating the test code.
RetryListener
When writing API automation tests with Rest Assured, it is not uncommon to
encounter flaky tests. These are tests that sometimes pass and sometimes
fail, even though the system under test has not changed. The most common
causes of flaky tests are network issues, server overload, and race conditions.
20
Flaky tests can be frustrating, as they waste time and effort and can lead to
To address the issue of flaky tests, TestNG provides a Retry Listener that allows
tests to be retried a certain number of times if they fail. The Retry Listener can
tests, which can increase the stability and reliability of the test suite.
Here are some of the benefits of using a Retry Listener in API automation
1. Reduced false positives and false negatives: By retrying failed tests, the
conditions. By retrying failed tests, the test suite can adapt to changing
3. Faster feedback: Retry listeners can help to reduce the time it takes to
get feedback on the test results. Instead of having to rerun failed tests
manually, the Retry Listener can automatically retry them, which saves
Overall, using a Retry Listener in API automation with Rest Assured can help
to improve the stability and reliability of the test suite, reduce false positives
and false negatives, and provide faster feedback on the test results.
21
Link
Extent Report
Sure! Here are the steps to install Jenkins server on Windows, Mac, and Linux machines,
Windows:
https://www.jenkins.io/download/
3. Choose the recommended plugins to install, or select the plugins you need.
Mac:
1. Install Homebrew, a package manager for macOS, if you haven't already. You can
4. Start the Jenkins service with the command "brew services start jenkins".
interface.
Linux:
1. Open a terminal window and update the package manager with the command
4. Start the Jenkins service with the command "sudo systemctl start jenkins".
interface.
Note that the installation steps may vary depending on the specific version of Windows,
Mac, or Linux you are using, as well as the Jenkins version. You can refer to the official
Spec builders in Rest Assured allow you to define the request and response
specifications for your API tests. Here are the steps to use spec builders in
Rest Assured:
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
tests:
make a GET request to the /users/1234 endpoint and verifying that the
Using spec builders can help you write cleaner and more maintainable API
tests by separating the request and response details from the test logic.
Example
First of all, let us create a JSON document with name sample.json with the 6
key-value pairs and an array as shown below −
{
"ID": "1",
"First_Name": "Sidharth Shukla",
"Last_Name": "Bhagavatula",
"Date_Of_Birth": "1989-09-26",
"Place_Of_Birth":"Vishakhapatnam",
"Salary": "25000"
"contact": [
"e-mail: [email protected]",
"phone: 9848022338",
"city: Hyderabad",
"Area: Madapur",
"State: Telangana"
25
]
}
Just like other element retrieve the json array using the get() method into the
JSONArray object.
OR
26
Following Java program parses the above created sample.json file, reads its
contents and, displays them.
Example
import Java.io.FileNotFoundException;
import Java.io.FileReader;
import Java.io.IOException;
import Java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadingArrayFromJSON {
public static void main(String args[]) {
//Creating a JSONParser object
JSONParser jsonParser = new JSONParser();
try {
//Parsing the contents of the JSON file
JSONObject jsonObject = (JSONObject) jsonParser.parse(new
FileReader("E:/test.json"));
//Forming URL
System.out.println("Contents of the JSON are: ");
System.out.println("ID: "+jsonObject.get("ID"));
System.out.println("First name: "+jsonObject.get("First_Name"));
System.out.println("Last name: "+jsonObject.get("Last_Name"));
System.out.println("Date of birth: "+ jsonObject.get("Date_Of_Birth"));
System.out.println("Place of birth: "+ jsonObject.get("Place_Of_Birth"));
System.out.println("Salary: "+jsonObject.get("Salary"));
//Retrieving the array
JSONArray jsonArray = (JSONArray) jsonObject.get("contact");
System.out.println("");
27
ID: 1
Salary: 25000
Contact details:
e-mail: [email protected]
phone: 9848022338
city: Hyderabad
28
Area: Madapur
State: Telangana
}
import Java.io.File;
import Java.io.IOException;
import Java.nio.file.Files;
import org.json.*;