This is a sample project to automate rest full API. Will add more such code and tutorial coming forward.
POM.XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.automationfraternity</groupId> <artifactId>APIAutomation</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <cucumber.version>4.2.0</cucumber.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured --> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>4.0.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.14.3</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!-- [[1.]] Plugin to Execute all the test under test folder of Maven TestNG driver file can also be configured for help: mvn surefire:help --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.14.1</version> <configuration> <!-- Suite testng xml file to consider for test execution --> <suiteXmlFiles> <suiteXmlFile>src/test/resources/TestNgDriver.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> </project> |
Test NG XML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="UTF-8"?> <suite parallel="false" name="Suite"> <test name="ReqResAPI"> <classes> <class name="restapi.reqres.TC_ReqRes" /> </classes> </test> <!-- Test --> <test name="CountriesAPI"> <classes> <class name="restapi.restcountrieseu.TC_RestApiCountries" /> </classes> </test> <!-- Test --> </suite> <!-- Suite --> |
Test NG Test for Rest Assured:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
package restapi.reqres; import org.testng.Assert; import org.testng.Reporter; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import io.restassured.response.Response; import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class TC_ReqRes { private String _baseUrl = "https://reqres.in"; @Test(dataProvider="DP_DataProvider_UsersInfo",enabled=false) public void t_01_rest_get_users_validate_status_code(String endPointUrl, String statusCodeExpected) { Reporter.log("Data Sent from Data Provider : End Point: " + endPointUrl + " Expected Status Code: " + statusCodeExpected,true); //Get the response Response resp = given(). baseUri(_baseUrl). when(). get(endPointUrl).thenReturn(); //Get the Status code from Response int statusCodeActual = resp.getStatusCode(); //Assert Status code Assert.assertEquals(statusCodeActual, Integer.parseInt(statusCodeExpected),"Status code Validation"); } @Test(enabled = false) public void t_02_get_fetch_all_users_info() { Response resp = given(). baseUri(_baseUrl). when(). get("/api/users/2").thenReturn(); int statusCode = resp.getStatusCode(); if (statusCode==200) { //Equal to is Hamcrest Matcher. Check the import at the top //Hamrest is a transitive dependency i.e. you do not have to download resp.then().body("data.first_name", equalTo("Janet")); Reporter.log("Test case passed. First name returned correctly. Full Json string: " + resp.asString(),true); }else { Assert.assertTrue(false, "Status code is not 200"); } } @DataProvider public Object[][] DP_DataProvider_UsersInfo() { String[][] result = { {"/api/users?page=1","200"}, {"/api/users?page=2","200"}, {"/api/users/1","200"}, {"/api/users/2","200"}, {"/api/users/3","200"}, {"/api/users/4","200"}, {"/api/users/5","200"}, {"/api/users/6","200"}, {"/api/users/7","200"}, {"/api/users/8","200"}, {"/api/users/9","200"}, {"/api/users/10","200"}, {"/api/users/11","200"}, {"/api/users/12","200"}, {"/api/users/23","404"}//User not found }; return result; } } |