Verify that the API returns a success message if the resource is updated successfully.
Language: Java
Framework: Rest assured
1//Assumptions:2//1. URL to API is known3//2. API resource to be updated is known4//3. User has authorization to make changes5//4. Test data for update is available67import io.restassured.RestAssured;8import io.restassured.response.Response;9import org.junit.Test;10import static io.restassured.RestAssured.given;11import static org.junit.Assert.assertEquals;1213public class APITest {1415 //Test to check successful update of resource using Rest Assured16 @Test17 public void testSuccessfulUpdate(){18 //Base URL of API19 RestAssured.baseURI = "https://example.api.com";2021 //Request body data22 String requestJson = "{\n" +23 " \"resource\": \"updated_data\"\n" +24 "}";2526 //Making API call to update resource27 Response response = given().body(requestJson).when().put("/resource-to-update");2829 //Get status code30 int statusCode = response.getStatusCode();3132 //Verify success message is returned for successful update33 assertEquals(200, statusCode);34 assertEquals("Success message", response.getBody().asString());3536 //Connect to remote client with desired capabilities37 //Assumptions:38 //1. Remote URL and desired capabilities are known39 //2. Driver is compatible with desired capabilities40 //3. Remote client is accessible41 //WebDriver driver = new RemoteWebDriver(new URL("http://remote-url:4444/wd/hub"), DesiredCapabilities.chrome());42 }43}
Language: Javascript
1const fetch = require('node-fetch');23async function updateResource(endpoint, data) {4 const response = await fetch(endpoint, {5 method: 'PUT',6 body: JSON.stringify(data),7 headers: { 'Content-Type': 'application/json' },8 });9 10 if (response.ok) {11 const json = await response.json();1213 // Check if the success message is returned14 if (json.success) {15 console.log('Resource updated successfully');16 return true;17 } else {18 console.log('Resource not updated successfully');19 return false;20 }21 } else {22 console.log(`Failed to update resource: ${response.status}`);23 return false;24 }25}2627// Test case using a local driver28const endpoint = 'http://localhost:3000/api/resources/1';29const data = { name: 'Updated resource' };30updateResource(endpoint, data);3132// Code to connect to remote client with desired capabilities33// const { Builder } = require('selenium-webdriver');34// const capabilities = {35// browserName: 'chrome',36// platformName: 'Windows 10',37// };38// const driver = await new Builder()39// .usingServer('http://your.remote.url:4444/wd/hub')40// .withCapabilities(capabilities)41// .build();42// const endpoint = 'http://your.server.url/api/resources/1';43// const data = { name: 'Updated resource' };44// await updateResource(endpoint, data);
Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing