General webpage functionality : Verify error message field labels

Check if proper field labels are being used in error messages.

Language: Java

Framework: Selenium 4

copy
1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;56public class VerifyErrorMessageFieldLabels {7 public static void main(String[] args) {8 /​/​Assuming the environment already has the latest version of ChromeDriver9 WebDriver driver = new ChromeDriver();10 driver.get("https:/​/​example.com"); /​/​Assuming the website is example.com11 12 /​/​Find the login button13 WebElement loginButton = driver.findElement(By.id("loginButton"));14 15 /​/​Click the login button to open the login page16 loginButton.click();17 18 /​/​Assuming the login page has field labels for username and password19 /​/​ Find the username and password input fields20 WebElement usernameField = driver.findElement(By.name("username"));21 WebElement passwordField = driver.findElement(By.name("password"));22 23 /​/​Enter invalid credentials and click the login button24 usernameField.sendKeys("invaliduser");25 passwordField.sendKeys("invalidpassword");26 loginButton.click();27 28 /​/​Assuming the error message for invalid credentials is displayed 29 /​/​ Find the label for the error message associated with the username field30 WebElement usernameErrorLabel = driver.findElement(By.xpath("/​/​label[@for='username'][contains(@class,'error')]"));31 32 /​/​Verify that the username error label contains proper field label text33 String expectedUsernameLabel = "Username:";34 String actualUsernameLabel = usernameErrorLabel.getText().trim();35 if (expectedUsernameLabel.equals(actualUsernameLabel)) {36 System.out.println("PASS: Proper field label is used in the error message for the username field");37 } else {38 System.out.println("FAIL: Improper field label is used in the error message for the username field");39 }40 41 /​/​Assuming the error message for invalid credentials is displayed 42 /​/​ Find the label for the error message associated with the password field43 WebElement passwordErrorLabel = driver.findElement(By.xpath("/​/​label[@for='password'][contains(@class,'error')]"));44 45 /​/​Verify that the password error label contains proper field label text46 String expectedPasswordLabel = "Password:";47 String actualPasswordLabel = passwordErrorLabel.getText().trim();48 if (expectedPasswordLabel.equals(actualPasswordLabel)) {49 System.out.println("PASS: Proper field label is used in the error message for the password field");50 } else {51 System.out.println("FAIL: Improper field label is used in the error message for the password field");52 }53 54 /​/​ Assuming there is a remote client with the desired capabilities55 /​/​Add the following commented code to connect to remote client with desired capabilities56 /​*57 ChromeOptions options = new ChromeOptions();58 options.setCapability(CapabilityType.PLATFORM_NAME, Platform.WINDOWS);59 options.setCapability(CapabilityType.BROWSER_NAME, "chrome");60 RemoteWebDriver driver = new RemoteWebDriver(new URL("<remote client URL>"), options);61 driver.manage().window().maximize();62 */​63 driver.quit();64 }65}

Language: Python

Framework: Selenium 4

copy
1#Assuming Chrome browser is being used and proper Selenium 4 package is installed23from selenium import webdriver4from selenium.webdriver.common.keys import Keys56#To use a local driver7driver = webdriver.Chrome()89#To connect to remote client with desired capabilities10#uncomment the below code and replace the necessary fields 11'''12from selenium.webdriver.common.desired_capabilities import DesiredCapabilities13driver = webdriver.Remote(14 command_executor='http:/​/​<remote-client-ip>:<port>/​wd/​hub',15 desired_capabilities=DesiredCapabilities.CHROME16)17'''1819#Assuming the URL of the webpage to be tested is already known and stored in a variable20url = "https:/​/​example.com"2122#Loading the webpage23driver.get(url)2425#Finding the error message field labels26error_field_labels = driver.find_elements_by_xpath("/​/​div[contains(@class,'error-msg')]/​/​label")2728#Assuming there are at least 1 error message fields present in the webpage29assert len(error_field_labels) > 0, "No error message field labels found on the webpage"3031#Checking if the proper field labels are being used on the error messages32for label in error_field_labels:33 assert label.text.endswith("_error"), "Improper field label being used in error message"3435#Closing the browser window36driver.quit()

Language: Javascript

Framework: Cypress

copy
1/​/​ Assuming the webpage under test has a form with two input fields - username and password2/​/​ The test logs in to the webpage and submits the form without inputting any values3/​/​ Then, it verifies that error messages for both fields are displayed with proper field labels45describe('General webpage functionality', () => {6 it('Verify error message field labels', () => {7 /​/​ Navigate to the webpage under test8 cy.visit('https:/​/​example.com/​login')910 /​/​ Input valid credentials and submit the form11 cy.get('#username').type('validuser')12 cy.get('#password').type('validpassword')13 cy.get('form').submit()1415 /​/​ Verify that error messages are not displayed16 cy.get('#error-message-username').should('not.exist')17 cy.get('#error-message-password').should('not.exist')1819 /​/​ Submit the form without inputting any values20 cy.get('form').submit()2122 /​/​ Verify that error messages are displayed with proper field labels23 cy.get('#error-message-username').should('contain', 'Username is required')24 cy.get('#error-message-password').should('contain', 'Password is required')2526 /​/​ Uncomment the following code to run the test on remote client with desired capabilities27 /​/​ const desiredCapabilities = {28 /​/​ browserName: 'chrome',29 /​/​ browserVersion: 'latest',30 /​/​ platformName: 'Windows 10'31 /​/​ }32 /​/​ cy.visit('https:/​/​example.com/​login', { userAgent: 'Mozilla/​5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/​537.36 (KHTML, like Gecko) Chrome/​86.0.4240.198 Safari/​537.36', desiredCapabilities })33 })34})

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