General webpage functionality : Check error highlighting

Check if the correct fields are highlighted in case of errors.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming the website URL is "https:/​/​example.com"2/​/​Assuming the web page contains a form with "username" and "password" fields3/​/​This code uses Selenium 4 with Java4import org.openqa.selenium.By;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.chrome.ChromeDriver;8import org.openqa.selenium.remote.DesiredCapabilities;9import org.openqa.selenium.remote.RemoteWebDriver;10import java.net.URL;1112public class ErrorHighlightingTest {13 public static void main(String[] args) {14 15 /​/​Connecting to local driver16 System.setProperty("webdriver.chrome.driver", "/​path/​to/​chromedriver");17 WebDriver driver = new ChromeDriver();18 19 /​/​Connecting to remote driver20 /​*DesiredCapabilities capabilities = new DesiredCapabilities();21 capabilities.setCapability("browserName", "chrome");22 RemoteWebDriver driver = null;23 try {24 driver = new RemoteWebDriver(new URL("http:/​/​127.0.0.1:4444/​wd/​hub"), capabilities);25 } catch (Exception e) {26 System.out.println("Could not connect to the remote client");27 e.printStackTrace();28 }*/​29 30 /​/​Open website31 driver.get("https:/​/​example.com");32 33 /​/​Assuming the error message is displayed in a pop-up when the form is submitted34 /​/​Assuming the error message is as follows35 String expectedErrorMsg = "Invalid username or password";36 37 /​/​Fill the form with valid values and submit to trigger the error38 WebElement username = driver.findElement(By.id("username"));39 WebElement password = driver.findElement(By.id("password"));40 WebElement submitBtn = driver.findElement(By.id("submit"));41 username.sendKeys("validname");42 password.sendKeys("validpass");43 submitBtn.click();44 45 /​/​Check if the error message contains the expected text46 WebElement errorMsg = driver.findElement(By.className("error-message"));47 if(errorMsg.getText().contains(expectedErrorMsg)){48 System.out.println("Error correctly highlighted");49 }50 else{51 System.out.println("Error highlighting incorrect");52 }53 54 /​/​Close the driver55 driver.quit();56 }57}

Language: Python

Framework: Selenium 4

copy
1# Assuming the webpage is open and the driver is initialized23# Test case code to check error highlighting on webpage functionality4def test_error_highlighting():5 # Locating the form field elements6 username_field = driver.find_element_by_name('username')7 password_field = driver.find_element_by_name('password')8 login_button = driver.find_element_by_xpath('/​/​button[@type="submit"]')910 # Entering invalid credentials and clicking login button11 username_field.send_keys('invalid_username')12 password_field.send_keys('invalid_password')13 login_button.click()1415 # Adding a delay to allow page to load completely16 time.sleep(5)1718 # Checking if the correct fields are highlighted in case of errors19 assert 'error' in username_field.get_attribute('class')20 assert 'error' in password_field.get_attribute('class')2122# Code to connect to remote client with desired capabilities23from selenium.webdriver.common.desired_capabilities import DesiredCapabilities2425desired_cap = {26 'browser': 'Chrome',27 'browser_version': '92.0',28 'os': 'Windows',29 'os_version': '10',30 'resolution': '1920x1080',31 'name': 'Test error highlighting on webpage functionality'32}3334driver = webdriver.Remote(35 command_executor='http:/​/​localhost:4444/​wd/​hub',36 desired_capabilities=desired_cap37)

Language: Javascript

Framework: Cypress

copy
1/​/​ Assumptions:2/​/​ 1. The webpage is accessible via URL3/​/​ 2. The webpage contains input fields4/​/​ 3. The webpage contains validation errors56/​/​ Code for local driver using Cypress78describe('Webpage functionality', function() {9 it('Should check error highlighting', function() {10 cy.visit('https:/​/​www.example.com') /​/​ replace with URL of webpage to be tested11 cy.get('#username-input') /​/​ replace with ID of username input field12 .type('invalidusername')13 cy.get('#password-input') /​/​ replace with ID of password input field14 .type('invalidpassword')15 cy.get('#submit-button') /​/​ replace with ID of submit button16 .click()17 cy.get('#username-input') /​/​ replace with ID of username input field18 .should('have.class', 'error') /​/​ replace with class name of error highlighting19 cy.get('#password-input') /​/​ replace with ID of password input field20 .should('have.class', 'error') /​/​ replace with class name of error highlighting21 })22})2324/​/​ Code for remote client using desired capabilities2526describe('Webpage functionality', function() {27 it('Should check error highlighting', function() {28 cy.visit('https:/​/​www.example.com', {29 browser: 'chrome', /​/​ replace with browser desired capability30 platform: 'Windows 10', /​/​ replace with platform desired capability31 version: 'latest', /​/​ replace with browser version desired capability32 })33 cy.get('#username-input') /​/​ replace with ID of username input field34 .type('invalidusername')35 cy.get('#password-input') /​/​ replace with ID of password input field36 .type('invalidpassword')37 cy.get('#submit-button') /​/​ replace with ID of submit button38 .click()39 cy.get('#username-input') /​/​ replace with ID of username input field40 .should('have.class', 'error') /​/​ replace with class name of error highlighting41 cy.get('#password-input') /​/​ replace with ID of password input field42 .should('have.class', 'error') /​/​ replace with class name of error highlighting43 cy.quit() /​/​ quit browser after test44 })45})

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