API Testing : Check currency representation

Verify that the API response contains the correct resource representation based on the specified currency.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint URL is "https:/​/​example.com/​api/​currency"2/​/​Assuming the currency parameter is passed as query parameter named "currency"3/​/​Assuming the expected currency representation is returned in "currencyRepresentation" field in the API response45import static io.restassured.RestAssured.*;6import static org.hamcrest.Matchers.*;78public class CurrencyRepresentationTest {910 @Test11 public void testCurrencyRepresentation() {12 13 /​/​Sending API request with currency parameter as USD14 given().15 param("currency", "USD").16 when().17 get("https:/​/​example.com/​api/​currency").18 then().19 statusCode(200).20 body("currencyRepresentation", equalTo("USD123.45"));21 22 /​/​Sending API request with currency parameter as EUR23 given().24 param("currency", "EUR").25 when().26 get("https:/​/​example.com/​api/​currency").27 then().28 statusCode(200).29 body("currencyRepresentation", equalTo("EUR123.45"));30 31 /​/​Assuming the API response does not contain currency representation for invalid currency32 /​/​Sending API request with currency parameter as invalid currency33 given().34 param("currency", "invalid").35 when().36 get("https:/​/​example.com/​api/​currency").37 then().38 statusCode(200).39 body("currencyRepresentation", nullValue());40 41 /​/​Assuming the above test case is expected to fail as API response may contain error message42 /​/​In that case, the code can be modified to validate error message instead of currency representation43 }44 45 @Test46 public void testCurrencyRepresentationRemote() {47 48 /​/​Assuming the remote client URL is "http:/​/​192.168.1.100:4444/​wd/​hub"49 /​/​Assuming the desired capabilities include Chrome browser and Windows 10 OS50 DesiredCapabilities capabilities = new DesiredCapabilities();51 capabilities.setBrowserName("chrome");52 capabilities.setPlatform(Platform.WINDOWS);53 WebDriver driver = new RemoteWebDriver(new URL("http:/​/​192.168.1.100:4444/​wd/​hub"), capabilities);54 /​/​Rest of the test code goes here55 56 }57 58}

Language: Javascript

copy
1/​/​ Mocha + Chai + Supertest.23/​/​Assumptions4/​/​1. The API endpoint is available and functional5/​/​2. The response returns JSON data containing currency information67const request = require('supertest');8const chai = require('chai');9const expect = chai.expect;1011describe('API testing for currency representation', () => {12 it('should return the correct resource representation based on the specified currency', (done) => {1314 /​/​Assumptions15 /​/​1. The currency parameter is valid and supported by the API16 /​/​2. The API response contains a JSON object with a 'currency' property17 18 const currency = 'USD'; /​/​Assuming currency to be tested is `USD`19 20 request('http:/​/​localhost:3000')21 .get(`/​api/​currency?currency=${currency}`)22 .end((err, res) => {23 if (err) return done(err);2425 /​/​Asserting that the response contains the expected `currency` property26 expect(res.body).to.haveOwnProperty('currency');27 28 /​/​Asserting that the value of the `currency` property is the expected currency29 expect(res.body.currency).to.be.equal(currency);30 done();31 });32 });33});3435/​/​Code to connect to remote client with desired capabilities36/​/​Assuming a remote Selenium client with Firefox browser3738const { Builder } = require('selenium-webdriver');39const { remote } = require('selenium-webdriver/​remote');40const firefox = require('selenium-webdriver/​firefox');4142const capabilities = {43 browserName: 'firefox',44 platformName: 'Windows 10',45 'moz:firefoxOptions': {46 args: ['--headless'] /​/​Assuming Firefox in headless mode47 },48};4950const browserOptions = new firefox.Options();51browserOptions.setPreference('intl.accept_languages', 'en-US,en');5253const driver = new Builder()54 .usingServer('http:/​/​REMOTE_HOST:REMOTE_PORT/​wd/​hub')55 .withCapabilities(capabilities)56 .setFirefoxOptions(browserOptions)57 .build(); 5859/​/​Assuming `http:/​/​REMOTE_HOST:REMOTE_PORT/​wd/​hub` is the remote Selenium server endpoint.

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