Disabled fields should be greyed out and users should not be able to set focus on these fields.
Language: Java
Framework: Selenium 4
1//Assumptions: The webpage being tested has disabled fields on it.2//The driver has already been instantiated34//Connect to a remote client with desired capabilities5/*6DesiredCapabilities desiredCapabilities = new DesiredCapabilities();7desiredCapabilities.setPlatform(Platform.YOUR_PLATFORM);8desiredCapabilities.setBrowserName("YOUR_BROWSER");9desiredCapabilities.setVersion("YOUR_BROWSER_VERSION");10driver = new RemoteWebDriver(new URL("YOUR_REMOTE_SERVER"), desiredCapabilities);11*/1213//Locate the disabled field14WebElement disabledField = driver.findElement(By.xpath("//input[@disabled='disabled']"));1516//Check that the field appears greyed out17String backgroundColor = disabledField.getCssValue("background-color");18String expectedBackgroundColor = "rgb(217, 217, 217)";19Assert.assertEquals(backgroundColor, expectedBackgroundColor, "The disabled field does not appear greyed out.");2021//Attempt to set focus on the disabled field22try {23 disabledField.click();24 Assert.fail("The disabled field should not be clickable.");25} catch (Exception e) {26 //Expected exception, do nothing27}
Language: Python
Framework: Selenium 4
1#Assuming we are testing a web application 2#Assuming the driver is already set up and imported 34#navigate to the webpage 5driver.get('https://www.example.com')67#Finding the elements on the webpage 8disabled_field = driver.find_element_by_xpath('//input[@disabled]')910#Asserting if the disabled field is greyed out 11assert disabled_field.value_of_css_property('background-color') == 'rgba(0, 0, 0, 0)'1213#Asserting if user is not able to set focus on the disabled field 14assert not disabled_field.is_enabled()1516#To use remote client with desired capabilities 17#uncomment below code and replace the desired capabilities values as per requirement 1819# from selenium import webdriver20# from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 2122# capabilities = DesiredCapabilities.CHROME.copy()23# capabilities['acceptInsecureCerts'] = True #add or remove capabilities as per requirement 2425# driver = webdriver.Remote(26# command_executor='http://127.0.0.1:4444/wd/hub',27# desired_capabilities=capabilities) #add the remote client hub URL 2829#navigate to the webpage 30# driver.get('https://www.example.com')
Language: Javascript
Framework: Cypress
1//Assuming the webpage is a simple HTML form with disabled fields23describe('General webpage functionality', () => {4 it('Check disabled field appearance', () => {5 cy.visit('https://example.com')6 7 //Identifying disabled fields and verifying their CSS properties8 cy.get('input:disabled').should('have.css', 'color', 'rgb(128, 128, 128)') //grey color9 .should('have.css', 'background-color', 'rgb(248, 248, 248)') //light grey background10 .should('have.attr', 'tabindex', '-1') //unable to set focus11 })12})1314//Connect to remote client with Desired Capabilities15const desiredCapabilities = {16 browserName: 'chrome',17 browserVersion: '90',18 platformName: 'Windows 10',19 'sauce:options': {20 username: 'YOUR_SAUCE_USERNAME',21 accessKey: 'YOUR_SAUCE_ACCESS_KEY',22 tunnelIdentifier: 'YOUR_TUNNEL_IDENTIFIER'23 }24}2526describe('General webpage functionality', () => {27 it('Check disabled field appearance', () => {28 cy.visit('https://example.com', {29 desiredCapabilities30 })31 32 cy.get('input:disabled').should('have.css', 'color', 'rgb(128, 128, 128)')33 .should('have.css', 'background-color', 'rgb(248, 248, 248)')34 .should('have.attr', 'tabindex', '-1')35 })36})
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