Drop-down field values should be displayed in defined sort order.
Language: Java
Framework: Selenium 4
1//Assumptions:2//1. The desired drop-down field is loaded on the webpage and is identifiable by it's unique identifier or class name.3//2. The defined sort order is either ascending or descending, and can be identified by comparing the values in the drop-down field before and after sorting.45//Code to implement the test case in Selenium 4 Java:67import org.openqa.selenium.By;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebElement;10import org.openqa.selenium.chrome.ChromeDriver;11import org.openqa.selenium.support.ui.Select;1213public class TestDropdownSortOrder {14 public static void main(String[] args) {15 //Assumption: ChromeDriver is installed on the local machine16 System.setProperty("webdriver.chrome.driver", "path\\to\\chromedriver.exe");17 WebDriver driver = new ChromeDriver();18 19 //Assumption: The webpage is loaded and can be accessed using the driver20 driver.get("https://example.com");21 //Assumption: The drop-down field is identifiable by it's id="sort_order"22 WebElement dropdown = driver.findElement(By.id("sort_order"));23 //Assumption: The values in the drop-down field are expected to be in ascending order by default24 Select select = new Select(dropdown);25 List<WebElement> optionsList = select.getOptions();26 27 //Assumption: The values in the drop-down field are expected to be in ascending order by default 28 for(int i = 1; i < optionsList.size(); i++) {29 WebElement currentOption = optionsList.get(i);30 WebElement prevOption = optionsList.get(i - 1);31 assert(prevOption.getText().compareTo(currentOption.getText()) <= 0);32 }33 34 // Sort in descending order and re-check the order of the values in the drop-down field35 select.selectByVisibleText("Descending");36 List<WebElement> optionsListDesc = select.getOptions();37 for(int i = 1; i < optionsListDesc.size(); i++) {38 WebElement currentOption = optionsListDesc.get(i);39 WebElement prevOption = optionsListDesc.get(i - 1);40 assert(currentOption.getText().compareTo(prevOption.getText()) <= 0);41 }42 43 //Assumption: The driver can be connected to a remote client with desired capabilities using Selenium Grid or similar technology44 //Code to connect to a remote client with desired capabilities (assumed to be Firefox browser)45 /*46 DesiredCapabilities capabilities = new DesiredCapabilities();47 capabilities.setBrowserName("firefox");48 capabilities.setPlatform(Platform.WINDOWS);49 WebDriver driverRemote = new RemoteWebDriver(new URL("http://remote_machine_IP:4444/wd/hub"), capabilities);50 */51 }52}
Language: Python
Framework: Selenium 4
1# Assuming the website has a drop-down field and its values are stored in a list named 'dropdown_values'23from selenium import webdriver4from selenium.webdriver.common.keys import Keys56# Local driver initialization7driver = webdriver.Chrome() 89# Remote client initialization with desired capabilities10# from selenium.webdriver.common.desired_capabilities import DesiredCapabilities11# driver = webdriver.Remote(12# command_executor='http://<REMOTE_IP>:4444/wd/hub',13# desired_capabilities=DesiredCapabilities.CHROME)1415# Open the website URL16driver.get("http://<WEBSITE_URL>")1718# Click on the drop-down field19dropdown= driver.find_element_by_id("<DROPDOWN_ID>")20dropdown.click()2122# Get the sorted values of the drop-down field23sorted_dropdown_values = sorted(dropdown_values)2425# Get the actual values of the drop-down field26actual_dropdown_values = []27for option in dropdown.find_elements_by_tag_name('option'):28 actual_dropdown_values.append(option.text)2930# Comparing the actual and sorted values of the drop-down field31assert sorted_dropdown_values == actual_dropdown_values, "The drop-down field values are not displayed in defined sort order."3233# Close the browser window34driver.quit()
Language: Javascript
Framework: Cypress
1// Assumptions:2// 1. The webpage under test has a drop-down field.3// 2. The sort order of the drop-down field is predefined.45// Import necessary Cypress libraries6import { Selector } from 'cypress';78// Test case implementation9describe('Test drop-down field sort order', () => {10 it('should display drop-down field values in defined sort order', () => {11 // Navigate to webpage under test12 cy.visit('https://www.example.com');1314 // Get the drop-down field15 const dropdown = Selector('select[name="options"]');1617 // Get all the options in the drop-down field18 cy.get(dropdown).find('option').then((options) => {19 // Check that the options are sorted in ascending order20 for (let i = 0; i < options.length - 1; i++) {21 expect(options[i].text).to.be.lessThan(options[i+1].text);22 }23 });24 });25}); 2627// Example code to connect to remote client with desired capabilities28/*29import { browser } from 'protractor';3031const capabilities = {32 'browserName': 'chrome',33 'version': '91.0',34 'platform': 'WINDOWS',35 'chromeOptions': {36 'args': ['disable-infobars']37 }38}3940const remoteServer = 'http://localhost:4444/wd/hub';4142describe('Test scenario: General webpage functionality', () => {43 it('should do something', () => {44 return browser.driver45 .getCapabilities()46 .then((caps) => {47 console.log('Current capabilities: ', caps);48 return browser.driver.getSession();49 })50 .then((sess) => {51 console.log('Current session ID: ', sess.getId());52 return browser.get('https://example.com');53 })54 .then(() => {55 console.log('Page title: ', browser.getTitle());56 // Do something here...57 });58 });59});6061browser.getProcessedConfig().then((config) => {62 console.log('Current configuration: ', config);63 return browser.driver.get(remoteServer);64})65.then(() => browser.driver.getSession().then((sess) => {66 console.log('Remote session ID: ', sess.getId());67 return browser.driver.manage().addCookie({name: 'COOKIE_NAME', value: 'COOKIE_VALUE'});68}))69.then(() => browser.driver.getCapabilities().then((caps) => {70 console.log('Remote capabilities: ', caps);71})).catch((err) => console.error(err));72*/
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