Verify that the API correctly handles SR tokens and returns the correct HTTP status code.
Language: Java
Framework: Rest assured
1//Assuming that the API endpoint is already available2//Assuming that we have valid SR tokens3//Assuming that the API will return HTTP status code 200 for valid SR tokens and 401 for invalid ones45import static io.restassured.RestAssured.*;67import org.testng.annotations.Test;89public class SRTokenHandlingTest {10 11 @Test12 public void testSRTokenHandling() {13 String srToken = "valid_SR_token"; //assuming we have a valid SR token14 given().15 header("sr-token", srToken). //set SR token header16 when().17 get("https://example.com/api/endpoint"). //send GET request to API endpoint18 then().19 assertThat().statusCode(200); //verify that HTTP status code is 20020 }21 22 @Test23 public void testInvalidSRTokenHandling() {24 String srToken = "invalid_SR_token"; //assuming we have an invalid SR token25 given().26 header("sr-token", srToken). //set SR token header27 when().28 get("https://example.com/api/endpoint"). //send GET request to API endpoint29 then().30 assertThat().statusCode(401); //verify that HTTP status code is 40131 }32 33 //Code to connect to remote client with desired capabilities34 //Assuming that the remote client URL is "https://example.com/wd/hub" and desired capability is Chrome browser35 public void testSRTokenHandlingOnRemoteClient() {36 DesiredCapabilities capabilities = DesiredCapabilities.chrome(); //desired capability is Chrome browser37 WebDriver driver = new RemoteWebDriver(new URL("https://example.com/wd/hub"), capabilities); //connect to remote client38 driver.get("https://example.com/api/endpoint"); //navigate to API endpoint39 String srToken = "valid_SR_token"; //assuming we have a valid SR token40 driver.findElement(By.xpath("//input[@name='sr-token']")).sendKeys(srToken); //set SR token field41 WebElement submitBtn = driver.findElement(By.xpath("//button[@type='submit']")); //find submit button element42 submitBtn.click(); //click submit button43 String response = driver.findElement(By.xpath("//div[@class='response']")).getText(); //get API response44 Assert.assertEquals(response, "API response"); //assert API response45 driver.quit(); //close browser session46 }47}
Language: Javascript
1// Mocha and Chai.23//Assuming the API endpoint for SR token handling is https://example.com/sr-token4const axios = require('axios');5const { expect } = require('chai');67describe('SR token handling', () => {8 it('should return the correct HTTP status code', async () => {9 //Assuming a valid SR token is 'abcd1234'10 const response = await axios.get('https://example.com/sr-token?token=abcd1234');11 expect(response.status).to.equal(200);12 });1314 it('should handle invalid SR token', async () => {15 //Assuming an invalid SR token is 'invalid'16 const response = await axios.get('https://example.com/sr-token?token=invalid');17 expect(response.status).to.equal(400);18 });1920 //Uncomment the following code to connect to remote client with desired capabilities.21 // const webdriver = require('selenium-webdriver');22 // const capabilities = {23 // browserName: 'chrome',24 // version: 'latest',25 // platform: 'Windows 10'26 // };27 // const remoteAddress = 'http://localhost:4444/wd/hub';2829 // const driver = new webdriver.Builder()30 // .withCapabilities(capabilities)31 // .usingServer(remoteAddress)32 // .build();33});
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