Verify that the API request parameters are correctly passed to the API.
Language: Java
Framework: Rest assured
1// Assumptions: 2// - The API endpoint is already defined3// - The request parameters are passed via query string4// - A successful API response should return a HTTP status code of 2005// - The expected request parameters are firstName and lastName67import org.testng.annotations.Test;8import io.restassured.RestAssured;9import io.restassured.response.Response;10import static io.restassured.RestAssured.given;1112public class APITest {1314 @Test15 public void verifyRequestParams() {16 // Define the endpoint URL17 RestAssured.baseURI = "https://example-api.com";18 19 // Define the expected request parameters20 String firstName = "John";21 String lastName = "Doe";22 23 // Define the query string parameters24 String queryParams = "?firstName=" + firstName + "&lastName=" + lastName;25 26 // Send the GET request to the API with the query string parameters27 Response response = given().log().all()28 .when()29 .get("/users" + queryParams)30 .then()31 .extract().response();32 33 // Verify that the API response returns a HTTP status code of 20034 assert response.getStatusCode() == 200 : "API request failed with HTTP status code: " + response.getStatusCode();35 36 // Verify that the API request parameters are correctly passed to the API37 assert response.getBody().asString().contains("\"firstName\":\"" + firstName + "\",") : "API response does not contain expected firstName request parameter";38 assert response.getBody().asString().contains("\"lastName\":\"" + lastName + "\",") : "API response does not contain expected lastName request parameter";39 40 // Additional code to connect to remote client with desired capabilities41 // Uncomment when executing tests on the remote client42 43 // RemoteWebDriver driver = new RemoteWebDriver(new URL("http://remote-machine:4444/wd/hub"), DesiredCapabilities.chrome());44 // driver.get("https://example-api.com");45 }46}
Language: Javascript
1// Code for API Testing in Javascript using Cypress framework:23// Assumptions:4// 1. API is available at "https://example.com/api"5// 2. API endpoint for request parameters is "/api/params"6// 3. Request parameters are passed as a JSON object in the request body7// 4. API response contains status code and response body in JSON format89// Import Cypress library10import cy from 'cypress';1112// Declare API endpoint13const API_ENDPOINT = 'https://example.com/api/params';1415// Define test case for verifying request parameters16describe('API Testing - Verify request parameters', () => {17 it('Verify that the API request parameters are correctly passed to the API', () => {18 // Define request parameters as a JSON object19 const requestParams = {20 param1: 'value1',21 param2: 'value2'22 };2324 // Send API request with request parameters25 cy.request('POST', API_ENDPOINT, requestParams)26 .then((response) => {27 // Verify response status code28 expect(response.status).to.equal(200);2930 // Verify response body contains request parameters31 expect(response.body).to.deep.equal(requestParams);32 });33 });34});3536// Optional: Code to connect to remote client with desired capabilities37// Assuming remote client is available at "https://example.com"38// and uses Chrome browser with version 90.039Cypress.config('baseUrl', 'https://example.com');40Cypress.config('defaultCommandTimeout', 10000);41Cypress.config('viewportWidth', 1920);42Cypress.config('viewportHeight', 1080);43Cypress.config('testFiles', '**/*.spec.js');44Cypress.config('chromeWebSecurity', false);4546const chromeBrowser = {47 browserName: 'Chrome',48 browserVersion: '90.0',49 'goog:chromeOptions': {50 args: [51 '--no-sandbox',52 '--disable-web-security'53 ]54 }55};5657const desiredCapabilities = {58 browserName: 'chrome',59 chrome: {60 version: '90.0'61 }62};6364Cypress.on('before:browser:launch', (browser = {}, launchOptions) => {65 if (browser.family === 'chrome') {66 launchOptions.args.push('--no-sandbox');67 launchOptions.args.push('--disable-web-security');68 return launchOptions;69 }70});7172cy.remote({73 user: 'username',74 key: 'accesskey',75 spec: 'cypress/integration/*.spec.js',76 env: {77 baseURL: 'https://example.com'78 },79 browser: chromeBrowser,80 desiredCapabilities: desiredCapabilities81});
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