API Testing : Check multi-currency support

Verify that the API correctly handles multi-currency support and returns the correct resources for each currency.

Language: Java

Framework: Rest assured

copy
1import org.testng.annotations.Test;2import static io.restassured.RestAssured.given;3import static org.hamcrest.Matchers.equalTo;45public class MultiCurrencyAPITest {6 7 @Test8 public void multiCurrencyTest() {9 10 /​/​Assuming base URL of the API11 String baseURL = "https:/​/​myapi.com";12 13 /​/​Assuming endpoint for multi-currency support14 String endpoint = "/​multi-currency";15 16 /​/​Assuming available currencies17 String[] currencies = {"USD", "EUR", "GBP"};18 19 /​/​Assuming expected resources for each currency20 String[] expectedResources = {"Resource for USD", "Resource for EUR", "Resource for GBP"};21 22 /​/​Using local driver23 given().24 baseUri(baseURL).25 basePath(endpoint).26 when().27 get().28 then().29 assertThat().30 statusCode(200).31 body("resources[0]",equalTo(expectedResources[0])).32 body("resources[1]",equalTo(expectedResources[1])).33 body("resources[2]",equalTo(expectedResources[2]));34 35 /​*36 /​/​Code to connect to remote client with desired capabilities37 DesiredCapabilities capabilities = new DesiredCapabilities();38 capabilities.setCapability("browserName", "chrome");39 WebDriver driver = new RemoteWebDriver(new URL("http:/​/​remoteclient:4444/​wd/​hub"), capabilities);40 41 /​/​Using remote driver42 given().43 baseUri(baseURL).44 basePath(endpoint).45 driver(driver).46 when().47 get().48 then().49 assertThat().50 statusCode(200).51 body("resources[0]",equalTo(expectedResources[0])).52 body("resources[1]",equalTo(expectedResources[1])).53 body("resources[2]",equalTo(expectedResources[2]));54 */​55 }56}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assumptions:4/​/​1. The API endpoint is already defined.5/​/​2. The API supports at least two currencies: USD and EUR.67const { expect } = require('chai');8const axios = require('axios');910describe('API multi-currency support test', function() {11 12 it('should return correct resources for USD currency', async function() {13 /​/​set currency to USD14 const currency = 'USD';15 16 /​/​make the API call17 const response = await axios.get(`API_ENDPOINT?currency=${currency}`);18 19 /​/​assertions20 expect(response.status).to.equal(200);21 expect(response.data.currency).to.equal(currency);22 expect(response.data.resources.length).to.be.greaterThan(0);23 });24 25 it('should return correct resources for EUR currency', async function() {26 /​/​set currency to EUR27 const currency = 'EUR';28 29 /​/​make the API call30 const response = await axios.get(`API_ENDPOINT?currency=${currency}`);31 32 /​/​assertions33 expect(response.status).to.equal(200);34 expect(response.data.currency).to.equal(currency);35 expect(response.data.resources.length).to.be.greaterThan(0);36 });37 38 /​/​this test case is assuming the API request for an unsupported currency will return a 404 error39 it('should return a 404 error for unsupported currency', async function() {40 /​/​set currency to an unsupported currency41 const currency = 'JPY';42 43 /​/​make the API call44 const response = await axios.get(`API_ENDPOINT?currency=${currency}`)45 .catch(error => {46 response = error.response;47 });48 49 /​/​assertions50 expect(response.status).to.equal(404);51 expect(response.data.error).to.exist;52 });53 54});5556/​/​Code to use local driver with Selenium 4 for Javascript57const webdriver = require('selenium-webdriver');58const { Builder } = webdriver;59const firefox = require('selenium-webdriver/​firefox');6061(async function example() {62 63 let driver = await new Builder()64 .forBrowser('firefox')65 .setFirefoxOptions(new firefox.Options()66 .headless())67 .build();68 69 try {70 await driver.get('https:/​/​www.example.com');71 /​/​perform actions on web page72 /​/​...73 } finally {74 await driver.quit();75 }76 77})();7879/​/​Commented code to connect to remote client with desired capabilities (Selenium Grid)8081/​/​ const { Builder } = require('selenium-webdriver');82/​/​ const { remote } = require('selenium-webdriver/​remote');8384/​/​ let capabilities = {85/​/​ browserName: 'firefox',86/​/​ browserVersion: '91.0',87/​/​ 'moz:firefoxOptions': {88/​/​ args: ['-headless']89/​/​ }90/​/​ };9192/​/​ let driver = new Builder()93/​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')94/​/​ .withCapabilities(capabilities)95/​/​ .build();96 97/​/​ try {98/​/​ await driver.get('https:/​/​www.example.com');99/​/​ /​/​perform actions on web page100/​/​ /​/​...101/​/​ } finally {102/​/​ await driver.quit();103/​/​ }

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