Verify that the API correctly handles client-side validation and returns the correct HTTP status code and error message.
Language: Java
Framework: Rest assured
1import org.junit.jupiter.api.Test;2import static io.restassured.RestAssured.given;3import static org.hamcrest.Matchers.*;45public class APITest {67 @Test8 public void verifyClientSideValidation() {9 // Assumptions:10 // - The API endpoint for the client-side validation is "/api/validation"11 // - Invalid input is sent as a POST request body in JSON format { "input": "invalid" }12 // - HTTP status code 400 is returned for invalid input13 // - The error message returned for invalid input is "Invalid input provided"1415 given()16 .contentType("application/json")17 .body("{\"input\": \"invalid\"}")18 .when()19 .post("/api/validation")20 .then()21 .statusCode(400)22 .body("message", equalTo("Invalid input provided"));23 24 // Uncomment the following code to connect to a remote client with desired capabilities25 /*26 DesiredCapabilities capabilities = DesiredCapabilities.chrome();27 RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);28 */29 }30}
Language: Javascript
1// Mocha and Chai.23// Assume the API endpoint is http://example.com/api4// Assume client-side validation fields are 'username' and 'password'56const chai = require('chai');7const chaiHttp = require('chai-http');8const expect = chai.expect;910chai.use(chaiHttp);1112describe('API Testing', function() {13 it('should handle client-side validation', function(done) {14 chai.request('http://example.com')15 .post('/api')16 .send({17 username: '',18 password: 'password123'19 })20 .end(function(err, res) {21 expect(res).to.have.status(400);22 expect(res.body).to.have.property('error').to.equal('Username cannot be empty');2324 done();25 });26 });27});2829// Commented code to connect to remote client with desired capabilities3031// const webdriver = require('selenium-webdriver');32// const remote = require('selenium-webdriver/remote');3334// const capabilities = webdriver.Capabilities.chrome();35// capabilities.set('chromeOptions', {36// 'args': ['--headless', '--disable-gpu']37// });3839// const driver = new webdriver.Builder()40// .withCapabilities(capabilities)41// .usingServer('http://remote-webdriver-url:4444/wd/hub')42// .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