Verify that the API returns a success message if the resource is retrieved successfully.
Language: Java
Framework: Rest assured
1import org.testng.annotations.Test;2import static io.restassured.RestAssured.*;3import static org.hamcrest.Matchers.*;45public class APITest {6 7 String resourceUrl = "https://example.com/resource";89 @Test10 public void testVerifySuccessfulRetrieval() {1112 given().13 when().14 get(resourceUrl).15 then().16 assertThat().17 statusCode(200).18 body("message", equalTo("Resource retrieved successfully"));19 }20 21 // Commented code to connect to remote client with desired capabilities22 /*23 DesiredCapabilities capabilities = new DesiredCapabilities();24 capabilities.setBrowserName("chrome");25 capabilities.setPlatform(Platform.WINDOWS);26 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);27 */28}2930// Assumptions:31// 1. The resource URL is correct and retrieves the expected resource.32// 2. The API returns a JSON response with a "message" key.33// 3. A 200 status code indicates successful retrieval.
Language: Javascript
1// Mocha and Chai.23const chai = require('chai');4const expect = chai.expect;5const request = require('supertest');67describe('API Testing', function() {8 let api;910 beforeEach(function() {11 api = require('../src/app');12 });1314 afterEach(function(done) {15 api.close(done);16 });1718 it('should return a success message if the resource is retrieved successfully', function(done) {19 const id = 1; // Assume that the resource id to be retrieved is 120 const expectedMessage = 'Resource retrieved successfully'; // Assume the expected success message2122 request(api)23 .get(`/api/resource/${id}`)24 .expect(200)25 .end(function(err, res) {26 if (err) return done(err);2728 expect(res.body).to.have.property('message').to.equal(expectedMessage);29 done();30 });31 });32});3334// Code to connect to remote client with desired capabilities35// const webdriver = require('selenium-webdriver');36// const remote = require('selenium-webdriver/remote');37// const capabilities = webdriver.Capabilities.chrome();3839// capabilities.set('chromeOptions', {40// 'args': [41// '--no-sandbox',42// '--disable-gpu'43// ]44// });4546// const driver = new webdriver.Builder()47// .withCapabilities(capabilities)48// .usingServer('http://remotehub:4444/wd/hub')49// .build();
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