API Testing : Check alphanumeric search

Verify that the API returns the correct results when searching for a string with a combination of letters and numbers.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming that the API endpoint for the search is available at "https:/​/​example.com/​search"23import org.testng.annotations.Test;4import static io.restassured.RestAssured.*;56public class API_Test {78 @Test9 public void alphanumericSearchTest() {10 /​/​Assuming that the search query "r2d2c3po" is a valid alphanumeric search query11 String searchQuery = "r2d2c3po";12 13 /​/​Using Rest Assured to send the GET request and verify the response14 given()15 .when()16 .get("https:/​/​example.com/​search?q=" + searchQuery)17 .then()18 .statusCode(200)19 .body("results", hasSize(1))20 .body("results[0].name", equalTo("R2D2-C3PO"));21 22 /​/​Assuming that the API can also be tested using a remote client with desired capabilities23 /​/​DesiredCapabilities capabilities = new DesiredCapabilities();24 /​/​capabilities.setCapability("platformName", "Android");25 /​/​capabilities.setCapability("platformVersion", "10.0");26 /​/​capabilities.setCapability("deviceName", "Pixel 3a XL");27 /​/​WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4723/​wd/​hub"), capabilities);28 /​/​RestAssured.defaultParser = Parser.JSON;29 /​/​given().get("https:/​/​example.com/​search?q=" + searchQuery).then().statusCode(200);30 /​/​driver.quit();31 }32}

Language: Javascript

copy
1/​/​ Mocha and Chai.23const { expect } = require('chai');4const request = require('supertest');56describe('API Testing', () => {7 /​/​Assuming the API endpoint is http:/​/​example.com/​search8 const url = 'http:/​/​example.com/​search';910 it('should return correct results for alphanumeric search', async () => {11 const query = 'test123';12 const response = await request(url).get(`?q=${query}`);13 expect(response.status).to.equal(200);14 expect(response.body).to.include(query);15 });16});1718/​/​To use local driver in JavaScript using Selenium, use the following commented code:1920const { Builder, By, Key, until } = require('selenium-webdriver');21const chrome = require('selenium-webdriver/​chrome');2223(async function example() {24 const options = new chrome.Options();25 options.addArguments('--disable-gpu');26 options.addArguments('--no-sandbox');27 options.addArguments('--disable-dev-shm-usage');2829 const driver = await new Builder()30 .forBrowser('chrome')31 .setChromeOptions(options)32 .build();3334 try {35 /​/​Assuming the URL to be tested is http:/​/​example.com/​login36 await driver.get('http:/​/​example.com/​login');3738 const loginBox = await driver.findElement(By.name('username'));39 const passwordBox = await driver.findElement(By.name('password'));40 const loginButton = await driver.findElement(By.id('login-button'));4142 /​/​Assuming the username and password to be tested are 'TestUser' and 'TestPass'43 await loginBox.sendKeys('TestUser', Key.TAB);44 await passwordBox.sendKeys('TestPass', Key.RETURN);4546 await driver.wait(until.urlIs('http:/​/​example.com/​dashboard'), 5000);4748 /​/​Add further verification steps as per requirements49 } finally {50 await driver.quit();51 }52})5354/​/​To connect to remote client with desired capabilities, use the following commented code:5556const { Builder, Capabilities } = require('selenium-webdriver');5758(async function example() {59 const capabilities = Capabilities.chrome();6061 /​/​Assuming the remote client is running on 127.0.0.1:444462 const driver = await new Builder()63 .usingServer('http:/​/​127.0.0.1:4444/​wd/​hub')64 .withCapabilities(capabilities)65 .build();6667 try {68 /​/​Assuming the URL to be tested is http:/​/​example.com/​69 await driver.get('http:/​/​example.com/​');7071 /​/​Add test steps as per requirements72 } finally {73 await driver.quit();74 }75})();

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.

Accelerate Your Automation Test Cycles With LambdaTest

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.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing