Các bước làm Automation API Testing

Các bước làm Automation API Testing

Tags
Rest-Assured
API Testing
Published
January 18, 2024
Author
 
package org.example; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; import org.testng.annotations.Test; import static io.restassured.RestAssured.given; public class Basic_Auth { @Test public void testAPI01(){ //Khai báo đối tượng request để thiết lập điều kiện đầu vào //Dùng given() chỉ thị sự thiết lập sẵn điều kiện RequestSpecification request = given(); request.baseUri("https://bookstore.toolsqa.com"); request.basePath("/BookStore"); request.header("Accept","application/json"); request.header("Content-Type","application/json"); //Gửi request và lưu kết quả vào response Response response = request.when().get("/v1/Books"); //In kết quả từ response dưới dạng JSON đã format response.prettyPrint(); } }
public class Auth { @Test public void auth() { RequestSpecification request = given(); request.baseUri("https://bookstore.toolsqa.com"); request.basePath("/Account"); request.header("Accept","application/json"); request.header("Content-Type","application/json"); request.body("{\n" + " \"userName\": \"lamthunguyen\",\n" + " \"password\": \"LeAcademy@123\"\n" + "}"); Response response = request.when().post("/v1/Authorized"); response.prettyPrint(); // Ví dụ trên có 1 tham số, trường hợp khác có thêm tham số thì các bạn cứ chấm viết tiếp phía sau nhé. // // request.queryParam("username", "anhtester") // .queryParam("param1", 10) // .queryParam("param2", "java"); // //Verify từng giá trị trong response // response.then().statusCode(200); // response.then().statusLine("HTTP/1.1 200 OK"); // response.then().contentType(ContentType.JSON); //Lưu giá trị token vào biến TOKEN nhé // response.getBody().path() // //Verify data từ body dạng JSON // response.then().body("response[0].name", equalTo("Đất Rừng Phương Nam")); // response.then().body("response[0].price", equalTo(12000)); } }
 

Kiểm tra xem giá trị Response trả về có đúng với mong muốn của mình hay không?

 
// Giả sử API trả về JSON Body như này: { "message": "Success", "response": { "id": 1, "name": "Đất Rừng Phương Nam", "category_id": 1, "price": 12000, "release_date": "2023/10/29", "status": 1, "image": [ { "id": 1, "path": "public/images/1avh0VncWchVJd1SrYpcgLTU4NccDVW6Ck8ixmW3.png" }, { "id": 3, "path": "public/images/TwSX1W1RgW26ppX3fhDtxVcLV7tsJAooDtxJWBP7.png" } ] } } //Verify kết quả từ response với Assert trong TestNG //Dùng class Assert chấm gọi 2 hàm chính là assertEquals và assertTrue Assert.assertEquals(response.getStatusCode(), 200, "Status Code chưa đúng."); Assert.assertEquals(response.getContentType(), "application/json", "Content Type chưa đúng."); //Khi lấy kết quả trong body thì cần dùng đối tượng class ResponseBody để lấy hết về kiểu String //Khi đó chỉ có thể dùng hàm contains để so sánh chứa, vì không lấy được từng giá trị của từng key ResponseBody body = response.getBody(); Assert.assertEquals(body.asString().contains("Success"), true, "Message Success không tồn tại."); //Muốn lấy giá trị từng key trong JSON body để compare chính xác thì //phải dùng hàm then() hoặc thư viện JsonPath //Muốn lấy giá trị từng key trong JSON body để compare chính xác thì dùng JsonPath JsonPath jsonPath = response.jsonPath(); //Lưu hết body vào đối tượng jsonPath //Truy xuất giá trị theo key hoặc đường dẫn xpath theo cấp bậc String name = jsonPath.get("response.name"); System.out.println("Name: " + name);

Tham khảo