API Testing : Check time zone representation

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

Language: Java

Framework: Rest assured

copy
1/​/​ Assuming that the API endpoint is accessible and the time zones are passed as query parameters2/​/​ Also assuming that the expected response will contain the time zone representation in the specified format3/​/​ Using Rest assured framework for API testing45import static io.restassured.RestAssured.*;6import static org.hamcrest.Matchers.*;78public class APITest {9 @Test10 public void testTimeZoneRepresentation() {11 /​/​ API endpoint URL12 String endpoint = "https:/​/​api.example.com/​endpoint";1314 /​/​ Query parameters15 String timeZoneParam = "timeZone=Australia/​Sydney";1617 given()18 .queryParam(timeZoneParam)19 .when()20 .get(endpoint)21 .then()22 .statusCode(200)23 .body("timeZoneRepresentation", equalTo("GMT+10:00")) /​/​ Assuming this is the expected format24 .log().all(); /​/​ Logging the response details for debugging purposes2526 /​/​ To connect to remote client with desired capabilities, add the following code:27 /​*28 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), DesiredCapabilities.chrome());29 driver.get(endpoint + "?" + timeZoneParam);30 String response = driver.getPageSource();31 /​/​ Assert and validate response as required32 driver.quit();33 */​34 }35}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​ Assumptions:4/​/​ 1. The API endpoint is already known and accessible.5/​/​ 2. The API response contains a timestamp in ISO format.6/​/​ 3. The time zone can be specified in the API request.78const assert = require('chai').assert;9const axios = require('axios');1011const apiUrl = 'https:/​/​example.com/​api';12const timeZone = 'UTC';1314describe('API Testing', function() {15 it('should check time zone representation', async function() {16 /​/​ Make API request to get response.17 const response = await axios.get(apiUrl, { 18 params: { 19 timeZone: timeZone 20 } 21 });2223 /​/​ Parse timestamp from response.24 const timestamp = new Date(response.data.timestamp);2526 /​/​ Check if timestamp is in correct timezone.27 assert.equal(timestamp.getTimezoneOffset(), 0, 'Incorrect time zone representation.');28 });29});3031/​/​ Uncomment this block to run the test on a remote client with desired capabilities.3233/​/​ const webdriver = require('selenium-webdriver');34/​/​ const capabilities = {35/​/​ browserName: 'chrome',36/​/​ platform: 'windows',37/​/​ version: 'latest'38/​/​ };3940/​/​ const driver = new webdriver.Builder()41/​/​ .withCapabilities(capabilities)42/​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')43/​/​ .build();4445/​/​ describe('API Testing', function() {46/​/​ before(async function() {47/​/​ await driver.get(apiUrl);48/​/​ });4950/​/​ after(async function() {51/​/​ await driver.quit();52/​/​ });5354/​/​ it('should check time zone representation', async function() {55/​/​ /​/​ Find and interact with time zone input.56/​/​ const timeZoneInput = await driver.findElement(webdriver.By.css('[name="timeZone"]'));57/​/​ await timeZoneInput.sendKeys(timeZone);5859/​/​ /​/​ Find and click submit button.60/​/​ const submitButton = await driver.findElement(webdriver.By.css('[type="submit"]'));61/​/​ await submitButton.click();6263/​/​ /​/​ Find and verify timestamp element.64/​/​ const timestampElement = await driver.findElement(webdriver.By.css('.timestamp'));65/​/​ const timestampText = await timestampElement.getText();66/​/​ const timestamp = new Date(timestampText);6768/​/​ assert.equal(timestamp.getTimezoneOffset(), 0, 'Incorrect time zone representation.');69/​/​ });70/​/​ });

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