Verify that the API response contains the correct resource representation based on the specified locale (e.g. en-US, fr-FR).
Language: Java
Framework: Rest assured
1//Assumptions:2//1. API endpoint has been established and its URL is 'http://example.com/api'3//2. Required dependencies and libraries for Rest assured have been imported.45import static io.restassured.RestAssured.*;6import static org.hamcrest.Matchers.*;78public class LocaleRepresentationTest {910 @Test11 public void testLocaleRepresentation() {12 13 //Set locale14 given()15 .param("locale","en-US")16 .when()17 .get("http://example.com/api")18 .then()19 .statusCode(200)20 .body("resourceRepresentation", equalTo("en-US"));21 22 //Set locale23 given()24 .param("locale","fr-FR")25 .when()26 .get("http://example.com/api")27 .then()28 .statusCode(200)29 .body("resourceRepresentation", equalTo("fr-FR"));30 31 //Connect to remote client32 //DesiredCapabilities caps = new DesiredCapabilities();33 //caps.setBrowserName("chrome");34 //caps.setPlatform(Platform.LINUX);35 //WebDriver driver = new RemoteWebDriver(new URL(remoteUrl), caps);36 //Use driver for API testing37 }38}
Language: Javascript
1// Mocha and Chai.23//Assumptions4//1. The API endpoint is known and the response code is 200 for the expected request.5//2. The locale code is passed as a query parameter named 'locale'.6//3. The expected resource representation for the locale is defined in a JSON file with locale codes as keys.78const request = require('request');9const expect = require('chai').expect;10const resourceData = require('./resource_data.json');1112describe('API Testing: Locale-based representation', () => {13 it('should return the correct resource representation for the specified locale', (done) => {14 //Assume an endpoint like https://api.example.com/resources15 const endpoint = 'https://api.example.com/resources';16 //Assume locale to be tested is 'en-US'17 const locale = 'en-US';18 //Assume request parameters and headers19 const params = {20 url: `${endpoint}?locale=${locale}`,21 headers: {22 'Content-Type': 'application/json'23 }24 };25 //Assume response to be JSON with a 'locale' property26 request.get(params, (error, response, body) => {27 const responseBody = JSON.parse(body);28 expect(response.statusCode).to.equal(200);29 expect(responseBody).to.have.property('locale').equal(locale);30 expect(responseBody).to.deep.equal(resourceData[locale]);31 done();32 });33 });34});3536//Connecting to remote client with desired capabilities37//Assume the remote client is using Chrome browser with desired capabilities of version 89.038const assert = require('chai').assert;39const {Builder, By, until} = require('selenium-webdriver');40const chrome = require('selenium-webdriver/chrome');41const chromedriver = require('chromedriver');4243describe('API Testing: Locale-based representation', () => {44 it('should return the correct resource representation for the specified locale using remote client', async () => {45 //Assume locale to be tested is 'fr-FR'46 const locale = 'fr-FR';47 //Assume URL of the web application48 const url = 'https://example.com';49 //Chrome options for the remote client50 const options = new chrome.Options().setChromeBinaryPath('path/to/chrome').setChromeVersion('89.0').addArguments('--headless');51 //Assuming the remote client is running on selenium grid with URL 'http://grid.example.com:4444/wd/hub'52 const driver = await new Builder().forBrowser('chrome').usingServer('http://grid.example.com:4444/wd/hub').withCapabilities(options.toCapabilities()).build();53 try {54 await driver.get(url);55 const langSelect = await driver.findElement(By.id('lang_select'));56 await langSelect.click();57 const selOption = await driver.findElement(By.xpath(`//option[@value="${locale}"]`));58 await selOption.click();59 const applyBtn = await driver.findElement(By.id('apply_btn'));60 await applyBtn.click();61 await driver.wait(until.elementLocated(By.id('resource_display')), 10000);62 const resourceDisplay = await driver.findElement(By.id('resource_display'));63 const resourceText = await resourceDisplay.getText();64 assert.strictEqual(resourceText, resourceData[locale].description);65 } finally {66 await driver.quit();67 }68 }).timeout(30000);//Assuming the wait time for each step of a test case is 10 seconds. So setting timeout to 30 seconds for this scenario.69});
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