General webpage functionality : Test drop down menu

Dropdown menu should be clickable and opens dropdown

Language: Java

Framework: Selenium 4

copy
1/​/​Assumptions: 2/​/​1. The webpage is already opened in a browser.3/​/​2. The dropdown menu is not already open.4/​/​3. The dropdown menu can be found by locating the element with the "id" attribute5/​/​ as specified in the HTML code of the webpage.67/​/​To use local driver:8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.chrome.ChromeDriver;1011System.setProperty("webdriver.chrome.driver", "path/​to/​chromedriver.exe");12/​/​path/​to/​chromedriver.exe is the location where the chromedriver executable is saved in your local machine1314WebDriver driver = new ChromeDriver();1516/​/​To connect to remote client with desired capabilities,17import org.openqa.selenium.remote.DesiredCapabilities;18import org.openqa.selenium.remote.RemoteWebDriver;1920DesiredCapabilities capabilities = DesiredCapabilities.chrome();21/​/​specify any desired capabilities as necessary2223WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);2425/​/​Test case code:26import org.openqa.selenium.By;27import org.openqa.selenium.WebElement;2829/​/​Locate the dropdown menu element30WebElement dropdownMenu = driver.findElement(By.id("dropdownmenu"));3132/​/​Click on the dropdown menu33dropdownMenu.click();3435/​/​Check if the dropdown is open36if(dropdownMenu.getAttribute("class").contains("open")){37 System.out.println("Dropdown opened successfully.");38}else{39 System.out.println("Dropdown failed to open.");40}

Language: Python

Framework: Selenium 4

copy
1Python with local driver.23# Assumptions:4# - The dropdown menu is located in the main navigation bar of the webpage5# - The dropdown menu is identified by its id "dropdown-menu"6# - Clicking on the dropdown menu will cause a dropdown to appear78from selenium import webdriver910# Create a new instance of the local browser driver11driver = webdriver.Chrome()1213# Connect to a remote browser using desired capabilities (uncomment to use)14# desired_capabilities = {"browserName": "chrome"}15# driver = webdriver.Remote(command_executor="http:/​/​selenium-hub:4444/​wd/​hub", desired_capabilities=desired_capabilities)1617# Navigate to the webpage to be tested18driver.get("http:/​/​www.example.com")1920# Find the dropdown menu using its id21dropdown_menu = driver.find_element_by_id("dropdown-menu")2223# Click on the dropdown menu to open the dropdown24dropdown_menu.click()2526# Wait for the dropdown to appear (assumes a delay of 2 seconds)27driver.implicitly_wait(2)2829# Verify that the dropdown is visible30assert dropdown_menu.is_displayed()3132# Close the browser33driver.quit()

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