Check if the drop-down list options are readable and not truncated due to field size limits.
Language: Java
Framework: Selenium 4
1//Assumptions:2//1. The web page has a drop-down list field.3//2. The options in the drop-down list are loaded dynamically.4//3. The field does not have any scroll option.5//4. The dropdown xpath or css locator will be provided.67//Imports8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebElement;10import org.openqa.selenium.By;11import org.openqa.selenium.support.ui.Select;1213//Test case code14public void testDropDownListVisibility(WebDriver driver, String dropDownListLocator) {15 WebElement dropDownList = driver.findElement(By.xpath(dropDownListLocator));16 Select select = new Select(dropDownList);17 18 //Verify if options are readable and not truncated due to field size limits19 boolean isTruncated = false;20 for (WebElement option : select.getOptions()) {21 if (option.getText().length() > select.getFirstSelectedOption().getText().length()) {22 isTruncated = true;23 break;24 }25 }26 27 //Assert28 assertTrue(!isTruncated);29}3031//Code to connect to remote client with desired capabilities32public void connectToRemoteClient() {33 //Assuming the remote client is a browserstack instance34 DesiredCapabilities caps = new DesiredCapabilities();35 caps.setCapability("os", "Windows");36 caps.setCapability("os_version", "10");37 caps.setCapability("browser", "Chrome");38 caps.setCapability("browser_version", "latest");39 40 WebDriver driver = new RemoteWebDriver(new URL("https://hub-cloud.browserstack.com/wd/hub"), caps);41 driver.manage().window().maximize(); //Maximizing the window42}
Language: Python
Framework: Selenium 4
1#Assumptions: 2#1. The dropdown list is implemented using HTML <select> tag 3#2. The dropdown list is present on the homepage of the website 4#3. The webpage is already loaded and driver is initialized56#Code to implement test case in Selenium using Python78#Import necessary libraries9from selenium.webdriver.common.by import By10from selenium.webdriver.support.ui import Select11from selenium.webdriver.support import expected_conditions as EC12from selenium.webdriver.support.ui import WebDriverWait13from selenium.webdriver.common.keys import Keys14from selenium.webdriver.common.desired_capabilities import DesiredCapabilities15from selenium import webdriver1617#Code to use local driver18#Uncomment this code to run tests on local machine19#driver = webdriver.Chrome()2021#Code to connect to remote client with desired capabilities22#Uncomment this code to run tests on remote client23#DesiredCapabilities.CHROME['platform'] = 'WINDOWS'24#driver = webdriver.Remote(command_executor='http://192.168.1.100:4444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME)2526#Navigate to homepage of website27driver.get("https://www.example.com")2829#Wait for dropdown list to be visible30wait = WebDriverWait(driver, 10)31dropdown_element = wait.until(EC.visibility_of_element_located((By.XPATH, "//select[@id='my_dropdown_list']")))3233#Create Select object for dropdown list34dropdown = Select(dropdown_element)3536#Get all options of dropdown list37options = dropdown.options3839#Iterate over options to check visibility of each option40for option in options:41 assert option.is_displayed() == True, "Option is not visible"42 43#Print success message if all options are visible44print("All dropdown list options are visible")
Language: Javascript
Framework: Cypress
1//Assuming the webpage is already loaded and the dropdown list is present on the page2describe('General webpage functionality testing', () => {3 it('Verify drop-down list options visibility', () => {4 cy.get('#dropdownList').click(); //select the dropdown list element5 cy.get('#dropdownListOptions').should('be.visible'); //check if the options are visible6 cy.get('#dropdownListOptions').each(($option) => { //check each option7 expect($option.text().length).to.be.greaterThan(0); //check if the option text is not empty8 expect($option.css('overflow')).to.equal('visible'); //check if the option text is not truncated9 });10 });11});1213//To connect to remote client with desired capabilities, use the following code:14describe('General webpage functionality testing', () => {15 it('Verify drop-down list options visibility', () => {16 const options = { //desired capabilities17 browserName: 'chrome',18 platformName: 'Windows 10',19 //add more capabilities if needed20 };21 cy.remote(options).then((browser) => { //connect to remote client22 browser.url('https://example.com'); //navigate to webpage23 //add test case code here24 browser.deleteSession(); //close remote session25 });26 });27});
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